3
3
require 'active_model/serializer/array_serializer'
4
4
require 'active_model/serializer/include_tree'
5
5
require 'active_model/serializer/associations'
6
+ require 'active_model/serializer/attributes'
6
7
require 'active_model/serializer/configuration'
7
8
require 'active_model/serializer/fieldset'
8
9
require 'active_model/serializer/lint'
@@ -13,35 +14,8 @@ module ActiveModel
13
14
class Serializer
14
15
include Configuration
15
16
include Associations
17
+ include Attributes
16
18
require 'active_model/serializer/adapter'
17
- class Attribute
18
- delegate :call , to : :reader
19
- attr_reader :name , :reader
20
- def initialize ( name )
21
- @name = name
22
- @reader = nil
23
- end
24
-
25
- def self . build ( name , block )
26
- if block
27
- AttributeBlock . new ( name , block )
28
- else
29
- AttributeReader . new ( name )
30
- end
31
- end
32
- end
33
- class AttributeReader < Attribute
34
- def initialize ( name )
35
- super ( name )
36
- @reader = -> ( instance ) { instance . read_attribute_for_serialization ( name ) }
37
- end
38
- end
39
- class AttributeBlock < Attribute
40
- def initialize ( name , block )
41
- super ( name )
42
- @reader = -> ( instance ) { instance . instance_eval ( &block ) }
43
- end
44
- end
45
19
46
20
# Matches
47
21
# "c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb:1:in `<top (required)>'"
@@ -73,12 +47,9 @@ def self.digest_caller_file(caller_line)
73
47
end
74
48
75
49
with_options instance_writer : false , instance_reader : false do |serializer |
76
- class_attribute :_type , instance_reader : true
77
- class_attribute :_attribute_mappings # @api private : maps attribute key names to names to names of implementing methods, @see Serializer#attribute
78
- self . _attribute_mappings ||= { }
79
- class_attribute :_links # @api private : links definitions, @see Serializer#link
50
+ serializer . class_attribute :_type , instance_reader : true
51
+ serializer . class_attribute :_links # @api private : links definitions, @see Serializer#link
80
52
self . _links ||= { }
81
-
82
53
serializer . class_attribute :_cache # @api private : the cache object
83
54
serializer . class_attribute :_fragmented # @api private : @see ::fragmented
84
55
serializer . class_attribute :_cache_key # @api private : when present, is first item in cache_key
@@ -95,11 +66,10 @@ def self.digest_caller_file(caller_line)
95
66
serializer . class_attribute :_cache_digest # @api private : Generated
96
67
end
97
68
98
- # Serializers inherit serialized_attributes, _attributes_keys , and _reflections .
69
+ # Serializers inherit _attribute_mappings, _reflections , and _links .
99
70
# Generates a unique digest for each serializer at load.
100
71
def self . inherited ( base )
101
72
caller_line = caller . first
102
- base . _attribute_mappings = _attribute_mappings . dup
103
73
base . _links = _links . dup
104
74
base . _cache_digest = digest_caller_file ( caller_line )
105
75
super
@@ -116,54 +86,6 @@ def self.link(name, value = nil, &block)
116
86
_links [ name ] = block || value
117
87
end
118
88
119
- # @example
120
- # class AdminAuthorSerializer < ActiveModel::Serializer
121
- # attributes :id, :name, :recent_edits
122
- def self . attributes ( *attrs )
123
- attrs = attrs . first if attrs . first . class == Array
124
-
125
- attrs . each do |attr |
126
- attribute ( attr )
127
- end
128
- end
129
-
130
- # TODO: remove the dynamic method definition
131
- # @example
132
- # class AdminAuthorSerializer < ActiveModel::Serializer
133
- # attributes :id, :recent_edits
134
- # attribute :name, key: :title
135
- #
136
- # attribute :full_name do
137
- # "#{object.first_name} #{object.last_name}"
138
- # end
139
- #
140
- # def recent_edits
141
- # object.edits.last(5)
142
- # end
143
- def self . attribute ( attr , options = { } , &block )
144
- key = options . fetch ( :key , attr )
145
- _attribute_mappings [ key ] = Attribute . build ( attr , block )
146
- end
147
-
148
- # @api private
149
- # names of attribute methods
150
- # @see Serializer::attribute
151
- def self . _attributes
152
- _attribute_mappings . keys
153
- end
154
-
155
- # @api private
156
- # maps attribute value to explict key name
157
- # @see Serializer::attribute
158
- # @see Adapter::FragmentCache#fragment_serializer
159
- def self . _attributes_keys
160
- _attribute_mappings
161
- . each_with_object ( { } ) do |( key , attribute_mapping ) , hash |
162
- next if key == attribute_mapping . name
163
- hash [ attribute_mapping . name ] = { key : key }
164
- end
165
- end
166
-
167
89
# @api private
168
90
# Used by FragmentCache on the CachedSerializer
169
91
# to call attribute methods on the fragmented cached serializer.
@@ -286,15 +208,6 @@ def json_key
286
208
root || object . class . model_name . to_s . underscore
287
209
end
288
210
289
- # Return the +attributes+ of +object+ as presented
290
- # by the serializer.
291
- def attributes ( requested_attrs = nil )
292
- self . class . _attribute_mappings . each_with_object ( { } ) do |( key , attribute_mapping ) , hash |
293
- next unless requested_attrs . nil? || requested_attrs . include? ( key )
294
- hash [ key ] = attribute_mapping . call ( self )
295
- end
296
- end
297
-
298
211
def read_attribute_for_serialization ( attr )
299
212
if _serializer_method_defined? ( attr )
300
213
send ( attr )
0 commit comments