Class: Wood::TreePattern::NodeFinder

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(ast = nil) ⇒ NodeFinder

Initializes Wood::TreePattern::NodeFinder with a given Node.

Parameters:

  • ast (Node) (defaults to: nil)

    AST node to start the search from.



11
12
13
# File 'lib/wood/tree_pattern/node_finder.rb', line 11

def initialize(ast = nil)
  @ast = ast
end

Instance Attribute Details

#astObject

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.

Parameters:

  • node_pattern

    Node pattern to find a child node with.

Returns:

  • (Node)

    First child node that matches ‘node_pattern` or `nil`.



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.

Parameters:

  • node_pattern

    Node pattern to find child nodes with.

Returns:

  • (Array<Node>)

    All child nodes that match ‘node_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.

Parameters:

  • node_pattern

    Node pattern to find a parent node with.

Returns:

  • (Node)

    Parent node found via ‘node_pattern` or `nil`.



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.

Parameters:

  • node_pattern

    Node pattern to find parent nodes with.

Returns:

  • (Array<Node>)

    All parent nodes that match ‘node_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