@@ -14,6 +14,9 @@ class Serializer
14
14
include Configuration
15
15
include Associations
16
16
require 'active_model/serializer/adapter'
17
+ Attribute = Struct . new ( :name , :reader ) do
18
+ delegate :call , to : :reader
19
+ end
17
20
18
21
# Matches
19
22
# "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)
46
49
47
50
with_options instance_writer : false , instance_reader : false do |serializer |
48
51
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 ||= { }
51
54
class_attribute :_links # @api private : links definitions, @see Serializer#link
52
55
self . _links ||= { }
53
56
@@ -71,7 +74,7 @@ def self.digest_caller_file(caller_line)
71
74
# Generates a unique digest for each serializer at load.
72
75
def self . inherited ( base )
73
76
caller_line = caller . first
74
- base . _attributes_map = _attributes_map . dup
77
+ base . _attribute_mappings = _attribute_mappings . dup
75
78
base . _links = _links . dup
76
79
base . _cache_digest = digest_caller_file ( caller_line )
77
80
super
@@ -120,7 +123,7 @@ def self.attribute(attr, options = {}, &block)
120
123
-> ( instance ) { instance . send ( attr ) }
121
124
end
122
125
123
- _attributes_map [ key ] = { attr : attr , reader : reader }
126
+ _attribute_mappings [ key ] = Attribute . new ( attr , reader )
124
127
125
128
ActiveModelSerializers . silence_warnings do
126
129
define_method attr do
@@ -130,17 +133,22 @@ def self.attribute(attr, options = {}, &block)
130
133
end
131
134
132
135
# @api private
133
- # An accessor for the old _attributes internal API
136
+ # names of attribute methods
137
+ # @see Serializer::attribute
134
138
def self . _attributes
135
- _attributes_map . keys
139
+ _attribute_mappings . keys
136
140
end
137
141
138
142
# @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
140
146
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
144
152
end
145
153
146
154
# @api private
@@ -268,12 +276,12 @@ def json_key
268
276
# Return the +attributes+ of +object+ as presented
269
277
# by the serializer.
270
278
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 |
272
280
next unless requested_attrs . nil? || requested_attrs . include? ( key )
273
281
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 )
275
283
else
276
- hash [ key ] = details [ :reader ] . call ( self )
284
+ hash [ key ] = attribute_mapping . call ( self )
277
285
end
278
286
end
279
287
end
0 commit comments