Class: AttrTypecastable::Types::BigDecimal

Inherits:
Base
  • Object
show all
Defined in:
lib/attr_typecastable/types/big_decimal.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize, #typecast

Constructor Details

This class inherits a constructor from AttrTypecastable::Types::Base

Instance Method Details

#do_typecast(value) ⇒ Object



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/attr_typecastable/types/big_decimal.rb', line 6

def do_typecast(value)
  require 'bigdecimal/util'

  return value if value.is_a?(::BigDecimal)

  if value.is_a?(::Rational)
    if @options[:fig]
      value.to_d(@options[:fig])
    else
      value.to_f.to_d
    end
  elsif value.is_a?(::Float)
    if @options[:precision]
      value.to_d(@options[:precision])
    else
      value.to_d
    end
  elsif value.respond_to?(:to_d)
    value.to_d
  else
    BigDecimal.new(value.to_s)
  end
end