Class: PathList::Walkers::FileSystem

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

Instance Method Summary collapse

Constructor Details

#initialize(rule_groups) ⇒ FileSystem

Returns a new instance of FileSystem.



6
7
8
# File 'lib/path_list/walkers/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
# File 'lib/path_list/walkers/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

  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



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

def each(parent_full_path, parent_relative_path, &block) # rubocop:disable Metrics/MethodLength
  children = ::Dir.children(parent_full_path)

  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