class Dentaku::AST::Comparator

Public Class Methods

precedence() click to toggle source
# File lib/dentaku/ast/comparators.rb, line 6
def self.precedence
  5
end

Public Instance Methods

operator() click to toggle source
# File lib/dentaku/ast/comparators.rb, line 14
def operator
  raise NotImplementedError
end
type() click to toggle source
# File lib/dentaku/ast/comparators.rb, line 10
def type
  :logical
end
value(context = {}) click to toggle source
# File lib/dentaku/ast/comparators.rb, line 18
def value(context = {})
  l = validate_value(cast(left.value(context)))
  r = validate_value(cast(right.value(context)))

  l.public_send(operator, r)
rescue ::ArgumentError => e
  raise Dentaku::ArgumentError.for(:incompatible_type, value: r, for: l.class), e.message
end

Private Instance Methods

cast(val) click to toggle source
# File lib/dentaku/ast/comparators.rb, line 29
def cast(val)
  return val unless val.is_a?(::String)
  return val unless val.match?(Arithmetic::DECIMAL) || val.match?(Arithmetic::INTEGER)

  v = BigDecimal(val, Float::DIG + 1)
  v = v.to_i if v.frac.zero?
  v
end
validate_value(value) click to toggle source
# File lib/dentaku/ast/comparators.rb, line 38
def validate_value(value)
  unless value.respond_to?(operator)
    raise Dentaku::ArgumentError.for(:invalid_operator, operation: self.class, operator: operator),
          "#{ self.class } requires operands that respond to #{operator}"
  end

  value
end