class ActiveModel::Type::Float
bag.weight # => Float::NAN
bag.weight = “NaN”
bag.weight # => nil
bag.weight = “”
bag.weight # => 0.25
bag.weight = “0.25”
bag = BagOfCoffee.new
- "NaN"
is cast to Float::NAN
.
- "-Infinity"
is cast to -Float::INFINITY
.
- "Infinity"
is cast to Float::INFINITY
.
- Blank strings are cast to nil
.
strings:
Values are cast using their to_f
method, except for the following
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