Class: Cel::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/cel/ast/elements/operation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(op, operands, depth: 1) ⇒ Operation

Returns a new instance of Operation.



9
10
11
12
13
14
# File 'lib/cel/ast/elements/operation.rb', line 9

def initialize(op, operands, depth: 1)
  @op = op
  @operands = operands
  @type = TYPES[:any]
  @depth = depth
end

Instance Attribute Details

#depthObject (readonly)

Returns the value of attribute depth.



5
6
7
# File 'lib/cel/ast/elements/operation.rb', line 5

def depth
  @depth
end

#opObject (readonly)

Returns the value of attribute op.



5
6
7
# File 'lib/cel/ast/elements/operation.rb', line 5

def op
  @op
end

#operandsObject (readonly)

Returns the value of attribute operands.



5
6
7
# File 'lib/cel/ast/elements/operation.rb', line 5

def operands
  @operands
end

#typeObject

Returns the value of attribute type.



7
8
9
# File 'lib/cel/ast/elements/operation.rb', line 7

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cel/ast/elements/operation.rb', line 16

def ==(other)
  case other
  when Array
    other.size == @operands.size + 1 &&
      other.first == @op &&
      other.slice(1..-1).zip(@operands).all? { |x1, x2| x1 == x2 }
  when Operation
    @op == other.op && @type == other.type && @operands == other.operands
  else
    super
  end
end

#to_sObject



33
34
35
36
37
# File 'lib/cel/ast/elements/operation.rb', line 33

def to_s
  return "#{@op}#{@operands.first}" if @operands.size == 1

  @operands.join(" #{@op} ")
end

#unary?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/cel/ast/elements/operation.rb', line 29

def unary?
  @operands.size == 1
end