Sha256: 2c4153939cbe0d186d46bdcfdf5ca3b5126a0b3f2ca32f82982f5bd00022b4ba

Contents?: true

Size: 1.37 KB

Versions: 3

Compression:

Stored size: 1.37 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 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 unless operand

          case operand
          when ComparisonString
            operand.str_exact
          when Condition
            operand.met?(self)
          when Query
            operand.execute(executor, [context_field], context_result)
          end
        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

3 entries across 3 versions & 1 rubygems

Version Path
ruby-marc-spec-0.1.2 lib/marc/spec/queries/condition_context.rb
ruby-marc-spec-0.1.1 lib/marc/spec/queries/condition_context.rb
ruby-marc-spec-0.1.0 lib/marc/spec/queries/condition_context.rb