Sha256: 4c4b1ee115220ebdf7f2be3aca13d0199e876f445a26a745fadbe47e0407c084

Contents?: true

Size: 1.3 KB

Versions: 9

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module Synvert::Core::NodeQuery::Compiler
  # Attribute is a pair of key, value and operator,
  class Attribute
    # Initialize a Attribute.
    # @param key [String] the key
    # @param value the value can be any class implement {Synvert::Core::NodeQuery::Compiler::Comparable}
    # @param operator [Symbol] the operator
    def initialize(key:, value:, operator: :==)
      @key = key
      @value = value
      @operator = operator
    end

    # Check if the node matches the attribute.
    # @param node [Parser::AST::Node] the node
    # @return [Boolean]
    def match?(node)
      @value.base_node = node if @value.is_a?(DynamicAttribute)
      node && @value.match?(node.child_node_by_name(@key), @operator)
    end

    def to_s
      case @operator
      when :!=
        "#{@key}!=#{@value}"
      when :=~
        "#{@key}=~#{@value}"
      when :!~
        "#{@key}!~#{@value}"
      when :>
        "#{@key}>#{@value}"
      when :>=
        "#{@key}>=#{@value}"
      when :<
        "#{@key}<#{@value}"
      when :<=
        "#{@key}<=#{@value}"
      when :in
        "#{@key} in (#{@value})"
      when :not_in
        "#{@key} not in (#{@value})"
      when :includes
        "#{@key} includes #{@value}"
      else
        "#{@key}=#{@value}"
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
synvert-core-1.2.1 lib/synvert/core/node_query/compiler/attribute.rb
synvert-core-1.2.0 lib/synvert/core/node_query/compiler/attribute.rb
synvert-core-1.1.1 lib/synvert/core/node_query/compiler/attribute.rb
synvert-core-1.1.0 lib/synvert/core/node_query/compiler/attribute.rb
synvert-core-1.0.5 lib/synvert/core/node_query/compiler/attribute.rb
synvert-core-1.0.4 lib/synvert/core/node_query/compiler/attribute.rb
synvert-core-1.0.3 lib/synvert/core/node_query/compiler/attribute.rb
synvert-core-1.0.2 lib/synvert/core/node_query/compiler/attribute.rb
synvert-core-1.0.1 lib/synvert/core/node_query/compiler/attribute.rb