From cd736e0adf133f9b3a50de422f4e61a1c4dbbff1 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Wed, 2 Dec 2015 17:47:24 -0600 Subject: [PATCH] Memoize attributes --- lib/active_model/serializer/attributes.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/active_model/serializer/attributes.rb b/lib/active_model/serializer/attributes.rb index 1c271f01..bea270e7 100644 --- a/lib/active_model/serializer/attributes.rb +++ b/lib/active_model/serializer/attributes.rb @@ -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