Class: Querly::NodePair

Inherits:
Object
  • Object
show all
Defined in:
lib/querly/node_pair.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node:, parent: nil) ⇒ NodePair

Returns a new instance of NodePair.



6
7
8
9
# File 'lib/querly/node_pair.rb', line 6

def initialize(node:, parent: nil)
  @node = node
  @parent = parent
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



3
4
5
# File 'lib/querly/node_pair.rb', line 3

def node
  @node
end

#parentObject (readonly)

Returns the value of attribute parent.



4
5
6
# File 'lib/querly/node_pair.rb', line 4

def parent
  @parent
end

Instance Method Details

#childrenObject



11
12
13
14
15
16
17
18
19
# File 'lib/querly/node_pair.rb', line 11

def children
  node.children.flat_map do |child|
    if child.is_a?(Parser::AST::Node)
      self.class.new(node: child, parent: self)
    else
      []
    end
  end
end

#each_subpair(&block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/querly/node_pair.rb', line 21

def each_subpair(&block)
  if block_given?
    return unless node

    yield self

    children.each do |child|
      child.each_subpair &block
    end
  else
    enum_for :each_subpair
  end
end