Module: FidorApi::Model::Helpers::AttributeDecimalMethods

Defined in:
lib/fidor_api/model/helpers/attribute_decimal_methods.rb

Instance Method Summary collapse

Instance Method Details

#attribute_decimal_methods(name) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fidor_api/model/helpers/attribute_decimal_methods.rb', line 6

def attribute_decimal_methods(name)
  define_method name do
    value = read_attribute(name)
    return if value.nil?

    BigDecimal((value / 100.0).to_s)
  end

  define_method "#{name}=" do |value|
    case value
    when String
      value = (BigDecimal(value) * 100.0).to_i
    when Integer
      value = value
    when BigDecimal
      value = (value * 100.0).to_i
    else
      raise ArgumentError
    end

    write_attribute(name, value)
  end
end