Class: DecisionAgent::Dmn::Feel::Types::Number

Inherits:
Object
  • Object
show all
Defined in:
lib/decision_agent/dmn/feel/types.rb

Overview

Wrapper for numbers with precision tracking

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, scale: nil) ⇒ Number

Returns a new instance of Number.



18
19
20
21
22
23
24
25
26
27
# File 'lib/decision_agent/dmn/feel/types.rb', line 18

def initialize(value, scale: nil)
  @value = case value
           when BigDecimal then value
           when String then BigDecimal(value)
           when Integer, Float then value
           else
             raise FeelTypeError, "Cannot convert #{value.class} to Number: #{value.inspect}"
           end
  @scale = scale
end

Instance Attribute Details

#scaleObject (readonly)

Returns the value of attribute scale.



16
17
18
# File 'lib/decision_agent/dmn/feel/types.rb', line 16

def scale
  @scale
end

#valueObject (readonly)

Returns the value of attribute value.



16
17
18
# File 'lib/decision_agent/dmn/feel/types.rb', line 16

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object



41
42
43
44
45
# File 'lib/decision_agent/dmn/feel/types.rb', line 41

def ==(other)
  return @value == other.value if other.is_a?(Number)

  @value == other
end

#inspectObject



47
48
49
# File 'lib/decision_agent/dmn/feel/types.rb', line 47

def inspect
  "#<Feel::Number #{@value}#{" (scale: #{@scale})" if @scale}>"
end

#to_fObject



37
38
39
# File 'lib/decision_agent/dmn/feel/types.rb', line 37

def to_f
  @value.to_f
end

#to_iObject



33
34
35
# File 'lib/decision_agent/dmn/feel/types.rb', line 33

def to_i
  @value.to_i
end

#to_rubyObject



29
30
31
# File 'lib/decision_agent/dmn/feel/types.rb', line 29

def to_ruby
  @value
end