class ActiveModel::Type::Float
-
"NaN"
is cast toFloat::NAN
.
-"-Infinity"
is cast to-Float::INFINITY
.
-"Infinity"
is cast toFloat::INFINITY
.
- Blank strings are cast tonil
.
strings:
Values are cast using theirto_f
method, except for the following
bag.weight # => Float::NAN
bag.weight = “NaN”
bag.weight # => nil
bag.weight = “”
bag.weight # => 0.25
bag.weight = “0.25”
bag = BagOfCoffee.new
end
attribute :weight, :float
include ActiveModel::Attributes
class BagOfCoffee
the:float
key.
Attribute type for floating point numeric values. It is registered under
= Active Model Float Type
def cast_value(value)
def cast_value(value) case value when ::Float then value when "Infinity" then ::Float::INFINITY when "-Infinity" then -::Float::INFINITY when "NaN" then ::Float::NAN else value.to_f end end
def type
def type :float end
def type_cast_for_schema(value)
def type_cast_for_schema(value) return "::Float::NAN" if value.try(:nan?) case value when ::Float::INFINITY then "::Float::INFINITY" when -::Float::INFINITY then "-::Float::INFINITY" else super end end