Small refactor to Serializer::_attribute_mappings

This commit is contained in:
Benjamin Fleischer 2015-12-01 23:39:40 -06:00
parent 7bde7bf752
commit 0bf45ec2a7

View File

@ -14,6 +14,9 @@ module ActiveModel
include Configuration include Configuration
include Associations include Associations
require 'active_model/serializer/adapter' require 'active_model/serializer/adapter'
Attribute = Struct.new(:name, :reader) do
delegate :call, to: :reader
end
# Matches # Matches
# "c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb:1:in `<top (required)>'" # "c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb:1:in `<top (required)>'"
@ -46,8 +49,8 @@ module ActiveModel
with_options instance_writer: false, instance_reader: false do |serializer| with_options instance_writer: false, instance_reader: false do |serializer|
class_attribute :_type, instance_reader: true class_attribute :_type, instance_reader: true
class_attribute :_attributes_map # @api private : maps attribute key names to names to names of implementing methods, @see Serializer#attribute class_attribute :_attribute_mappings # @api private : maps attribute key names to names to names of implementing methods, @see Serializer#attribute
self._attributes_map ||= {} self._attribute_mappings ||= {}
class_attribute :_links # @api private : links definitions, @see Serializer#link class_attribute :_links # @api private : links definitions, @see Serializer#link
self._links ||= {} self._links ||= {}
@ -71,7 +74,7 @@ module ActiveModel
# Generates a unique digest for each serializer at load. # Generates a unique digest for each serializer at load.
def self.inherited(base) def self.inherited(base)
caller_line = caller.first caller_line = caller.first
base._attributes_map = _attributes_map.dup base._attribute_mappings = _attribute_mappings.dup
base._links = _links.dup base._links = _links.dup
base._cache_digest = digest_caller_file(caller_line) base._cache_digest = digest_caller_file(caller_line)
super super
@ -120,7 +123,7 @@ module ActiveModel
->(instance) { instance.send(attr) } ->(instance) { instance.send(attr) }
end end
_attributes_map[key] = { attr: attr, reader: reader } _attribute_mappings[key] = Attribute.new(attr, reader)
ActiveModelSerializers.silence_warnings do ActiveModelSerializers.silence_warnings do
define_method attr do define_method attr do
@ -130,17 +133,22 @@ module ActiveModel
end end
# @api private # @api private
# An accessor for the old _attributes internal API # names of attribute methods
# @see Serializer::attribute
def self._attributes def self._attributes
_attributes_map.keys _attribute_mappings.keys
end end
# @api private # @api private
# An accessor for the old _attributes_keys internal API # maps attribute value to explict key name
# @see Serializer::attribute
# @see Adapter::FragmentCache#fragment_serializer
def self._attributes_keys def self._attributes_keys
_attributes_map _attribute_mappings
.select { |key, details| key != details[:attr] } .each_with_object({}) do |(key, attribute_mapping), hash|
.each_with_object({}) { |(key, details), acc| acc[details[:attr]] = { key: key } } next if key == attribute_mapping.name
hash[attribute_mapping.name] = { key: key }
end
end end
# @api private # @api private
@ -268,12 +276,12 @@ module ActiveModel
# 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) def attributes(requested_attrs = nil)
self.class._attributes_map.each_with_object({}) do |(key, details), hash| self.class._attribute_mappings.each_with_object({}) do |(key, attribute_mapping), hash|
next unless requested_attrs.nil? || requested_attrs.include?(key) next unless requested_attrs.nil? || requested_attrs.include?(key)
if self.class._fragmented if self.class._fragmented
hash[key] = self.class._fragmented.public_send(details[:attr]) hash[key] = self.class._fragmented.public_send(attribute_mapping.name)
else else
hash[key] = details[:reader].call(self) hash[key] = attribute_mapping.call(self)
end end
end end
end end