Merge branch 'refactor'

This commit is contained in:
Santiago Pastorino 2013-05-21 17:04:16 -07:00
commit 86d993ea38
8 changed files with 268 additions and 314 deletions

View File

@ -1,7 +1,7 @@
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
$:.unshift File.expand_path("../lib", __FILE__) $:.unshift File.expand_path("../lib", __FILE__)
require "active_model/serializers/version" require "active_model/serializer/version"
Gem::Specification.new do |gem| Gem::Specification.new do |gem|
gem.authors = ["José Valim", "Yehuda Katz"] gem.authors = ["José Valim", "Yehuda Katz"]

View File

@ -1,3 +1,5 @@
require 'active_model/serializable'
require 'active_model/serializer/caching'
require "active_support/core_ext/class/attribute" require "active_support/core_ext/class/attribute"
require 'active_support/dependencies' require 'active_support/dependencies'
require 'active_support/descendants_tracker' require 'active_support/descendants_tracker'
@ -15,6 +17,9 @@ module ActiveModel
class ArraySerializer class ArraySerializer
extend ActiveSupport::DescendantsTracker extend ActiveSupport::DescendantsTracker
include ActiveModel::Serializable
include ActiveModel::Serializer::Caching
attr_reader :object, :options attr_reader :object, :options
class_attribute :root class_attribute :root
@ -30,60 +35,25 @@ module ActiveModel
end end
def initialize(object, options={}) def initialize(object, options={})
@object, @options = object, options @object = object
@options = options
end end
def meta_key def serialize_object
@options[:meta_key].try(:to_sym) || :meta
end
def include_meta(hash)
hash[meta_key] = @options[:meta] if @options.has_key?(:meta)
end
def as_json(*args)
@options[:hash] = hash = {}
@options[:unique_values] = {}
if root = @options[:root]
hash.merge!(root => serializable_array)
include_meta hash
hash
else
serializable_array serializable_array
end end
end
def to_json(*args)
if perform_caching?
cache.fetch expand_cache_key([self.class.to_s.underscore, cache_key, 'to-json']) do
super
end
else
super
end
end
def serializable_array def serializable_array
if perform_caching? object.map do |item|
cache.fetch expand_cache_key([self.class.to_s.underscore, cache_key, 'serializable-array']) do if options.has_key? :each_serializer
_serializable_array serializer = options[:each_serializer]
end
else
_serializable_array
end
end
private
def _serializable_array
@object.map do |item|
if @options.has_key? :each_serializer
serializer = @options[:each_serializer]
elsif item.respond_to?(:active_model_serializer) elsif item.respond_to?(:active_model_serializer)
serializer = item.active_model_serializer serializer = item.active_model_serializer
else
serializer = DefaultSerializer
end end
serializable = serializer ? serializer.new(item, @options) : DefaultSerializer.new(item, @options) serializable = serializer.new(item, options)
if serializable.respond_to?(:serializable_hash) if serializable.respond_to?(:serializable_hash)
serializable.serializable_hash serializable.serializable_hash
@ -92,13 +62,5 @@ module ActiveModel
end end
end end
end end
def expand_cache_key(*args)
ActiveSupport::Cache.expand_cache_key(args)
end
def perform_caching?
perform_caching && cache && respond_to?(:cache_key)
end
end end
end end

View File

@ -0,0 +1,49 @@
require 'active_support/core_ext/object/to_json'
module ActiveModel
# Enable classes to Classes including this module to serialize themselves by implementing a serialize method and an options method.
#
# Example:
#
# require 'active_model_serializers'
#
# class MySerializer
# include ActiveModel::Serializable
#
# def initialize
# @options = {}
# end
#
# attr_reader :options
#
# def serialize
# { a: 1 }
# end
# end
#
# puts MySerializer.new.to_json
module Serializable
def as_json(args={})
if root = args[:root] || options[:root]
options[:hash] = hash = {}
options[:unique_values] = {}
hash.merge!(root => serialize)
include_meta hash
hash
else
serialize
end
end
private
def include_meta(hash)
hash[meta_key] = options[:meta] if options.has_key?(:meta)
end
def meta_key
options[:meta_key].try(:to_sym) || :meta
end
end
end

