mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Improve attribute value computation.
This commit is contained in:
parent
ee0283cb57
commit
1d4b27f60f
@ -5,8 +5,8 @@ module ActiveModel
|
|||||||
|
|
||||||
included do
|
included do
|
||||||
with_options instance_writer: false, instance_reader: false do |serializer|
|
with_options instance_writer: false, instance_reader: false do |serializer|
|
||||||
serializer.class_attribute :_attribute_mappings # @api private : maps attribute key names to names to names of implementing methods, @see #attribute
|
serializer.class_attribute :_attribute_procs # @api private : maps attribute key names to names to names of implementing methods, @see #attribute
|
||||||
self._attribute_mappings ||= {}
|
self._attribute_procs ||= {}
|
||||||
serializer.class_attribute :_attribute_keys # @api private : maps attribute names to keys, @see #attribute
|
serializer.class_attribute :_attribute_keys # @api private : maps attribute names to keys, @see #attribute
|
||||||
self._attribute_keys ||= {}
|
self._attribute_keys ||= {}
|
||||||
end
|
end
|
||||||
@ -17,7 +17,16 @@ module ActiveModel
|
|||||||
@attributes = nil if reload
|
@attributes = nil if reload
|
||||||
@attributes ||= self.class._attribute_keys.each_with_object({}) do |(name, key), hash|
|
@attributes ||= self.class._attribute_keys.each_with_object({}) do |(name, key), hash|
|
||||||
next unless requested_attrs.nil? || requested_attrs.include?(key)
|
next unless requested_attrs.nil? || requested_attrs.include?(key)
|
||||||
hash[key] = self.class._attribute_mappings[name].call(self)
|
hash[key] = _attribute_value(name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# @api private
|
||||||
|
def _attribute_value(name)
|
||||||
|
if self.class._attribute_procs[name]
|
||||||
|
instance_eval(&self.class._attribute_procs[name])
|
||||||
|
else
|
||||||
|
read_attribute_for_serialization(name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -25,7 +34,7 @@ module ActiveModel
|
|||||||
module ClassMethods
|
module ClassMethods
|
||||||
def inherited(base)
|
def inherited(base)
|
||||||
super
|
super
|
||||||
base._attribute_mappings = _attribute_mappings.dup
|
base._attribute_procs = _attribute_procs.dup
|
||||||
base._attribute_keys = _attribute_keys.dup
|
base._attribute_keys = _attribute_keys.dup
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -54,16 +63,7 @@ module ActiveModel
|
|||||||
# end
|
# end
|
||||||
def attribute(attr, options = {}, &block)
|
def attribute(attr, options = {}, &block)
|
||||||
_attribute_keys[attr] = options.fetch(:key, attr)
|
_attribute_keys[attr] = options.fetch(:key, attr)
|
||||||
_attribute_mappings[attr] = _attribute_mapping(attr, block)
|
_attribute_procs[attr] = block
|
||||||
end
|
|
||||||
|
|
||||||
# @api private
|
|
||||||
def _attribute_mapping(name, block)
|
|
||||||
if block
|
|
||||||
->(instance) { instance.instance_eval(&block) }
|
|
||||||
else
|
|
||||||
->(instance) { instance.read_attribute_for_serialization(name) }
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# @api private
|
# @api private
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user