Class: Avromatic::Model::Types::DecimalType

Inherits:
AbstractType show all
Defined in:
lib/avromatic/model/types/decimal_type.rb

Constant Summary collapse

VALUE_CLASSES =
[::BigDecimal].freeze
INPUT_CLASSES =
[::BigDecimal, ::Float, ::Integer].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractType

#coerced?, #coercible?

Constructor Details

#initialize(precision:, scale: 0) ⇒ DecimalType

Returns a new instance of DecimalType.



16
17
18
19
20
# File 'lib/avromatic/model/types/decimal_type.rb', line 16

def initialize(precision:, scale: 0)
  super()
  @precision = precision
  @scale = scale
end

Instance Attribute Details

#precisionObject (readonly)

Returns the value of attribute precision.



14
15
16
# File 'lib/avromatic/model/types/decimal_type.rb', line 14

def precision
  @precision
end

#scaleObject (readonly)

Returns the value of attribute scale.



14
15
16
# File 'lib/avromatic/model/types/decimal_type.rb', line 14

def scale
  @scale
end

Instance Method Details

#coerce(input) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/avromatic/model/types/decimal_type.rb', line 34

def coerce(input)
  case input
  when ::NilClass, ::BigDecimal
    input
  when ::Float, ::Integer
    input.to_d
  else
    raise ArgumentError.new("Could not coerce '#{input.inspect}' to #{name}")
  end
end

#input_classesObject



26
27
28
# File 'lib/avromatic/model/types/decimal_type.rb', line 26

def input_classes
  INPUT_CLASSES
end

#nameObject



30
31
32
# File 'lib/avromatic/model/types/decimal_type.rb', line 30

def name
  "decimal(#{precision}, #{scale})"
end

#referenced_model_classesObject



49
50
51
# File 'lib/avromatic/model/types/decimal_type.rb', line 49

def referenced_model_classes
  EMPTY_ARRAY
end

#serialize(value, _strict) ⇒ Object



45
46
47
# File 'lib/avromatic/model/types/decimal_type.rb', line 45

def serialize(value, _strict)
  value
end

#value_classesObject



22
23
24
# File 'lib/avromatic/model/types/decimal_type.rb', line 22

def value_classes
  VALUE_CLASSES
end