Sha256: d1b75a8a00e0713ff22899701be4aff2505e0c1cbbbd9c2c92f535bc2465b5b8

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

require 'marc/spec/queries/comparison_string'
require 'marc/spec/queries/condition'
require 'marc/spec/queries/query'

module MARC
  module Spec
    module Queries
      class ConditionContext
        attr_reader :context_field, :context_result, :executor

        def initialize(context_field, context_result, executor)
          @context_field = context_field
          @context_result = context_result
          @executor = executor
        end

        def operand_value(operand, implicit: false)
          return operand_value(context_result) if implicit && operand.nil?

          raw_value = operand_value_raw(operand)
          is_boolean = [true, false].include?(raw_value)
          is_boolean ? raw_value : as_string(raw_value)
        end

        private

        def operand_value_raw(operand)
          return operand.str_exact if operand.is_a?(ComparisonString)
          return operand.met?(self) if operand.is_a?(Condition)
          return operand.execute(executor, [context_field], context_result) if operand.is_a?(Query)

          operand
        end

        def as_string(op_val)
          return unless op_val
          return op_val if op_val.is_a?(String)
          return op_val.value if op_val.respond_to?(:value) && !op_val.is_a?(MARC::DataField)
          return op_val.map { |v| as_string(v) } if op_val.is_a?(Array)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby-marc-spec-0.1.3 lib/marc/spec/queries/condition_context.rb