lib/cel/ast/elements.rb in cel-0.2.1 vs lib/cel/ast/elements.rb in cel-0.2.2

- old
+ new

@@ -141,10 +141,11 @@ super(value) check end def ==(other) + other = other.value if other.is_a?(Literal) @value == other || super end def self.to_cel_type(val) val = Protobuf.convert_from_protobuf(val) if val.is_a?(Google::Protobuf::MessageExts) @@ -192,11 +193,11 @@ Number.new(@type, super) end OUT end - LOGICAL_OPERATORS.each do |op| + (LOGICAL_OPERATORS - %w[==]).each do |op| class_eval(<<-OUT, __FILE__, __LINE__ + 1) def #{op}(other) Bool.new(super) end OUT @@ -206,17 +207,21 @@ class Bool < Literal def initialize(value) super(:bool, value) end - LOGICAL_OPERATORS.each do |op| + (LOGICAL_OPERATORS - %w[==]).each do |op| class_eval(<<-OUT, __FILE__, __LINE__ + 1) def #{op}(other) Bool.new(super) end OUT end + + def ! + Bool.new(super) + end end class Null < Literal def initialize super(:null_type, nil) @@ -244,18 +249,10 @@ def matches(pattern) Macro.matches(self, pattern) end - LOGICAL_OPERATORS.each do |op| - class_eval(<<-OUT, __FILE__, __LINE__ + 1) - def #{op}(other) - other.is_a?(Cel::Literal) ? Bool.new(super) : super - end - OUT - end - %i[+ -].each do |op| class_eval(<<-OUT, __FILE__, __LINE__ + 1) def #{op}(other) String.new(super) end @@ -270,11 +267,11 @@ def to_ary [self] end - LOGICAL_OPERATORS.each do |op| + (LOGICAL_OPERATORS - %w[==]).each do |op| class_eval(<<-OUT, __FILE__, __LINE__ + 1) def #{op}(other) Bool.new(super) end OUT @@ -356,11 +353,11 @@ end class Timestamp < Literal def initialize(value) value = case value - when String then Time.parse(value) + when ::String then Time.parse(value) when Numeric then Time.at(value) else value end super(:timestamp, value) end @@ -441,11 +438,11 @@ end class Duration < Literal def initialize(value) value = case value - when String + when ::String init_from_string(value) when Hash seconds, nanos = value.values_at(:seconds, :nanos) seconds ||= 0 nanos ||= 0 @@ -549,9 +546,13 @@ when Operation @op == other.op && @type == other.type && @operands == other.operands else super end + end + + def unary? + @operands.size == 1 end def to_s return "#{@op}#{@operands.first}" if @operands.size == 1