class ActiveAttr::Typecasting::BigDecimalTypecaster
Typecasts an Object
to a BigDecimal
@example Usage
BigDecimalTypecaster.new.call(1).to_s #=> "0.1E1"
@since 0.5.0
Public Instance Methods
call(value)
click to toggle source
Typecasts an object to a BigDecimal
Attempt to convert using to_d, else it creates a BigDecimal using the String representation of the value.
@example Typecast an Integer
typecaster.call(1).to_s #=> "0.1E1"
@param [Object, to_d, to_s] value The object to typecast
@return [BigDecimal, nil] The result of typecasting
@since 0.5.0
# File lib/active_attr/typecasting/big_decimal_typecaster.rb, line 28 def call(value) if value.is_a? BigDecimal value elsif value.is_a? Rational value.to_f.to_d elsif value.blank? nil elsif value.respond_to? :to_d value.to_d else BigDecimal(value.to_s) end rescue ArgumentError BigDecimal("0") end