diff options
author | Mikel Lindsaar <raasdnil@gmail.com> | 2010-01-17 20:14:14 +1100 |
---|---|---|
committer | Mikel Lindsaar <raasdnil@gmail.com> | 2010-01-17 20:14:14 +1100 |
commit | 19814df40d171b2ed83875a36d7361881ccf2e70 (patch) | |
tree | 08d0d241a0e7aafd1c3fd77db9c5802f22cd4f33 /activemodel/lib/active_model/serialization.rb | |
parent | e6063282f98b9d40da00897727e5ae01ca61b9fc (diff) | |
download | rails-19814df40d171b2ed83875a36d7361881ccf2e70.tar.gz rails-19814df40d171b2ed83875a36d7361881ccf2e70.tar.bz2 rails-19814df40d171b2ed83875a36d7361881ccf2e70.zip |
Adding documentation for ActiveModel::Serialization
Diffstat (limited to 'activemodel/lib/active_model/serialization.rb')
-rw-r--r-- | activemodel/lib/active_model/serialization.rb | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/activemodel/lib/active_model/serialization.rb b/activemodel/lib/active_model/serialization.rb index 4c0073f687..28f95f0cdc 100644 --- a/activemodel/lib/active_model/serialization.rb +++ b/activemodel/lib/active_model/serialization.rb @@ -2,6 +2,64 @@ require 'active_support/core_ext/hash/except' require 'active_support/core_ext/hash/slice' module ActiveModel + + # Provides a basic serialization to a serializable_hash for your object. + # + # A minimal implementation could be: + # + # class Person + # + # include ActiveModel::Serialization + # + # attr_accessor :name + # + # def attributes + # @attributes ||= {'name' => 'nil'} + # end + # + # end + # + # Which would provide you with: + # + # person = Person.new + # person.serializable_hash # => {"name"=>nil} + # person.name = "Bob" + # person.serializable_hash # => {"name"=>"Bob"} + # + # You need to declare some sort of attributes hash which contains the attributes + # you want to serialize and their current value. + # + # 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. + # + # So a minimal implementation including XML and JSON would be: + # + # class Person + # + # include ActiveModel::Serializers::JSON + # include ActiveModel::Serializers::Xml + # + # attr_accessor :name + # + # def attributes + # @attributes ||= {'name' => 'nil'} + # end + # + # end + # + # Which would provide you with: + # + # person = Person.new + # person.serializable_hash # => {"name"=>nil} + # person.to_json # => "{\"name\":null}" + # person.to_xml # => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<serial-person... + # + # person.name = "Bob" + # person.serializable_hash # => {"name"=>"Bob"} + # person.to_json # => "{\"name\":\"Bob\"}" + # person.to_xml # => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<serial-person... module Serialization def serializable_hash(options = nil) options ||= {} |