@@ -5,8 +5,8 @@ module Attributes
5
5
6
6
included do
7
7
with_options instance_writer : false , instance_reader : false do |serializer |
8
- serializer . class_attribute :_attribute_mappings # @api private : maps attribute key names to names to names of implementing methods, @see #attribute
9
- self . _attribute_mappings ||= { }
8
+ serializer . class_attribute :_attribute_procs # @api private : maps attribute key names to names to names of implementing methods, @see #attribute
9
+ self . _attribute_procs ||= { }
10
10
serializer . class_attribute :_attribute_keys # @api private : maps attribute names to keys, @see #attribute
11
11
self . _attribute_keys ||= { }
12
12
end
@@ -17,15 +17,24 @@ def attributes(requested_attrs = nil, reload = false)
17
17
@attributes = nil if reload
18
18
@attributes ||= self . class . _attribute_keys . each_with_object ( { } ) do |( name , key ) , hash |
19
19
next unless requested_attrs . nil? || requested_attrs . include? ( key )
20
- hash [ key ] = self . class . _attribute_mappings [ name ] . call ( self )
20
+ hash [ key ] = _attribute_value ( name )
21
+ end
22
+ end
23
+
24
+ # @api private
25
+ def _attribute_value ( name )
26
+ if self . class . _attribute_procs [ name ]
27
+ instance_eval ( &self . class . _attribute_procs [ name ] )
28
+ else
29
+ read_attribute_for_serialization ( name )
21
30
end
22
31
end
23
32
end
24
33
25
34
module ClassMethods
26
35
def inherited ( base )
27
36
super
28
- base . _attribute_mappings = _attribute_mappings . dup
37
+ base . _attribute_procs = _attribute_procs . dup
29
38
base . _attribute_keys = _attribute_keys . dup
30
39
end
31
40
@@ -54,16 +63,7 @@ def attributes(*attrs)
54
63
# end
55
64
def attribute ( attr , options = { } , &block )
56
65
_attribute_keys [ attr ] = options . fetch ( :key , attr )
57
- _attribute_mappings [ attr ] = _attribute_mapping ( attr , block )
58
- end
59
-
60
- # @api private
61
- def _attribute_mapping ( name , block )
62
- if block
63
- -> ( instance ) { instance . instance_eval ( &block ) }
64
- else
65
- -> ( instance ) { instance . read_attribute_for_serialization ( name ) }
66
- end
66
+ _attribute_procs [ attr ] = block
67
67
end
68
68
69
69
# @api private
0 commit comments