Skip to content

Commit a7e2fbb

Browse files
committed
Extract latent Attribute object.
1 parent 32eb5ea commit a7e2fbb

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module ActiveModel
2+
class Serializer
3+
Attribute = Struct.new(:name, :key, :block) do
4+
def value(serializer)
5+
if block
6+
serializer.instance_eval(&block)
7+
else
8+
serializer.read_attribute_for_serialization(name)
9+
end
10+
end
11+
end
12+
end
13+
end

lib/active_model/serializer/attributes.rb

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,27 @@ module Attributes
55

66
included do
77
with_options instance_writer: false, instance_reader: false do |serializer|
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-
serializer.class_attribute :_attribute_keys # @api private : maps attribute names to keys, @see #attribute
11-
self._attribute_keys ||= {}
8+
serializer.class_attribute :_attributes_data # @api private
9+
self._attributes_data ||= {}
1210
end
1311

12+
autoload :Attribute
13+
1414
# Return the +attributes+ of +object+ as presented
1515
# by the serializer.
1616
def attributes(requested_attrs = nil, reload = false)
1717
@attributes = nil if reload
18-
@attributes ||= self.class._attribute_keys.each_with_object({}) do |(name, key), hash|
19-
next unless requested_attrs.nil? || requested_attrs.include?(key)
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)
18+
@attributes ||= self.class._attributes_data.values.each_with_object({}) do |attr, hash|
19+
next unless requested_attrs.nil? || requested_attrs.include?(attr.key)
20+
hash[attr.key] = attr.value(self)
3021
end
3122
end
3223
end
3324

3425
module ClassMethods
3526
def inherited(base)
3627
super
37-
base._attribute_procs = _attribute_procs.dup
38-
base._attribute_keys = _attribute_keys.dup
28+
base._attributes_data = _attributes_data.dup
3929
end
4030

4131
# @example
@@ -62,26 +52,25 @@ def attributes(*attrs)
6252
# object.edits.last(5)
6353
# end
6454
def attribute(attr, options = {}, &block)
65-
_attribute_keys[attr] = options.fetch(:key, attr)
66-
_attribute_procs[attr] = block
55+
_attributes_data[attr] = Attribute.new(attr, options.fetch(:key, attr), block)
6756
end
6857

6958
# @api private
7059
# keys of attributes
7160
# @see Serializer::attribute
7261
def _attributes
73-
_attribute_keys.values
62+
_attributes_data.values.map(&:key)
7463
end
7564

7665
# @api private
7766
# maps attribute value to explict key name
7867
# @see Serializer::attribute
7968
# @see Adapter::FragmentCache#fragment_serializer
8069
def _attributes_keys
81-
_attribute_keys
82-
.each_with_object({}) do |(name, key), hash|
83-
next if key == name
84-
hash[name] = { key: key }
70+
_attributes_data.values
71+
.each_with_object({}) do |attr, hash|
72+
next if attr.key == attr.name
73+
hash[attr.name] = { key: attr.key }
8574
end
8675
end
8776
end

0 commit comments

Comments
 (0)