Class: Chef::ChefFS::FileSystem::Lister

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/chef/chef_fs/file_system.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, pattern) ⇒ Lister

Returns a new instance of Lister.



43
44
45
46
# File 'lib/chef/chef_fs/file_system.rb', line 43

def initialize(root, pattern)
  @root = root
  @pattern = pattern
end

Instance Attribute Details

#patternObject (readonly)

Returns the value of attribute pattern.



49
50
51
# File 'lib/chef/chef_fs/file_system.rb', line 49

def pattern
  @pattern
end

#rootObject (readonly)

Returns the value of attribute root.



48
49
50
# File 'lib/chef/chef_fs/file_system.rb', line 48

def root
  @root
end

Instance Method Details

#each(&block) ⇒ Object



51
52
53
# File 'lib/chef/chef_fs/file_system.rb', line 51

def each(&block)
  list_from(root, &block)
end

#list_from(entry, &block) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/chef/chef_fs/file_system.rb', line 55

def list_from(entry, &block)
  # Include self in results if it matches
  if pattern.match?(entry.display_path)
    yield(entry)
  end

  if pattern.could_match_children?(entry.display_path)
    # If it's possible that our children could match, descend in and add matches.
    exact_child_name = pattern.exact_child_name_under(entry.display_path)

    # If we've got an exact name, don't bother listing children; just grab the
    # child with the given name.
    if exact_child_name
      exact_child = entry.child(exact_child_name)
      if exact_child
        list_from(exact_child, &block)
      end

      # Otherwise, go through all children and find any matches
    elsif entry.dir?
      results = entry.children.parallel_map { |child| Chef::ChefFS::FileSystem.list(child, pattern) }
      results.flat_each(&block)
    end
  end
end