2009-08-13 23:27:09 -04:00
|
|
|
require 'active_support/core_ext/hash/except'
|
|
|
|
require 'active_support/core_ext/hash/slice'
|
|
|
|
|
|
|
|
module ActiveModel
|
2010-06-14 05:19:01 -04:00
|
|
|
# == Active Model Serialization
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# Provides a basic serialization to a serializable_hash for your object.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# A minimal implementation could be:
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# class Person
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# include ActiveModel::Serialization
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# attr_accessor :name
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# def attributes
|
|
|
|
# @attributes ||= {'name' => 'nil'}
|
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# Which would provide you with:
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# 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.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
|
|
|
# Most of the time though, you will want to include the JSON or XML
|
|
|
|
# serializations. Both of these modules automatically include the
|
2010-01-31 18:08:20 -05:00
|
|
|
# ActiveModel::Serialization module, so there is no need to explicitly
|
|
|
|
# include it.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# So a minimal implementation including XML and JSON would be:
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# class Person
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# include ActiveModel::Serializers::JSON
|
|
|
|
# include ActiveModel::Serializers::Xml
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# attr_accessor :name
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# def attributes
|
|
|
|
# @attributes ||= {'name' => 'nil'}
|
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# Which would provide you with:
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# person = Person.new
|
|
|
|
# person.serializable_hash # => {"name"=>nil}
|
2010-08-29 10:10:31 -04:00
|
|
|
# person.as_json # => {"name"=>nil}
|
|
|
|
# person.to_json # => "{\"name\":null}"
|
2010-01-31 18:08:20 -05:00
|
|
|
# person.to_xml # => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<serial-person...
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-31 18:08:20 -05:00
|
|
|
# person.name = "Bob"
|
|
|
|
# person.serializable_hash # => {"name"=>"Bob"}
|
2010-08-29 10:10:31 -04:00
|
|
|
# person.as_json # => {"name"=>"Bob"}
|
|
|
|
# person.to_json # => "{\"name\":\"Bob\"}"
|
2010-01-31 18:08:20 -05:00
|
|
|
# person.to_xml # => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<serial-person...
|
2010-08-04 17:02:38 -04:00
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# Valid options are <tt>:only</tt>, <tt>:except</tt> and <tt>:methods</tt> .
|
2009-08-13 23:27:09 -04:00
|
|
|
module Serialization
|
|
|
|
def serializable_hash(options = nil)
|
|
|
|
options ||= {}
|
|
|
|
|
2010-08-29 10:10:31 -04:00
|
|
|
only = Array.wrap(options[:only]).map(&:to_s)
|
|
|
|
except = Array.wrap(options[:except]).map(&:to_s)
|
2009-08-13 23:27:09 -04:00
|
|
|
|
|
|
|
attribute_names = attributes.keys.sort
|
2010-08-29 10:10:31 -04:00
|
|
|
if only.any?
|
|
|
|
attribute_names &= only
|
|
|
|
elsif except.any?
|
|
|
|
attribute_names -= except
|
2009-08-13 23:27:09 -04:00
|
|
|
end
|
|
|
|
|
2010-09-22 11:07:48 -04:00
|
|
|
method_names = Array.wrap(options[:methods]).map { |n| n if respond_to?(n.to_s) }.compact
|
|
|
|
Hash[(attribute_names + method_names).map { |n| [n, send(n)] }]
|
2009-08-13 23:27:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|