lib/restpack_serializer/serializable.rb in restpack_serializer-0.6.5 vs lib/restpack_serializer/serializable.rb in restpack_serializer-0.6.6
- old
+ new
@@ -36,19 +36,17 @@
if model.kind_of?(Array)
return model.map { |item| as_json(item, context) }
end
@model, @context = model, context
- user_defined_methods = self.class.user_defined_methods || []
data = {}
if self.class.serializable_attributes.present?
self.class.serializable_attributes.each do |key, attribute|
method_name = attribute[:include_method_name]
name = attribute[:name]
-
- if user_defined_methods.include?(method_name)
+ if self.class.memoized_has_user_defined_method?(method_name)
data[key] = self.send(name) if self.send(method_name)
else
#the default implementation of `include_abc?`
if @context[method_name].nil? || @context[method_name]
data[key] = self.send(name)
@@ -100,9 +98,27 @@
def method_added(name)
#we track used defined methods so that we can make quick decisions at runtime
@user_defined_methods ||= []
if @track_defined_methods
@user_defined_methods << name
+ end
+ end
+
+ def has_user_defined_method?(method_name)
+ user_defined_methods = self.user_defined_methods || []
+ return true if user_defined_methods.include?(method_name)
+ return self.superclass.try(:has_user_defined_method?, method_name)
+ end
+
+ def memoized_has_user_defined_method?(method_name)
+ @memoized_user_defined_methods ||= {}
+
+ if @memoized_user_defined_methods.has_key? method_name
+ return @memoized_user_defined_methods[method_name]
+ else
+ has_method = has_user_defined_method?(method_name)
+ @memoized_user_defined_methods[method_name] = has_method
+ return has_method
end
end
def array_as_json(models, context = {})
new.as_json(models, context)