Simplify attributes handling.

This commit is contained in:
Lucas Hosseini 2015-12-11 00:29:38 +01:00
parent 7bc66c5334
commit ee0283cb57

View File

@ -1,55 +1,23 @@
module ActiveModel module ActiveModel
class Serializer class Serializer
module Attributes module Attributes
# @api private
class Attribute
delegate :call, to: :reader
attr_reader :name, :reader
def initialize(name)
@name = name
@reader = :no_reader
end
def self.build(name, block)
if block
AttributeBlock.new(name, block)
else
AttributeReader.new(name)
end
end
end
# @api private
class AttributeReader < Attribute
def initialize(name)
super(name)
@reader = ->(instance) { instance.read_attribute_for_serialization(name) }
end
end
# @api private
class AttributeBlock < Attribute
def initialize(name, block)
super(name)
@reader = ->(instance) { instance.instance_eval(&block) }
end
end
extend ActiveSupport::Concern extend ActiveSupport::Concern
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_mappings # @api private : maps attribute key names to names to names of implementing methods, @see #attribute
self._attribute_mappings ||= {} self._attribute_mappings ||= {}
serializer.class_attribute :_attribute_keys # @api private : maps attribute names to keys, @see #attribute
self._attribute_keys ||= {}
end end
# 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_mappings.each_with_object({}) do |(key, attribute_mapping), 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] = attribute_mapping.call(self) hash[key] = self.class._attribute_mappings[name].call(self)
end end
end end
end end
@ -58,6 +26,7 @@ module ActiveModel
def inherited(base) def inherited(base)
super super
base._attribute_mappings = _attribute_mappings.dup base._attribute_mappings = _attribute_mappings.dup
base._attribute_keys = _attribute_keys.dup
end end
# @example # @example
@ -84,15 +53,24 @@ module ActiveModel
# object.edits.last(5) # object.edits.last(5)
# end # end
def attribute(attr, options = {}, &block) def attribute(attr, options = {}, &block)
key = options.fetch(:key, attr) _attribute_keys[attr] = options.fetch(:key, attr)
_attribute_mappings[key] = Attribute.build(attr, block) _attribute_mappings[attr] = _attribute_mapping(attr, block)
end end
# @api private # @api private
# names of attribute methods def _attribute_mapping(name, block)
if block
->(instance) { instance.instance_eval(&block) }
else
->(instance) { instance.read_attribute_for_serialization(name) }
end
end
# @api private
# keys of attributes
# @see Serializer::attribute # @see Serializer::attribute
def _attributes def _attributes
_attribute_mappings.keys _attribute_keys.values
end end
# @api private # @api private
@ -100,10 +78,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_mappings _attribute_keys
.each_with_object({}) do |(key, attribute_mapping), hash| .each_with_object({}) do |(name, key), hash|
next if key == attribute_mapping.name next if key == name
hash[attribute_mapping.name] = { key: key } hash[name] = { key: key }
end end
end end
end end