File tree Expand file tree Collapse file tree 3 files changed +35
-16
lines changed
activemodel/lib/active_model/type Expand file tree Collapse file tree 3 files changed +35
-16
lines changed Original file line number Diff line number Diff line change @@ -13,15 +13,24 @@ module Type
13
13
# attribute :weight, :decimal
14
14
# end
15
15
#
16
+ # Numeric instances are converted to BigDecimal instances. Any other objects
17
+ # are cast using their +to_d+ method, except for blank strings, which are
18
+ # cast to +nil+. If a +to_d+ method is not defined, the object is converted
19
+ # to a string using +to_s+, which is then cast using +to_d+.
20
+ #
16
21
# bag = BagOfCoffee.new
17
- # bag.weight = "0.0001"
18
22
#
19
- # bag.weight # => 0.1e-3
23
+ # bag.weight = 0.01
24
+ # bag.weight # => 0.1e-1
20
25
#
21
- # Numeric instances are converted to BigDecimal instances. Any other objects
22
- # are cast using their +to_d+ method, if it exists. If it does not exist,
23
- # the object is converted to a string using +to_s+, which is then coerced to
24
- # a BigDecimal using +to_d+.
26
+ # bag.weight = "0.01"
27
+ # bag.weight # => 0.1e-1
28
+ #
29
+ # bag.weight = ""
30
+ # bag.weight # => nil
31
+ #
32
+ # bag.weight = :arbitrary
33
+ # bag.weight # => nil (the result of `.to_s.to_d`)
25
34
#
26
35
# Decimal precision defaults to 18, and can be customized when declaring an
27
36
# attribute:
Original file line number Diff line number Diff line change @@ -13,18 +13,24 @@ module Type
13
13
# attribute :weight, :float
14
14
# end
15
15
#
16
+ # Values are cast using their +to_f+ method, except for the following
17
+ # strings:
18
+ #
19
+ # - Blank strings are cast to +nil+.
20
+ # - <tt>"Infinity"</tt> is cast to <tt>Float::INFINITY</tt>.
21
+ # - <tt>"-Infinity"</tt> is cast to <tt>-Float::INFINITY</tt>.
22
+ # - <tt>"NaN"</tt> is cast to <tt>Float::NAN</tt>.
23
+ #
16
24
# bag = BagOfCoffee.new
17
- # bag.weight = "0.25"
18
25
#
26
+ # bag.weight = "0.25"
19
27
# bag.weight # => 0.25
20
28
#
21
- # Values are coerced to their float representation using their +to_f+
22
- # methods. However, the following strings which represent floating point
23
- # constants are cast accordingly:
29
+ # bag.weight = ""
30
+ # bag.weight # => nil
24
31
#
25
- # - <tt>"Infinity"</tt> is cast to <tt>Float::INFINITY</tt>.
26
- # - <tt>"-Infinity"</tt> is cast to <tt>-Float::INFINITY</tt>.
27
- # - <tt>"NaN"</tt> is cast to <tt>Float::NAN</tt>.
32
+ # bag.weight = "NaN"
33
+ # bag.weight # => Float::NAN
28
34
class Float < Value
29
35
include Helpers ::Numeric
30
36
Original file line number Diff line number Diff line change @@ -11,13 +11,17 @@ module Type
11
11
# attribute :age, :integer
12
12
# end
13
13
#
14
+ # Values are cast using their +to_i+ method, except for blank strings, which
15
+ # are cast to +nil+. If a +to_i+ method is not defined or raises an error,
16
+ # the value will be cast to +nil+.
17
+ #
14
18
# person = Person.new
15
- # person.age = "18"
16
19
#
20
+ # person.age = "18"
17
21
# person.age # => 18
18
22
#
19
- # Values are cast using their +to_i+ method, if it exists. If it does not
20
- # exist, or if it raises an error, the value will be cast to + nil+:
23
+ # person.age = ""
24
+ # person.age # => nil
21
25
#
22
26
# person.age = :not_an_integer
23
27
# person.age # => nil (because Symbol does not define #to_i)
You can’t perform that action at this time.
0 commit comments