Skip to content

Commit

Permalink
Update ActiveModel::Attributes examples in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
uxxman committed Feb 10, 2022
1 parent 0f36425 commit 3324cbd
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 11 deletions.
11 changes: 8 additions & 3 deletions activemodel/lib/active_model/attributes.rb
Expand Up @@ -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
Expand All @@ -50,7 +51,8 @@ module ClassMethods
# attribute :active, :boolean, default: true
# end
#
# person = Person.new(name: "Volmer")
# person = Person.new
# person.name = "Volmer"
#
# person.name # => "Volmer"
# person.active # => true
Expand Down Expand Up @@ -132,7 +134,10 @@ def initialize_dup(other) # :nodoc:
# 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
Expand Down
4 changes: 3 additions & 1 deletion activemodel/lib/active_model/type/big_integer.rb
Expand Up @@ -13,7 +13,9 @@ module Type
# 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
Expand Down
3 changes: 2 additions & 1 deletion activemodel/lib/active_model/type/date.rb
Expand Up @@ -11,7 +11,8 @@ module Type
# 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
Expand Down
3 changes: 2 additions & 1 deletion activemodel/lib/active_model/type/date_time.rb
Expand Up @@ -11,7 +11,8 @@ module Type
# 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
Expand Down
4 changes: 3 additions & 1 deletion activemodel/lib/active_model/type/decimal.rb
Expand Up @@ -13,7 +13,9 @@ module Type
# 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
Expand Down
4 changes: 3 additions & 1 deletion activemodel/lib/active_model/type/float.rb
Expand Up @@ -13,7 +13,9 @@ module Type
# 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+
Expand Down
5 changes: 4 additions & 1 deletion activemodel/lib/active_model/type/immutable_string.rb
Expand Up @@ -13,6 +13,7 @@ module Type
#
# person = Person.new
# person.name = 1
#
# person.name # => "1"
# person.name.frozen? # => true
#
Expand All @@ -27,7 +28,9 @@ module Type
# 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)
Expand Down
4 changes: 3 additions & 1 deletion activemodel/lib/active_model/type/integer.rb
Expand Up @@ -11,7 +11,9 @@ module Type
# 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
Expand Down
3 changes: 2 additions & 1 deletion activemodel/lib/active_model/type/time.rb
Expand Up @@ -11,7 +11,8 @@ module Type
# 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
Expand Down

0 comments on commit 3324cbd

Please sign in to comment.