Skip to content
Browse files

Extract Serialization

  • Loading branch information...
1 parent adcb700 commit 41dc315d6c62ba70f42585d856a4a9849d9d06df @cgriego committed Aug 23, 2012
View
1 CHANGELOG.md
@@ -1,5 +1,6 @@
# ActiveAttr 0.7.0 (Unreleased) #
+* Added Serialization
* Changed Typecasting::DateTypecaster to not raise on invalid Strings
# ActiveAttr 0.6.0 (June 27, 2012) #
View
9 README.md
@@ -193,6 +193,15 @@ including the MassAssignmentSecurity model.
include ActiveModel::ForbiddenAttributesProtection
end
+### Serialization ###
+
+The Serialization module is a shortcut for incorporating ActiveModel's
+serialization functionality into your model with one include.
+
+ class Person
+ include ActiveAttr::Model
+ end
+
### Model ###
The Model module is a shortcut for incorporating the most common model
View
1 lib/active_attr.rb
@@ -21,6 +21,7 @@ module ActiveAttr
autoload :MassAssignmentSecurity
autoload :Model
autoload :QueryAttributes
+ autoload :Serialization
autoload :TypecastedAttributes
autoload :Typecasting
autoload :UnknownAttributeError
View
11 lib/active_attr/model.rb
@@ -4,8 +4,8 @@
require "active_attr/logger"
require "active_attr/mass_assignment_security"
require "active_attr/query_attributes"
+require "active_attr/serialization"
require "active_attr/typecasted_attributes"
-require "active_model"
require "active_support/concern"
module ActiveAttr
@@ -29,13 +29,6 @@ module Model
include AttributeDefaults
include QueryAttributes
include TypecastedAttributes
-
- if defined? ActiveModel::Serializable
- include ActiveModel::Serializable::JSON
- include ActiveModel::Serializable::XML
- else
- include ActiveModel::Serializers::JSON
- include ActiveModel::Serializers::Xml
- end
+ include Serialization
end
end
View
31 lib/active_attr/serialization.rb
@@ -0,0 +1,31 @@
+require "active_attr/attributes"
+require "active_attr/mass_assignment"
+require "active_model"
+require "active_support/concern"
+
+module ActiveAttr
+ # Serialization is a shortcut for incorporating ActiveModel's
+ # serialization functionality into your model with one include
+ #
+ # See the included modules to learn more.
+ #
+ # @example Usage
+ # class Person
+ # include ActiveAttr::Serialization
+ # end
+ #
+ # @since 0.7.0
+ module Serialization
+ extend ActiveSupport::Concern
+ include Attributes
+ include MassAssignment
+
+ if defined? ActiveModel::Serializable
+ include ActiveModel::Serializable::JSON
+ include ActiveModel::Serializable::XML
+ else
+ include ActiveModel::Serializers::JSON
+ include ActiveModel::Serializers::Xml
+ end
+ end
+end
View
31 spec/functional/active_attr/serialization_spec.rb
@@ -0,0 +1,31 @@
+require "spec_helper"
+require "active_attr/serialization"
+require "active_support/core_ext/hash/conversions"
+require "active_support/json/decoding"
+
+module ActiveAttr
+ describe Serialization do
+ let :model_class do
+ Class.new do
+ include Serialization
+ attribute :first_name
+
+ def self.name
+ "Person"
+ end
+ end
+ end
+
+ subject { model_class.new }
+
+ it "serializes to/from JSON" do
+ subject.first_name = "Chris"
+ model_class.new.from_json(subject.to_json).first_name.should == "Chris"
+ end
+
+ it "serializes to/from XML" do
+ subject.first_name = "Chris"
+ model_class.new.from_xml(subject.to_xml).first_name.should == "Chris"
+ end
+ end
+end

0 comments on commit 41dc315

Please sign in to comment.
Something went wrong with that request. Please try again.