Class: PathList::Walkers::GitignoreCollectingFileSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/path_list/walkers/gitignore_collecting_file_system.rb

Instance Method Summary collapse

Constructor Details

#initialize(rule_groups) ⇒ GitignoreCollectingFileSystem

Returns a new instance of GitignoreCollectingFileSystem.



6
7
8
# File 'lib/path_list/walkers/gitignore_collecting_file_system.rb', line 6

def initialize(rule_groups)
  @rule_groups = rule_groups
end

Instance Method Details

#allowed?(path, directory: nil, content: nil) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
20
# File 'lib/path_list/walkers/gitignore_collecting_file_system.rb', line 10

def allowed?(path, directory: nil, content: nil)
  full_path = ::File.expand_path(path)
  return false if directory.nil? ? ::File.lstat(full_path).directory? : directory

  @rule_groups.add_gitignore_to_root(full_path)

  candidate = ::PathList::RootCandidate.new(full_path, nil, directory, content)
  @rule_groups.allowed_recursive?(candidate)
rescue ::Errno::ENOENT, ::Errno::EACCES, ::Errno::ENOTDIR, ::Errno::ELOOP, ::Errno::ENAMETOOLONG
  false
end

#each(parent_full_path, parent_relative_path, &block) ⇒ Object

rubocop:disable Metrics/MethodLength



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/path_list/walkers/gitignore_collecting_file_system.rb', line 22

def each(parent_full_path, parent_relative_path, &block) # rubocop:disable Metrics/MethodLength
  children = ::Dir.children(parent_full_path)
  @rule_groups.add_gitignore(parent_full_path) if children.include?('.gitignore')

  children.each do |filename|
    begin
      full_path = parent_full_path + filename
      relative_path = parent_relative_path + filename
      dir = ::File.lstat(full_path).directory?
      candidate = ::PathList::RootCandidate.new(full_path, filename, dir, nil)

      next unless @rule_groups.allowed_unrecursive?(candidate)

      if dir
        each(full_path + '/', relative_path + '/', &block)
      else
        yield relative_path
      end
    rescue ::Errno::ENOENT, ::Errno::EACCES, ::Errno::ENOTDIR, ::Errno::ELOOP, ::Errno::ENAMETOOLONG
      nil
    end
  end
end