View File

@ -1,3 +1,5 @@
require 'active_model/serializable'
require 'active_model/serializer/caching'
require "active_support/core_ext/class/attribute" require "active_support/core_ext/class/attribute"
require "active_support/core_ext/module/anonymous" require "active_support/core_ext/module/anonymous"
require 'active_support/dependencies' require 'active_support/dependencies'
@ -40,6 +42,9 @@ module ActiveModel
class Serializer class Serializer
extend ActiveSupport::DescendantsTracker extend ActiveSupport::DescendantsTracker
include ActiveModel::Serializable
include ActiveModel::Serializer::Caching
INCLUDE_METHODS = {} INCLUDE_METHODS = {}
INSTRUMENT = { :serialize => :"serialize.serializer", :associations => :"associations.serializer" } INSTRUMENT = { :serialize => :"serialize.serializer", :associations => :"associations.serializer" }
@ -70,7 +75,6 @@ module ActiveModel
class_attribute :perform_caching class_attribute :perform_caching
class << self class << self
# set perform caching like root
def cached(value = true) def cached(value = true)
self.perform_caching = value self.perform_caching = value
end end
@ -125,7 +129,7 @@ module ActiveModel
define_include_method attr define_include_method attr
self._associations[attr] = klass.refine(attr, options) self._associations[attr] = [klass, options]
end end
end end
@ -148,7 +152,7 @@ module ActiveModel
# with the association name does not exist, the association name is # with the association name does not exist, the association name is
# dispatched to the serialized object. # dispatched to the serialized object.
def has_many(*attrs) def has_many(*attrs)
associate(Associations::HasMany, attrs) associate(Association::HasMany, attrs)
end end
# Defines an association in the object should be rendered. # Defines an association in the object should be rendered.
@ -158,7 +162,7 @@ module ActiveModel
# with the association name does not exist, the association name is # with the association name does not exist, the association name is
# dispatched to the serialized object. # dispatched to the serialized object.
def has_one(*attrs) def has_one(*attrs)
associate(Associations::HasOne, attrs) associate(Association::HasOne, attrs)
end end
# Return a schema hash for the current serializer. This information # Return a schema hash for the current serializer. This information
@ -213,8 +217,8 @@ module ActiveModel
end end
associations = {} associations = {}
_associations.each do |attr, association_class| _associations.each do |attr, (association_class, options)|
association = association_class.new(attr, self) association = association_class.new(attr, options)
if model_association = klass.reflect_on_association(association.name) if model_association = klass.reflect_on_association(association.name)
# Real association. # Real association.
@ -316,49 +320,23 @@ module ActiveModel
@options[:url_options] || {} @options[:url_options] || {}
end end
def meta_key
@options[:meta_key].try(:to_sym) || :meta
end
def include_meta(hash)
hash[meta_key] = @options[:meta] if @options.has_key?(:meta)
end
def to_json(*args)
if perform_caching?
cache.fetch expand_cache_key([self.class.to_s.underscore, cache_key, 'to-json']) do
super
end
else
super
end
end
# Returns a json representation of the serializable # Returns a json representation of the serializable
# object including the root. # object including the root.
def as_json(options={}) def as_json(args={})
if root = options.fetch(:root, @options.fetch(:root, root_name)) super(root: args.fetch(:root, options.fetch(:root, root_name)))
@options[:hash] = hash = {}
@options[:unique_values] = {}
hash.merge!(root => serializable_hash)
include_meta hash
hash
else
serializable_hash
end end
def serialize_object
serializable_hash
end end
# Returns a hash representation of the serializable # Returns a hash representation of the serializable
# object without the root. # object without the root.
def serializable_hash def serializable_hash
if perform_caching? return nil if @object.nil?
cache.fetch expand_cache_key([self.class.to_s.underscore, cache_key, 'serializable-hash']) do @node = attributes
_serializable_hash include_associations! if _embed
end @node
else
_serializable_hash
end
end end
def include_associations! def include_associations!
@ -374,23 +352,8 @@ module ActiveModel
end end
def include!(name, options={}) def include!(name, options={})
# Make sure that if a special options[:hash] was passed in, we generate
# a new unique values hash and don't clobber the original. If the hash
# passed in is the same as the current options hash, use the current
# unique values.
#
# TODO: Should passing in a Hash even be public API here?
unique_values =
if hash = options[:hash]
if @options[:hash] == hash
@options[:unique_values] ||= {}
else
{}
end
else
hash = @options[:hash] hash = @options[:hash]
@options[:unique_values] ||= {} unique_values = @options[:unique_values] ||= {}
end
node = options[:node] ||= @node node = options[:node] ||= @node
value = options[:value] value = options[:value]
@ -403,19 +366,28 @@ module ActiveModel
end end
end end
klass, klass_options = _associations[name]
association_class = association_class =
if klass = _associations[name] if klass
options = klass_options.merge options
klass klass
elsif value.respond_to?(:to_ary) elsif value.respond_to?(:to_ary)
Associations::HasMany Association::HasMany
else else
Associations::HasOne Association::HasOne
end end
association = association_class.new(name, self, options) options = default_embed_options.merge!(options)
options[:value] ||= send(name)
association = association_class.new(name, options, self.options)
if association.embed_ids? if association.embed_ids?
node[association.key] = association.serialize_ids node[association.key] =
if options[:embed_key] || self.respond_to?(name) || !self.object.respond_to?(association.id_key)
association.serialize_ids
else
self.object.read_attribute_for_serialization(association.id_key)
end
if association.embed_in_root? && hash.nil? if association.embed_in_root? && hash.nil?
raise IncludeError.new(self.class, association.name) raise IncludeError.new(self.class, association.name)
@ -473,27 +445,21 @@ module ActiveModel
alias :read_attribute_for_serialization :send alias :read_attribute_for_serialization :send
def _serializable_hash
return nil if @object.nil?
@node = attributes
include_associations! if _embed
@node
end
def perform_caching?
perform_caching && cache && respond_to?(:cache_key)
end
def expand_cache_key(*args)
ActiveSupport::Cache.expand_cache_key(args)
end
# Use ActiveSupport::Notifications to send events to external systems. # Use ActiveSupport::Notifications to send events to external systems.
# The event name is: name.class_name.serializer # The event name is: name.class_name.serializer
def instrument(name, payload = {}, &block) def instrument(name, payload = {}, &block)
event_name = INSTRUMENT[name] event_name = INSTRUMENT[name]
ActiveSupport::Notifications.instrument(event_name, payload, &block) ActiveSupport::Notifications.instrument(event_name, payload, &block)
end end
private
def default_embed_options
{
:embed => _embed,
:include => _root_embed
}
end
end end
# DefaultSerializer # DefaultSerializer

