mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Add _fast_attributes
This commit is contained in:
parent
9c1a9e18c4
commit
ffb94290e0
@ -100,6 +100,16 @@ module AMS
|
|||||||
_fields << key
|
_fields << key
|
||||||
_attributes[attribute_name] = { key: key }
|
_attributes[attribute_name] = { key: key }
|
||||||
|
|
||||||
|
# protect inheritance chains and open classes
|
||||||
|
# if a serializer inherits from another OR
|
||||||
|
# attributes are added later in a classes lifecycle
|
||||||
|
# poison the cache
|
||||||
|
add_instance_method <<-METHOD, self
|
||||||
|
def _fast_attributes
|
||||||
|
raise NameError
|
||||||
|
end
|
||||||
|
METHOD
|
||||||
|
|
||||||
add_instance_method <<-METHOD, self
|
add_instance_method <<-METHOD, self
|
||||||
def #{attribute_name}
|
def #{attribute_name}
|
||||||
object.#{attribute_name}
|
object.#{attribute_name}
|
||||||
@ -345,11 +355,15 @@ module AMS
|
|||||||
#
|
#
|
||||||
# TODO: Support sparse fieldsets
|
# TODO: Support sparse fieldsets
|
||||||
def attributes
|
def attributes
|
||||||
hash = {}
|
_fast_attributes
|
||||||
|
rescue NameError
|
||||||
|
faster_method = String.new(%(def _fast_attributes\n hash={}\n))
|
||||||
_attributes.each do |attribute_name, config|
|
_attributes.each do |attribute_name, config|
|
||||||
hash[config[:key]] = send(attribute_name)
|
faster_method << %(hash[:"#{config[:key]}"] = #{attribute_name}\n)
|
||||||
end
|
end
|
||||||
hash
|
faster_method << "hash\nend"
|
||||||
|
self.class.add_instance_method faster_method, self.class
|
||||||
|
_fast_attributes
|
||||||
end
|
end
|
||||||
|
|
||||||
# Builds a Hash of specified relations
|
# Builds a Hash of specified relations
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user