From 9a7702a1df19b8d52d1500b821207efd8c099a54 Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Fri, 6 Jul 2012 00:09:08 -0500 Subject: update ActiveModel::Serialization documentation [ci skip] --- activemodel/lib/active_model/serialization.rb | 50 +++++++++++++++++++-------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/activemodel/lib/active_model/serialization.rb b/activemodel/lib/active_model/serialization.rb index 6d8fd21814..8a63014ffb 100644 --- a/activemodel/lib/active_model/serialization.rb +++ b/activemodel/lib/active_model/serialization.rb @@ -25,17 +25,16 @@ module ActiveModel # person.name = "Bob" # person.serializable_hash # => {"name"=>"Bob"} # - # You need to declare an attributes hash which contains the attributes - # you want to serialize. Attributes must be strings, not symbols. - # When called, serializable hash will use - # instance methods that match the name of the attributes hash's keys. - # In order to override this behavior, take a look at the private - # method +read_attribute_for_serialization+. + # You need to declare an attributes hash which contains the attributes you + # want to serialize. Attributes must be strings, not symbols. When called, + # serializable hash will use instance methods that match the name of the + # attributes hash's keys. In order to override this behavior, take a look at + # the private method +read_attribute_for_serialization+. # # Most of the time though, you will want to include the JSON or XML # serializations. Both of these modules automatically include the - # ActiveModel::Serialization module, so there is no need to explicitly - # include it. + # ActiveModel::Serialization module, so there is no need to + # explicitly include it. # # A minimal implementation including XML and JSON would be: # @@ -64,13 +63,37 @@ module ActiveModel # person.to_json # => "{\"name\":\"Bob\"}" # person.to_xml # => "\n:only, :except, :methods and :include. - # The following are all valid examples: + # Valid options are :only, :except, :methods and + # :include. The following are all valid examples: # - # person.serializable_hash(:only => 'name') - # person.serializable_hash(:include => :address) - # person.serializable_hash(:include => { :address => { :only => 'city' }}) + # person.serializable_hash(only: 'name') + # person.serializable_hash(include: :address) + # person.serializable_hash(include: { address: { only: 'city' }}) module Serialization + # Returns a serialized hash of your object. + # + # class Person + # include ActiveModel::Serialization + # + # attr_accessor :name, :age + # + # def attributes + # {'name' => nil, 'age' => nil} + # end + # + # def capitalized_name + # name.capitalize + # end + # end + # + # person = Person.new + # person.name = 'bob' + # person.age = 22 + # person.serializable_hash # => {"name"=>"bob", "age"=>22} + # person.serializable_hash(only: :name) # => {"name"=>"bob"} + # person.serializable_hash(except: :name) # => {"age"=>22} + # person.serializable_hash(methods: :capitalized_name) + # # => {"name"=>"bob", "age"=>22, "capitalized_name"=>"Bob"} def serializable_hash(options = nil) options ||= {} @@ -115,7 +138,6 @@ module ActiveModel # @data[key] # end # end - # alias :read_attribute_for_serialization :send # Add associations specified via the :include option. -- cgit v1.2.3