Class: Cel::Duration
- Defined in:
- lib/cel/ast/elements/duration.rb
Instance Attribute Summary
Attributes inherited from Literal
Instance Method Summary collapse
-
#initialize(value) ⇒ Duration
constructor
A new instance of Duration.
- #to_ruby_type ⇒ Object
- #to_s ⇒ Object
Methods inherited from Literal
Methods included from CelMethods
Constructor Details
#initialize(value) ⇒ Duration
Returns a new instance of Duration.
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_type ⇒ Object
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_s ⇒ Object
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 |