mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #44394 from uxxman/activemodel_attributes_docs
Update ActiveModel::Attributes examples in docs [ci-skip]
This commit is contained in:
commit
aebde93508
9 changed files with 30 additions and 11 deletions
|
@ -23,7 +23,8 @@ module ActiveModel
|
|||
# attribute :active, :boolean, default: true
|
||||
# end
|
||||
#
|
||||
# person = Person.new(name: "Volmer")
|
||||
# person = Person.new
|
||||
# person.name = "Volmer"
|
||||
#
|
||||
# person.name # => "Volmer"
|
||||
# person.active # => true
|
||||
|
@ -50,7 +51,8 @@ module ActiveModel
|
|||
# attribute :active, :boolean, default: true
|
||||
# end
|
||||
#
|
||||
# person = Person.new(name: "Volmer")
|
||||
# person = Person.new
|
||||
# person.name = "Volmer"
|
||||
#
|
||||
# person.name # => "Volmer"
|
||||
# person.active # => true
|
||||
|
@ -132,7 +134,10 @@ module ActiveModel
|
|||
# attribute :age, :integer
|
||||
# end
|
||||
#
|
||||
# person = Person.new(name: "Francesco", age: 22)
|
||||
# person = Person.new
|
||||
# person.name = "Francesco"
|
||||
# person.age = 22
|
||||
#
|
||||
# person.attributes # => { "name" => "Francesco", "age" => 22}
|
||||
def attributes
|
||||
@attributes.to_hash
|
||||
|
|
|
@ -13,7 +13,9 @@ module ActiveModel
|
|||
# attribute :id, :big_integer
|
||||
# end
|
||||
#
|
||||
# person = Person.new(id: "18_000_000_000")
|
||||
# person = Person.new
|
||||
# person.id = "18_000_000_000"
|
||||
#
|
||||
# person.id # => 18000000000
|
||||
#
|
||||
# All casting and serialization are performed in the same way as the
|
||||
|
|
|
@ -11,7 +11,8 @@ module ActiveModel
|
|||
# attribute :birthday, :date
|
||||
# end
|
||||
#
|
||||
# person = Person.new(birthday: "1989-07-13")
|
||||
# person = Person.new
|
||||
# person.birthday = "1989-07-13"
|
||||
#
|
||||
# person.birthday.class # => Date
|
||||
# person.birthday.year # => 1989
|
||||
|
|
|
@ -11,7 +11,8 @@ module ActiveModel
|
|||
# attribute :start, :datetime
|
||||
# end
|
||||
#
|
||||
# event = Event.new(start: "Wed, 04 Sep 2013 03:00:00 EAT")
|
||||
# event = Event.new
|
||||
# event.start = "Wed, 04 Sep 2013 03:00:00 EAT"
|
||||
#
|
||||
# event.start.class # => Time
|
||||
# event.start.year # => 2013
|
||||
|
|
|
@ -13,7 +13,9 @@ module ActiveModel
|
|||
# attribute :weight, :decimal
|
||||
# end
|
||||
#
|
||||
# bag = BagOfCoffee.new(weight: "0.0001")
|
||||
# bag = BagOfCoffee.new
|
||||
# bag.weight = "0.0001"
|
||||
#
|
||||
# bag.weight # => 0.1e-3
|
||||
#
|
||||
# Numeric instances are converted to BigDecimal instances. Any other objects
|
||||
|
|
|
@ -13,7 +13,9 @@ module ActiveModel
|
|||
# attribute :weight, :float
|
||||
# end
|
||||
#
|
||||
# bag = BagOfCoffee.new(weight: "0.25")
|
||||
# bag = BagOfCoffee.new
|
||||
# bag.weight = "0.25"
|
||||
#
|
||||
# bag.weight # => 0.25
|
||||
#
|
||||
# Values are coerced to their float representation using their +to_f+
|
||||
|
|
|
@ -13,6 +13,7 @@ module ActiveModel
|
|||
#
|
||||
# person = Person.new
|
||||
# person.name = 1
|
||||
#
|
||||
# person.name # => "1"
|
||||
# person.name.frozen? # => true
|
||||
#
|
||||
|
@ -27,7 +28,9 @@ module ActiveModel
|
|||
# attribute :active, :immutable_string, true: "aye", false: "nay"
|
||||
# end
|
||||
#
|
||||
# person = Person.new(active: true)
|
||||
# person = Person.new
|
||||
# person.active = true
|
||||
#
|
||||
# person.active # => "aye"
|
||||
class ImmutableString < Value
|
||||
def initialize(**args)
|
||||
|
|
|
@ -11,7 +11,9 @@ module ActiveModel
|
|||
# attribute :age, :integer
|
||||
# end
|
||||
#
|
||||
# person = Person.new(age: "18")
|
||||
# person = Person.new
|
||||
# person.age = "18"
|
||||
#
|
||||
# person.age # => 18
|
||||
#
|
||||
# Values are cast using their +to_i+ method, if it exists. If it does not
|
||||
|
|
|
@ -11,7 +11,8 @@ module ActiveModel
|
|||
# attribute :start, :time
|
||||
# end
|
||||
#
|
||||
# event = Event.new(start: "2022-02-18T13:15:00-05:00")
|
||||
# event = Event.new
|
||||
# event.start = "2022-02-18T13:15:00-05:00"
|
||||
#
|
||||
# event.start.class # => Time
|
||||
# event.start.year # => 2022
|
||||
|
|
Loading…
Reference in a new issue