aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib
diff options
context:
space:
mode:
authorFrancesco Rodriguez <lrodriguezsanc@gmail.com>2012-07-30 21:19:35 -0500
committerFrancesco Rodriguez <lrodriguezsanc@gmail.com>2012-07-30 21:34:03 -0500
commit4ff5a5df4d08fad1ed26164a057be152e40bf8d9 (patch)
tree9dd2e88ecdb53bbc508e2a970e240b77fbefe558 /activemodel/lib
parentba94c6b76d443a5bea55b144461910e690016452 (diff)
downloadrails-4ff5a5df4d08fad1ed26164a057be152e40bf8d9.tar.gz
rails-4ff5a5df4d08fad1ed26164a057be152e40bf8d9.tar.bz2
rails-4ff5a5df4d08fad1ed26164a057be152e40bf8d9.zip
add documentation to ActiveModel #from_json method [ci skip]
Diffstat (limited to 'activemodel/lib')
-rw-r--r--activemodel/lib/active_model/serializers/json.rb36
1 files changed, 34 insertions, 2 deletions
diff --git a/activemodel/lib/active_model/serializers/json.rb b/activemodel/lib/active_model/serializers/json.rb
index e4c7553cb8..ee888d3ee1 100644
--- a/activemodel/lib/active_model/serializers/json.rb
+++ b/activemodel/lib/active_model/serializers/json.rb
@@ -19,8 +19,8 @@ module ActiveModel
# passed through +options+.
#
# The option <tt>include_root_in_json</tt> controls the top-level behavior
- # of +as_json+. If true +as_json+ will emit a single root node named after
- # the object's type. The default value for <tt>include_root_in_json</tt>
+ # of +as_json+. If +true+, +as_json+ will emit a single root node named
+ # after the object's type. The default value for <tt>include_root_in_json</tt>
# option is +false+.
#
# user = User.find(1)
@@ -101,6 +101,38 @@ module ActiveModel
end
end
+ # Sets the model +attributes+ from a JSON string. Returns +self+.
+ #
+ # class Person
+ # include ActiveModel::Serializers::JSON
+ #
+ # attr_accessor :name, :age, :awesome
+ #
+ # def attributes=(hash)
+ # hash.each do |key, value|
+ # instance_variable_set("@#{key}", value)
+ # end
+ # end
+ #
+ # def attributes
+ # instance_values
+ # end
+ # end
+ #
+ # person = Person.new
+ # person.from_json("{\"name\":\"bob\",\"age\":22,\"awesome\":true}")
+ # person.name # => "bob"
+ # person.age # => 22
+ # person.awesome # => true
+ #
+ # The default value for +include_root+ is +false+. You can change it to
+ # +true+ if the given JSON string includes a single root node.
+ #
+ # person = Person.new
+ # person.from_json("{\"person\":{\"name\":\"bob\",\"age\":22,\"awesome\":true}}", true)
+ # person.name # => "bob"
+ # person.age # => 22
+ # person.awesome # => true
def from_json(json, include_root=include_root_in_json)
hash = ActiveSupport::JSON.decode(json)
hash = hash.values.first if include_root