Sha256: f073b627da5ee1ddbf49a2eac49484aebc14f6134697a6c9c457b93d0ec6c1e0
Contents?: true
Size: 1.67 KB
Versions: 3
Compression:
Stored size: 1.67 KB
Contents
require_relative './operation' module Dentaku module AST class Comparator < Operation def self.precedence 5 end def type :logical end def operator raise NotImplementedError end 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 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 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 end class LessThan < Comparator def operator :< end end class LessThanOrEqual < Comparator def operator :<= end end class GreaterThan < Comparator def operator :> end end class GreaterThanOrEqual < Comparator def operator :>= end end class NotEqual < Comparator def operator :!= end end class Equal < Comparator def operator :== end def display_operator "=" end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
dentaku-3.5.4 | lib/dentaku/ast/comparators.rb |
dentaku-3.5.3 | lib/dentaku/ast/comparators.rb |
dentaku-3.5.2 | lib/dentaku/ast/comparators.rb |