mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16: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
|
||||
# by the serializer.
|
||||
def attributes(requested_attrs = nil)
|
||||
self.class._attribute_mappings.each_with_object({}) do |(key, attribute_mapping), hash|
|
||||
def attributes(requested_attrs = nil, reload = false)
|
||||
@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)
|
||||
hash[key] = attribute_mapping.call(self)
|
||||
end
|
||||
@ -65,7 +66,6 @@ module ActiveModel
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: remove the dynamic method definition
|
||||
# @example
|
||||
# class AdminAuthorSerializer < ActiveModel::Serializer
|
||||
# attributes :id, :recent_edits
|
||||
|
||||
Loading…
Reference in New Issue
Block a user