Class: Synvert::Core::NodeQuery::Compiler::Expression
- Inherits:
-
Object
- Object
- Synvert::Core::NodeQuery::Compiler::Expression
- Defined in:
- lib/synvert/core/node_query/compiler/expression.rb
Overview
Expression represents a node query expression.
Instance Method Summary collapse
-
#initialize(selector: nil, rest: nil) ⇒ Expression
constructor
Initialize a Expression.
-
#match?(node) ⇒ Boolean
Check if the node matches the expression.
-
#query_nodes(node) ⇒ Array<Parser::AST::Node>
Query nodes by the selector and the rest expression.
- #to_s ⇒ Object
Constructor Details
#initialize(selector: nil, rest: nil) ⇒ Expression
Initialize a Expression.
9 10 11 12 |
# File 'lib/synvert/core/node_query/compiler/expression.rb', line 9 def initialize(selector: nil, rest: nil) @selector = selector @rest = rest end |
Instance Method Details
#match?(node) ⇒ Boolean
Check if the node matches the expression.
17 18 19 |
# File 'lib/synvert/core/node_query/compiler/expression.rb', line 17 def match?(node) !query_nodes(node).empty? end |
#query_nodes(node) ⇒ Array<Parser::AST::Node>
Query nodes by the selector and the rest expression.
25 26 27 28 29 30 31 32 |
# File 'lib/synvert/core/node_query/compiler/expression.rb', line 25 def query_nodes(node) matching_nodes = @selector.query_nodes(node) return matching_nodes if @rest.nil? matching_nodes.flat_map do |matching_node| @rest.query_nodes(matching_node) end end |
#to_s ⇒ Object
34 35 36 37 38 39 |
# File 'lib/synvert/core/node_query/compiler/expression.rb', line 34 def to_s result = [] result << @selector.to_s if @selector result << @rest.to_s if @rest result.join(' ') end |