Class: Wood::TreePattern::NodeFinder
- Inherits:
-
Object
- Object
- Wood::TreePattern::NodeFinder
- Defined in:
- lib/wood/tree_pattern/node_finder.rb
Overview
Provides methods for searching for parent and child nodes relative to a given Node.
Instance Attribute Summary collapse
-
#ast ⇒ Object
Node to start relative search from.
Instance Method Summary collapse
- #delete_child_node(node_pattern) ⇒ Object
- #delete_child_nodes(node_pattern) ⇒ Object
-
#find_child_node(node_pattern) ⇒ Node
Finds a child node that matches a given pattern.
-
#find_child_nodes(node_pattern) ⇒ Array<Node>
Finds all child nodes that match a given pattern.
-
#find_parent_node(node_pattern) ⇒ Node
Finds a parent node that matches a given pattern.
-
#find_parent_nodes(node_pattern) ⇒ Array<Node>
Finds any parent nodes that match a given pattern.
-
#initialize(ast = nil) ⇒ NodeFinder
constructor
Initializes NodeFinder with a given Node.
Constructor Details
#initialize(ast = nil) ⇒ NodeFinder
Initializes Wood::TreePattern::NodeFinder with a given Node.
11 12 13 |
# File 'lib/wood/tree_pattern/node_finder.rb', line 11 def initialize(ast = nil) @ast = ast end |
Instance Attribute Details
#ast ⇒ Object
Node to start relative search from.
6 7 8 |
# File 'lib/wood/tree_pattern/node_finder.rb', line 6 def ast @ast end |
Instance Method Details
#delete_child_node(node_pattern) ⇒ Object
66 67 68 |
# File 'lib/wood/tree_pattern/node_finder.rb', line 66 def delete_child_node(node_pattern) __delete_child_node__(ast, node_pattern) end |
#delete_child_nodes(node_pattern) ⇒ Object
70 71 72 |
# File 'lib/wood/tree_pattern/node_finder.rb', line 70 def delete_child_nodes(node_pattern) __delete_child_nodes__(ast, node_pattern) end |
#find_child_node(node_pattern) ⇒ Node
Finds a child node that matches a given pattern.
44 45 46 47 48 49 50 51 |
# File 'lib/wood/tree_pattern/node_finder.rb', line 44 def find_child_node(node_pattern) ast.child_nodes.each do |child| if found = __find_child_node__(child, node_pattern) return found end end return nil end |
#find_child_nodes(node_pattern) ⇒ Array<Node>
Finds all child nodes that match a given pattern.
57 58 59 60 61 62 63 64 |
# File 'lib/wood/tree_pattern/node_finder.rb', line 57 def find_child_nodes(node_pattern) found = [] ast.child_nodes.each do |child| found += __find_child_nodes__(child, node_pattern) end found.uniq! return found end |
#find_parent_node(node_pattern) ⇒ Node
Finds a parent node that matches a given pattern.
19 20 21 22 23 24 25 |
# File 'lib/wood/tree_pattern/node_finder.rb', line 19 def find_parent_node(node_pattern) ast = @ast while ast = ast.parent_node return ast if node_pattern === ast end return nil end |
#find_parent_nodes(node_pattern) ⇒ Array<Node>
Finds any parent nodes that match a given pattern.
31 32 33 34 35 36 37 38 |
# File 'lib/wood/tree_pattern/node_finder.rb', line 31 def find_parent_nodes(node_pattern) ast = @ast found = [] while ast = ast.parent_node found << ast if node_pattern === ast end return found end |