Class: Exa::Visitor
- Inherits:
-
Object
- Object
- Exa::Visitor
- Defined in:
- lib/exa/visitor.rb
Instance Method Summary collapse
- #gather_branches(branch = @root, depth: 10) ⇒ Object
- #gather_leaves(branch = @root, depth: 10) ⇒ Object
-
#initialize(root) ⇒ Visitor
constructor
A new instance of Visitor.
- #query(path) ⇒ Object
- #seek(path, create_missing: true) ⇒ Object
Constructor Details
Instance Method Details
#gather_branches(branch = @root, depth: 10) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/exa/visitor.rb', line 18 def gather_branches(branch=@root, depth: 10) return [] if depth < 0 if branch.children.any? [ branch ] + branch.children.flat_map do |child| gather_branches(child, depth: depth-1) end.uniq else # we are a leaf! [ ] end end |
#gather_leaves(branch = @root, depth: 10) ⇒ Object
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/exa/visitor.rb', line 7 def gather_leaves(branch=@root, depth: 10) return [] if depth < 0 if branch.children.any? branch.children.flat_map do |child| gather_leaves(child, depth: depth-1) end.uniq else # we are a leaf! [ branch ] end end |
#query(path) ⇒ Object
48 49 50 51 52 53 54 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/exa/visitor.rb', line 48 def query(path) path = '/' + path unless path.start_with?('/') _root, next_segment, *remaining_segments = path.split('/') remaining_path = '/' + remaining_segments.join('/') current = @root return [current] unless next_segment if next_segment == '*' # need to multiplex remaining query across *all* children next_children = current.children if remaining_segments.any? next_children.flat_map do |child| child.query(remaining_path).uniq end else next_children end elsif next_segment == '**' # this is more subtle, and really points to: # do we need to be treating the path query as a regular expression? # and matching against *all* filenames? if remaining_segments.any? gather_branches(current).flat_map do |folder| folder.query(remaining_path).uniq end else gather_leaves(current) end elsif next_segment == '..' # need to back up... parent = current.parent if remaining_segments.any? parent.children.flat_map do |child| child.query(remaining_path).uniq end.uniq else [ parent ] end else # need to find just child matching *this* segment next_child = current.children.detect { |c| c.name == next_segment } if next_child if remaining_segments.any? next_child.query(remaining_path).uniq else [ next_child ] end else [] end end end |
#seek(path, create_missing: true) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/exa/visitor.rb', line 29 def seek(path, create_missing: true) path = '/' + path unless path.start_with?('/') _root, *path_segments = path.split('/') current = @root path_segments.each do |segment| next_child = current.children.detect do |child| child.name == segment end current = if next_child next_child else return nil unless create_missing current.create_child(child_name: segment) end end current end |