active_model_serializers/lib/active_model_serializers/adapter/attributes.rb
Benjamin Fleischer b7e2bc06ed Memoize resource relationships
bin/bench_regression
  "version": "0.10.0.rc5",
  "rails_version": "4.2.6",
  "benchmark_run[environment]": "2.2.2p95",

perf/only_calc_associations_once
"commit_hash": "1e7c428",
caching on: caching serializers: gc off 741.7702402782281/ips; 1355 objects
caching on: non-caching serializers: gc off 712.3752615532874/ips; 1257 objects
caching off: caching serializers: gc off 706.0789199312495/ips; 1355 objects
caching off: non-caching serializers: gc off 751.5310710635379/ips; 1257 objects

master
"commit_hash": "1033b711c7d7c231bb5b832e7dfe7f99389f22c4"
caching on: caching serializers: gc off 567.7959835633892/ips; 1803 objects
caching on: non-caching serializers: gc off 776.4929551133658/ips; 1257 objects
caching off: caching serializers: gc off 538.046851190591/ips; 1803 objects
caching off: non-caching serializers: gc off 738.5596630209004/ips; 1257 objects
2016-04-18 11:11:13 -05:00

75 lines
2.3 KiB
Ruby

module ActiveModelSerializers
module Adapter
class Attributes < Base
def initialize(serializer, options = {})
super
@include_tree = ActiveModel::Serializer::IncludeTree.from_include_args(options[:include] || '*')
@cached_attributes = options[:cache_attributes] || {}
end
def serializable_hash(options = nil)
options = serialization_options(options)
if serializer.respond_to?(:each)
serializable_hash_for_collection(options)
else
serializable_hash_for_single_resource(options)
end
end
private
def serializable_hash_for_collection(options)
cache_attributes
serializer.map { |s| Attributes.new(s, instance_options).serializable_hash(options) }
end
def serializable_hash_for_single_resource(options)
resource = resource_object_for(options)
relationships = resource_relationships(options)
resource.merge(relationships)
end
def resource_relationships(options)
relationships = {}
serializer.associations(@include_tree).each do |association|
relationships[association.key] ||= relationship_value_for(association, options)
end
relationships
end
def relationship_value_for(association, options)
return association.options[:virtual_value] if association.options[:virtual_value]
return unless association.serializer && association.serializer.object
opts = instance_options.merge(include: @include_tree[association.key])
Attributes.new(association.serializer, opts).serializable_hash(options)
end
# no-op: Attributes adapter does not include meta data, because it does not support root.
def include_meta(json)
json
end
# Set @cached_attributes
def cache_attributes
return if @cached_attributes.present?
@cached_attributes = ActiveModel::Serializer.cache_read_multi(serializer, self, @include_tree)
end
def resource_object_for(options)
if serializer.class.cache_enabled?
@cached_attributes.fetch(serializer.cache_key(self)) do
serializer.cached_fields(options[:fields], self)
end
else
serializer.cached_fields(options[:fields], self)
end
end
end
end
end