Class: NodeQuery::Compiler::Expression

Inherits:
Object
  • Object
show all
Defined in:
lib/node_query/compiler/expression.rb

Overview

Expression represents a node query expression.

Instance Method Summary collapse

Constructor Details

#initialize(selector: nil, rest: nil) ⇒ Expression

Initialize a Expression.

Parameters:



9
10
11
12
# File 'lib/node_query/compiler/expression.rb', line 9

def initialize(selector: nil, rest: nil)
  @selector = selector
  @rest = rest
end

Instance Method Details

#query_nodes(node, options = {}) ⇒ Array<Node>

Query nodes by the selector and the rest expression.

Parameters:

  • node (Node)

    node to match

  • options (Hash) (defaults to: {})

    if query the current node

Options Hash (options):

  • :including_self (boolean)

    if query the current node, default is ture

  • :stop_at_first_match (boolean)

    if stop at first match, default is false

  • :recursive (boolean)

    if recursively query child nodes, default is true

Returns:

  • (Array<Node>)

    matching nodes.



21
22
23
24
25
26
27
28
# File 'lib/node_query/compiler/expression.rb', line 21

def query_nodes(node, options = {})
  matching_nodes = @selector.query_nodes(node, options)
  return matching_nodes if @rest.nil?

  matching_nodes.flat_map do |matching_node|
    @rest.query_nodes(matching_node, options)
  end
end

#to_sObject



30
31
32
33
34
35
# File 'lib/node_query/compiler/expression.rb', line 30

def to_s
  result = []
  result << @selector.to_s if @selector
  result << @rest.to_s if @rest
  result.join(' ')
end