View File

@ -1,233 +1,173 @@
module ActiveModel module ActiveModel
class Serializer class Serializer
module Associations #:nodoc: class Association #:nodoc:
class Config #:nodoc: # name: The name of the association.
class_attribute :options #
# options: A hash. These keys are accepted:
def self.refine(name, class_options) #
current_class = self # value: The object we're associating with.
#
Class.new(self) do # serializer: The class used to serialize the association.
singleton_class.class_eval do #
define_method(:to_s) do # embed: Define how associations should be embedded.
"(subclass of #{current_class.name})" # - :objects # Embed associations as full objects.
end # - :ids # Embed only the association ids.
# - :ids, :include => true # Embed the association ids and include objects in the root.
alias inspect to_s #
end # include: Used in conjunction with embed :ids. Includes the objects in the root.
#
self.options = class_options # root: Used in conjunction with include: true. Defines the key used to embed the objects.
#
# cache the root so we can reuse it without falling back on a per-instance basis # key: Key name used to store the ids in.
begin #
self.options[:root] ||= self.new(name, nil).root # embed_key: Method used to fetch ids. Defaults to :id.
rescue #
# this could fail if it needs a valid source, for example a polymorphic association # polymorphic: Is the association is polymorphic?. Values: true or false.
end def initialize(name, options={}, serializer_options={})
end
end
self.options = {}
def initialize(name, source, options={})
@name = name @name = name
@source = source @object = options[:value]
embed = options[:embed]
@embed_ids = embed == :id || embed == :ids
@embed_objects = embed == :object || embed == :objects
@embed_key = options[:embed_key] || :id
@embed_in_root = options[:include]
serializer = options[:serializer]
@serializer = serializer.is_a?(String) ? serializer.constantize : serializer
@options = options @options = options
@serializer_options = serializer_options
end end
def option(key, default=nil) attr_reader :object, :root, :name, :embed_ids, :embed_objects, :embed_in_root
if @options.key?(key) alias embeddable? object
@options[key] alias embed_objects? embed_objects
elsif self.class.options.key?(key) alias embed_ids? embed_ids
self.class.options[key] alias use_id_key? embed_ids?
else alias embed_in_root? embed_in_root
default
end
end
def target_serializer
serializer = option(:serializer)
serializer.is_a?(String) ? serializer.constantize : serializer
end
def source_serializer
@source
end
def key def key
option(:key) || @name if key = options[:key]
key
elsif use_id_key?
id_key
else
name
end
end end
def root private
option(:root) || @name
end
def name attr_reader :embed_key, :serializer, :options, :serializer_options
option(:name) || @name
end
def associated_object
option(:value) || source_serializer.send(name)
end
def embed_ids?
[:id, :ids].include? option(:embed, source_serializer._embed)
end
def embed_objects?
[:object, :objects].include? option(:embed, source_serializer._embed)
end
def embed_in_root?
option(:include, source_serializer._root_embed)
end
def embeddable?
!associated_object.nil?
end
protected
def find_serializable(object) def find_serializable(object)
if target_serializer if serializer
target_serializer.new(object, source_serializer.options) serializer.new(object, serializer_options)
elsif object.respond_to?(:active_model_serializer) && (ams = object.active_model_serializer) elsif object.respond_to?(:active_model_serializer) && (ams = object.active_model_serializer)
ams.new(object, source_serializer.options) ams.new(object, serializer_options)
else else
object object
end end
end end
class HasMany < Association #:nodoc:
def root
options[:root] || name
end end
class HasMany < Config #:nodoc: def id_key
def key "#{name.to_s.singularize}_ids".to_sym
if key = option(:key)
key
elsif embed_ids?
"#{@name.to_s.singularize}_ids".to_sym
else
@name
end
end
def embed_key
if key = option(:embed_key)
key
else
:id
end
end
def serialize
associated_object.map do |item|
find_serializable(item).serializable_hash
end
end end
def serializables def serializables
associated_object.map do |item| object.map do |item|
find_serializable(item) find_serializable(item)
end end
end end
def serialize
object.map do |item|
find_serializable(item).serializable_hash
end
end
def serialize_ids def serialize_ids
ids_key = "#{@name.to_s.singularize}_ids".to_sym object.map do |item|
if !option(:embed_key) && !source_serializer.respond_to?(@name.to_s) && source_serializer.object.respond_to?(ids_key)
source_serializer.object.read_attribute_for_serialization(ids_key)
else
associated_object.map do |item|
item.read_attribute_for_serialization(embed_key) item.read_attribute_for_serialization(embed_key)
end end
end end
end end
end
class HasOne < Config #:nodoc: class HasOne < Association #:nodoc:
def embeddable? def initialize(name, options={}, serializer_options={})
if polymorphic? && associated_object.nil? super
false @polymorphic = options[:polymorphic]
else
true
end
end
def polymorphic?
option :polymorphic
end end
def root def root
if root = option(:root) if root = options[:root]
root root
elsif polymorphic? elsif polymorphic?
associated_object.class.to_s.pluralize.demodulize.underscore.to_sym object.class.to_s.pluralize.demodulize.underscore.to_sym
else else
@name.to_s.pluralize.to_sym name.to_s.pluralize.to_sym
end end
end end
def key def id_key
if key = option(:key) "#{name}_id".to_sym
key
elsif embed_ids? && !polymorphic?
"#{@name}_id".to_sym
else
@name
end
end end
def embed_key def embeddable?
if key = option(:embed_key) super || !polymorphic?
key
else
:id
end
end
def polymorphic_key
associated_object.class.to_s.demodulize.underscore.to_sym
end
def serialize
object = associated_object
if object && polymorphic?
{
:type => polymorphic_key,
polymorphic_key => find_serializable(object).serializable_hash
}
elsif object
find_serializable(object).serializable_hash
end
end end
def serializables def serializables
object = associated_object
value = object && find_serializable(object) value = object && find_serializable(object)
value ? [value] : [] value ? [value] : []
end end
def serialize_ids def serialize
id_key = "#{@name}_id".to_sym if object
if polymorphic? if polymorphic?
if associated_object
{ {
:type => polymorphic_key, :type => polymorphic_key,
:id => associated_object.read_attribute_for_serialization(embed_key) polymorphic_key => find_serializable(object).serializable_hash
} }
else else
nil find_serializable(object).serializable_hash
end end
elsif !option(:embed_key) && !source_serializer.respond_to?(@name.to_s) && source_serializer.object.respond_to?(id_key) end
source_serializer.object.read_attribute_for_serialization(id_key) end
elsif associated_object
associated_object.read_attribute_for_serialization(embed_key) def serialize_ids
if object
id = object.read_attribute_for_serialization(embed_key)
if polymorphic?
{
:type => polymorphic_key,
:id => id
}
else else
nil id
end end
end end
end end
private
attr_reader :polymorphic
alias polymorphic? polymorphic
def use_id_key?
embed_ids? && !polymorphic?
end
def polymorphic_key
object.class.to_s.demodulize.underscore.to_sym
end
end
end end
end end
end end

