Class: Synvert::Core::Rewriter::QueryScope

Inherits:
Scope
  • Object
show all
Defined in:
lib/synvert/core/rewriter/scope/query_scope.rb

Overview

QueryScope finds out nodes by using node query language, then changes its scope to matching node.

Instance Method Summary collapse

Constructor Details

#initialize(instance, nql, options = {}) { ... } ⇒ QueryScope

Initialize a QueryScope.

Parameters:

Yields:

  • run on all matching nodes



12
13
14
15
16
17
# File 'lib/synvert/core/rewriter/scope/query_scope.rb', line 12

def initialize(instance, nql, options = {}, &block)
  super(instance, &block)

  @options = { including_self: true, stop_at_first_match: false, recursive: true }.merge(options)
  @node_query = NodeQuery.new(nql)
end

Instance Method Details

#processObject

Find out the matching nodes.

It checks the current node and iterates all child nodes, then run the block code on each matching node.

Raises:

  • (Synvert::Core::NodeQuery::Compiler::ParseError)

    if the query string is invalid.



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/synvert/core/rewriter/scope/query_scope.rb', line 24

def process
  current_node = @instance.current_node
  return unless current_node

  matching_nodes = @node_query.query_nodes(current_node, @options)
  @instance.process_with_node(current_node) do
    matching_nodes.each do |node|
      @instance.process_with_node(node) do
        @instance.instance_eval(&@block)
      end
    end
  end
end