Sha256: e29bf62257acf1f2076792790bd8bcf45dc40046f9fd02c7dc0cc9ec10a1afd8

Contents?: true

Size: 834 Bytes

Versions: 18

Compression:

Stored size: 834 Bytes

Contents

# frozen_string_literal: true

module Synvert::Core
  # Condition checks if nql or rules matches.
  class Rewriter::Condition
    # Initialize a Condition.
    #
    # @param instance [Synvert::Core::Rewriter::Instance]
    # @param nql_or_rules [String|Hash]
    # @yield run when condition matches
    def initialize(instance, nql_or_rules, &block)
      @instance = instance
      @node_query = NodeQuery.new(nql_or_rules, adapter: instance.parser)
      @block = block
    end

    # If condition matches, run the block code.
    def process
      @instance.instance_eval(&@block) if match?
    end

    protected

    # Check if condition matches
    #
    # @abstract
    def match?
      raise NotImplementedError, 'must be implemented by subclasses'
    end

    def target_node
      @instance.current_node
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
synvert-core-1.35.4 lib/synvert/core/rewriter/condition.rb
synvert-core-1.35.3 lib/synvert/core/rewriter/condition.rb
synvert-core-1.35.2 lib/synvert/core/rewriter/condition.rb
synvert-core-1.35.1 lib/synvert/core/rewriter/condition.rb
synvert-core-1.35.0 lib/synvert/core/rewriter/condition.rb
synvert-core-1.34.0 lib/synvert/core/rewriter/condition.rb
synvert-core-1.33.3 lib/synvert/core/rewriter/condition.rb
synvert-core-1.33.2 lib/synvert/core/rewriter/condition.rb
synvert-core-1.33.1 lib/synvert/core/rewriter/condition.rb
synvert-core-1.33.0 lib/synvert/core/rewriter/condition.rb
synvert-core-1.32.1 lib/synvert/core/rewriter/condition.rb
synvert-core-1.32.0 lib/synvert/core/rewriter/condition.rb
synvert-core-1.31.1 lib/synvert/core/rewriter/condition.rb
synvert-core-1.31.0 lib/synvert/core/rewriter/condition.rb
synvert-core-1.30.3 lib/synvert/core/rewriter/condition.rb
synvert-core-1.30.2 lib/synvert/core/rewriter/condition.rb
synvert-core-1.30.1 lib/synvert/core/rewriter/condition.rb
synvert-core-1.30.0 lib/synvert/core/rewriter/condition.rb