View File

@ -0,0 +1,37 @@
module ActiveModel
class Serializer
module Caching
def to_json(*args)
if caching_enabled?
key = expand_cache_key([self.class.to_s.underscore, cache_key, 'to-json'])
cache.fetch key do
super
end
else
super
end
end
def serialize(*args)
if caching_enabled?
key = expand_cache_key([self.class.to_s.underscore, cache_key, 'serialize'])
cache.fetch key do
serialize_object
end
else
serialize_object
end
end
private
def caching_enabled?
perform_caching && cache && respond_to?(:cache_key)
end
def expand_cache_key(*args)
ActiveSupport::Cache.expand_cache_key(args)
end
end
end
end

View File

@ -68,7 +68,7 @@ class CachingTest < ActiveModel::TestCase
instance.to_json instance.to_json
assert_equal(instance.serializable_hash, serializer.cache.read('serializer/Adam/serializable-hash')) assert_equal(instance.serializable_hash, serializer.cache.read('serializer/Adam/serialize'))
assert_equal(instance.to_json, serializer.cache.read('serializer/Adam/to-json')) assert_equal(instance.to_json, serializer.cache.read('serializer/Adam/to-json'))
end end
@ -90,7 +90,7 @@ class CachingTest < ActiveModel::TestCase
instance.to_json instance.to_json
assert_equal instance.serializable_array, serializer.cache.read('array_serializer/cache-key/serializable-array') assert_equal instance.serializable_array, serializer.cache.read('array_serializer/cache-key/serialize')
assert_equal instance.to_json, serializer.cache.read('array_serializer/cache-key/to-json') assert_equal instance.to_json, serializer.cache.read('array_serializer/cache-key/to-json')
end end
end end