From 4ff5a5df4d08fad1ed26164a057be152e40bf8d9 Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Mon, 30 Jul 2012 21:19:35 -0500 Subject: add documentation to ActiveModel #from_json method [ci skip] --- activemodel/lib/active_model/serializers/json.rb | 36 ++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'activemodel/lib/active_model') 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 include_root_in_json 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 include_root_in_json + # of +as_json+. If +true+, +as_json+ will emit a single root node named + # after the object's type. The default value for include_root_in_json # 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 -- cgit v1.2.3