mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Memoize attributes
This commit is contained in:
parent
036604b149
commit
cd736e0adf
@ -40,8 +40,9 @@ module ActiveModel
|
|||||||
|
|
||||||
# Return the +attributes+ of +object+ as presented
|
# Return the +attributes+ of +object+ as presented
|
||||||
# by the serializer.
|
# by the serializer.
|
||||||
def attributes(requested_attrs = nil)
|
def attributes(requested_attrs = nil, reload = false)
|
||||||
self.class._attribute_mappings.each_with_object({}) do |(key, attribute_mapping), hash|
|
@attributes = nil if reload
|
||||||
|
@attributes ||= self.class._attribute_mappings.each_with_object({}) do |(key, attribute_mapping), hash|
|
||||||
next unless requested_attrs.nil? || requested_attrs.include?(key)
|
next unless requested_attrs.nil? || requested_attrs.include?(key)
|
||||||
hash[key] = attribute_mapping.call(self)
|
hash[key] = attribute_mapping.call(self)
|
||||||
end
|
end
|
||||||
@ -65,7 +66,6 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# TODO: remove the dynamic method definition
|
|
||||||
# @example
|
# @example
|
||||||
# class AdminAuthorSerializer < ActiveModel::Serializer
|
# class AdminAuthorSerializer < ActiveModel::Serializer
|
||||||
# attributes :id, :recent_edits
|
# attributes :id, :recent_edits
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user