Class: Cel::Duration

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

Instance Attribute Summary

Attributes inherited from Literal

#type, #value

Instance Method Summary collapse

Methods inherited from Literal

to_cel_type

Methods included from CelMethods

included

Constructor Details

#initialize(value) ⇒ Duration

Returns a new instance of Duration.

Raises:



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cel/ast/elements/duration.rb', line 5

def initialize(value)
  value =
    case value
    when ::String
      init_from_string(value)
    when Hash
      seconds, nanos = value.values_at(:seconds, :nanos)
      seconds ||= 0
      nanos ||= 0
      seconds + (nanos / 1_000_000_000.0)
    else
      value
    end

  raise EvaluateError, "out of range" unless (value * 1_000_000_000).between?(-MAX_INT, MAX_INT)

  super(:duration, value)
end

Instance Method Details

#to_ruby_typeObject



82
83
84
# File 'lib/cel/ast/elements/duration.rb', line 82

def to_ruby_type
  Protobuf.duration_class.new(seconds: getSeconds.value, nanos: getMilliseconds.value * 1_000_000_000.0)
end

#to_sObject



24
25
26
27
28
# File 'lib/cel/ast/elements/duration.rb', line 24

def to_s
  seconds = getSeconds
  millis = getMilliseconds
  millis.positive? ? "#{seconds}s#{millis}m" : "#{seconds}s"
end