Skip to content

Commit 0bf45ec

Browse files
committed
Small refactor to Serializer::_attribute_mappings
1 parent 7bde7bf commit 0bf45ec

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

lib/active_model/serializer.rb

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ class Serializer
1414
include Configuration
1515
include Associations
1616
require 'active_model/serializer/adapter'
17+
Attribute = Struct.new(:name, :reader) do
18+
delegate :call, to: :reader
19+
end
1720

1821
# Matches
1922
# "c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb:1:in `<top (required)>'"
@@ -46,8 +49,8 @@ def self.digest_caller_file(caller_line)
4649

4750
with_options instance_writer: false, instance_reader: false do |serializer|
4851
class_attribute :_type, instance_reader: true
49-
class_attribute :_attributes_map # @api private : maps attribute key names to names to names of implementing methods, @see Serializer#attribute
50-
self._attributes_map ||= {}
52+
class_attribute :_attribute_mappings # @api private : maps attribute key names to names to names of implementing methods, @see Serializer#attribute
53+
self._attribute_mappings ||= {}
5154
class_attribute :_links # @api private : links definitions, @see Serializer#link
5255
self._links ||= {}
5356

@@ -71,7 +74,7 @@ def self.digest_caller_file(caller_line)
7174
# Generates a unique digest for each serializer at load.
7275
def self.inherited(base)
7376
caller_line = caller.first
74-
base._attributes_map = _attributes_map.dup
77+
base._attribute_mappings = _attribute_mappings.dup
7578
base._links = _links.dup
7679
base._cache_digest = digest_caller_file(caller_line)
7780
super
@@ -120,7 +123,7 @@ def self.attribute(attr, options = {}, &block)
120123
->(instance) { instance.send(attr) }
121124
end
122125

123-
_attributes_map[key] = { attr: attr, reader: reader }
126+
_attribute_mappings[key] = Attribute.new(attr, reader)
124127

125128
ActiveModelSerializers.silence_warnings do
126129
define_method attr do
@@ -130,17 +133,22 @@ def self.attribute(attr, options = {}, &block)
130133
end
131134

132135
# @api private
133-
# An accessor for the old _attributes internal API
136+
# names of attribute methods
137+
# @see Serializer::attribute
134138
def self._attributes
135-
_attributes_map.keys
139+
_attribute_mappings.keys
136140
end
137141

138142
# @api private
139-
# An accessor for the old _attributes_keys internal API
143+
# maps attribute value to explict key name
144+
# @see Serializer::attribute
145+
# @see Adapter::FragmentCache#fragment_serializer
140146
def self._attributes_keys
141-
_attributes_map
142-
.select { |key, details| key != details[:attr] }
143-
.each_with_object({}) { |(key, details), acc| acc[details[:attr]] = { key: key } }
147+
_attribute_mappings
148+
.each_with_object({}) do |(key, attribute_mapping), hash|
149+
next if key == attribute_mapping.name
150+
hash[attribute_mapping.name] = { key: key }
151+
end
144152
end
145153

146154
# @api private
@@ -268,12 +276,12 @@ def json_key
268276
# Return the +attributes+ of +object+ as presented
269277
# by the serializer.
270278
def attributes(requested_attrs = nil)
271-
self.class._attributes_map.each_with_object({}) do |(key, details), hash|
279+
self.class._attribute_mappings.each_with_object({}) do |(key, attribute_mapping), hash|
272280
next unless requested_attrs.nil? || requested_attrs.include?(key)
273281
if self.class._fragmented
274-
hash[key] = self.class._fragmented.public_send(details[:attr])
282+
hash[key] = self.class._fragmented.public_send(attribute_mapping.name)
275283
else
276-
hash[key] = details[:reader].call(self)
284+
hash[key] = attribute_mapping.call(self)
277285
end
278286
end
279287
end

0 commit comments

Comments
 (0)