mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Extract latent Attribute object.
This commit is contained in:
parent
1d4b27f60f
commit
7d24cbfd3d
13
lib/active_model/serializer/attribute.rb
Normal file
13
lib/active_model/serializer/attribute.rb
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
module ActiveModel
|
||||||
|
class Serializer
|
||||||
|
Attribute = Struct.new(:name, :key, :block) do
|
||||||
|
def value(serializer)
|
||||||
|
if block
|
||||||
|
serializer.instance_eval(&block)
|
||||||
|
else
|
||||||
|
serializer.read_attribute_for_serialization(name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -5,28 +5,19 @@ 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_procs # @api private : maps attribute key names to names to names of implementing methods, @see #attribute
|
serializer.class_attribute :_attributes_data # @api private
|
||||||
self._attribute_procs ||= {}
|
self._attributes_data ||= {}
|
||||||
serializer.class_attribute :_attribute_keys # @api private : maps attribute names to keys, @see #attribute
|
|
||||||
self._attribute_keys ||= {}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
autoload :Attribute
|
||||||
|
|
||||||
# Return the +attributes+ of +object+ as presented
|
# Return the +attributes+ of +object+ as presented
|
||||||
# by the serializer.
|
# by the serializer.
|
||||||
def attributes(requested_attrs = nil, reload = false)
|
def attributes(requested_attrs = nil, reload = false)
|
||||||
@attributes = nil if reload
|
@attributes = nil if reload
|
||||||
@attributes ||= self.class._attribute_keys.each_with_object({}) do |(name, key), hash|
|
@attributes ||= self.class._attributes_data.values.each_with_object({}) do |attr, hash|
|
||||||
next unless requested_attrs.nil? || requested_attrs.include?(key)
|
next unless requested_attrs.nil? || requested_attrs.include?(attr.key)
|
||||||
hash[key] = _attribute_value(name)
|
hash[attr.key] = attr.value(self)
|
||||||
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
|
||||||
@ -34,8 +25,7 @@ module ActiveModel
|
|||||||
module ClassMethods
|
module ClassMethods
|
||||||
def inherited(base)
|
def inherited(base)
|
||||||
super
|
super
|
||||||
base._attribute_procs = _attribute_procs.dup
|
base._attributes_data = _attributes_data.dup
|
||||||
base._attribute_keys = _attribute_keys.dup
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# @example
|
# @example
|
||||||
@ -62,15 +52,15 @@ module ActiveModel
|
|||||||
# object.edits.last(5)
|
# object.edits.last(5)
|
||||||
# end
|
# end
|
||||||
def attribute(attr, options = {}, &block)
|
def attribute(attr, options = {}, &block)
|
||||||
_attribute_keys[attr] = options.fetch(:key, attr)
|
key = options.fetch(:key, attr)
|
||||||
_attribute_procs[attr] = block
|
_attributes_data[attr] = Attribute.new(attr, key, block)
|
||||||
end
|
end
|
||||||
|
|
||||||
# @api private
|
# @api private
|
||||||
# keys of attributes
|
# keys of attributes
|
||||||
# @see Serializer::attribute
|
# @see Serializer::attribute
|
||||||
def _attributes
|
def _attributes
|
||||||
_attribute_keys.values
|
_attributes_data.values.map(&:key)
|
||||||
end
|
end
|
||||||
|
|
||||||
# @api private
|
# @api private
|
||||||
@ -78,10 +68,10 @@ module ActiveModel
|
|||||||
# @see Serializer::attribute
|
# @see Serializer::attribute
|
||||||
# @see Adapter::FragmentCache#fragment_serializer
|
# @see Adapter::FragmentCache#fragment_serializer
|
||||||
def _attributes_keys
|
def _attributes_keys
|
||||||
_attribute_keys
|
_attributes_data.values
|
||||||
.each_with_object({}) do |(name, key), hash|
|
.each_with_object({}) do |attr, hash|
|
||||||
next if key == name
|
next if attr.key == attr.name
|
||||||
hash[name] = { key: key }
|
hash[attr.name] = { key: attr.key }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user