Sha256: aa084e21ad988310bc5d14bf33242fd4f093ea784940851580dbf11026ed4d2f

Contents?: true

Size: 1.25 KB

Versions: 10

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module Synvert::Core
  # QueryScope finds out nodes by using node query language, then changes its scope to matching node.
  class Rewriter::QueryScope < Rewriter::Scope
    # Initialize a QueryScope.
    #
    # @param instance [Synvert::Core::Rewriter::Instance]
    # @param nql [String]
    # @param options [Hash]
    # @yield run on all matching nodes
    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

    # Find out the matching nodes.
    #
    # It checks the current node and iterates all child nodes,
    # then run the block code on each matching node.
    # @raise [Synvert::Core::NodeQuery::Compiler::ParseError] if the query string is invalid.
    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
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
synvert-core-1.11.0 lib/synvert/core/rewriter/scope/query_scope.rb
synvert-core-1.10.1 lib/synvert/core/rewriter/scope/query_scope.rb
synvert-core-1.10.0 lib/synvert/core/rewriter/scope/query_scope.rb
synvert-core-1.9.2 lib/synvert/core/rewriter/scope/query_scope.rb
synvert-core-1.9.1 lib/synvert/core/rewriter/scope/query_scope.rb
synvert-core-1.9.0 lib/synvert/core/rewriter/scope/query_scope.rb
synvert-core-1.8.1 lib/synvert/core/rewriter/scope/query_scope.rb
synvert-core-1.8.0 lib/synvert/core/rewriter/scope/query_scope.rb
synvert-core-1.7.0 lib/synvert/core/rewriter/scope/query_scope.rb
synvert-core-1.6.0 lib/synvert/core/rewriter/scope/query_scope.rb