|
1 | 1 | module ActiveModel
|
2 | 2 | class Serializer
|
3 | 3 | module Attributes
|
4 |
| - # @api private |
5 |
| - class Attribute |
6 |
| - delegate :call, to: :reader |
7 |
| - |
8 |
| - attr_reader :name, :reader |
9 |
| - |
10 |
| - def initialize(name) |
11 |
| - @name = name |
12 |
| - @reader = :no_reader |
13 |
| - end |
14 |
| - |
15 |
| - def self.build(name, block) |
16 |
| - if block |
17 |
| - AttributeBlock.new(name, block) |
18 |
| - else |
19 |
| - AttributeReader.new(name) |
20 |
| - end |
21 |
| - end |
22 |
| - end |
23 |
| - # @api private |
24 |
| - class AttributeReader < Attribute |
25 |
| - def initialize(name) |
26 |
| - super(name) |
27 |
| - @reader = ->(instance) { instance.read_attribute_for_serialization(name) } |
28 |
| - end |
29 |
| - end |
30 |
| - # @api private |
31 |
| - class AttributeBlock < Attribute |
32 |
| - def initialize(name, block) |
33 |
| - super(name) |
34 |
| - @reader = ->(instance) { instance.instance_eval(&block) } |
35 |
| - end |
36 |
| - end |
37 |
| - |
38 | 4 | extend ActiveSupport::Concern
|
39 | 5 |
|
40 | 6 | included do
|
41 | 7 | with_options instance_writer: false, instance_reader: false do |serializer|
|
42 |
| - serializer.class_attribute :_attribute_mappings # @api private : maps attribute key names to names to names of implementing methods, @see #attribute |
43 |
| - self._attribute_mappings ||= {} |
| 8 | + serializer.class_attribute :_attributes_data # @api private |
| 9 | + self._attributes_data ||= {} |
44 | 10 | end
|
45 | 11 |
|
| 12 | + extend ActiveSupport::Autoload |
| 13 | + autoload :Attribute |
| 14 | + |
46 | 15 | # Return the +attributes+ of +object+ as presented
|
47 | 16 | # by the serializer.
|
48 | 17 | def attributes(requested_attrs = nil, reload = false)
|
49 | 18 | @attributes = nil if reload
|
50 |
| - @attributes ||= self.class._attribute_mappings.each_with_object({}) do |(key, attribute_mapping), hash| |
| 19 | + @attributes ||= self.class._attributes_data.each_with_object({}) do |(key, attr), hash| |
51 | 20 | next unless requested_attrs.nil? || requested_attrs.include?(key)
|
52 |
| - hash[key] = attribute_mapping.call(self) |
| 21 | + hash[key] = attr.value(self) |
53 | 22 | end
|
54 | 23 | end
|
55 | 24 | end
|
56 | 25 |
|
57 | 26 | module ClassMethods
|
58 | 27 | def inherited(base)
|
59 | 28 | super
|
60 |
| - base._attribute_mappings = _attribute_mappings.dup |
| 29 | + base._attributes_data = _attributes_data.dup |
61 | 30 | end
|
62 | 31 |
|
63 | 32 | # @example
|
@@ -85,25 +54,25 @@ def attributes(*attrs)
|
85 | 54 | # end
|
86 | 55 | def attribute(attr, options = {}, &block)
|
87 | 56 | key = options.fetch(:key, attr)
|
88 |
| - _attribute_mappings[key] = Attribute.build(attr, block) |
| 57 | + _attributes_data[key] = Attribute.new(attr, block) |
89 | 58 | end
|
90 | 59 |
|
91 | 60 | # @api private
|
92 |
| - # names of attribute methods |
| 61 | + # keys of attributes |
93 | 62 | # @see Serializer::attribute
|
94 | 63 | def _attributes
|
95 |
| - _attribute_mappings.keys |
| 64 | + _attributes_data.keys |
96 | 65 | end
|
97 | 66 |
|
98 | 67 | # @api private
|
99 | 68 | # maps attribute value to explict key name
|
100 | 69 | # @see Serializer::attribute
|
101 | 70 | # @see Adapter::FragmentCache#fragment_serializer
|
102 | 71 | def _attributes_keys
|
103 |
| - _attribute_mappings |
104 |
| - .each_with_object({}) do |(key, attribute_mapping), hash| |
105 |
| - next if key == attribute_mapping.name |
106 |
| - hash[attribute_mapping.name] = { key: key } |
| 72 | + _attributes_data |
| 73 | + .each_with_object({}) do |(key, attr), hash| |
| 74 | + next if key == attr.name |
| 75 | + hash[attr.name] = { key: key } |
107 | 76 | end
|
108 | 77 | end
|
109 | 78 | end
|
|
0 commit comments