Skip to content

Commit

Permalink
Extract latent Attribute object.
Browse files Browse the repository at this point in the history
  • Loading branch information
beauby committed Dec 27, 2015
1 parent 32eb5ea commit 8fe3ecf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
13 changes: 13 additions & 0 deletions lib/active_model/serializer/attribute.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module ActiveModel
class Serializer
Attribute = Struct.new(:name, :key, :block) do
def value(serializer)
if block
serializer.instance_eval(&block)
else
serializer.read_attribute_for_serialization(name)
end
end
end
end
end
40 changes: 15 additions & 25 deletions lib/active_model/serializer/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,27 @@ module Attributes

included do
with_options instance_writer: false, instance_reader: false do |serializer|
serializer.class_attribute :_attribute_procs # @api private : maps attribute key names to names to names of implementing methods, @see #attribute
self._attribute_procs ||= {}
serializer.class_attribute :_attribute_keys # @api private : maps attribute names to keys, @see #attribute
self._attribute_keys ||= {}
serializer.class_attribute :_attributes_data # @api private
self._attributes_data ||= {}
end

autoload :Attribute

# Return the +attributes+ of +object+ as presented
# by the serializer.
def attributes(requested_attrs = nil, reload = false)
@attributes = nil if reload
@attributes ||= self.class._attribute_keys.each_with_object({}) do |(name, key), hash|
next unless requested_attrs.nil? || requested_attrs.include?(key)
hash[key] = _attribute_value(name)
end
end

# @api private
def _attribute_value(name)
if self.class._attribute_procs[name]
instance_eval(&self.class._attribute_procs[name])
else
read_attribute_for_serialization(name)
@attributes ||= self.class._attributes_data.values.each_with_object({}) do |attr, hash|
next unless requested_attrs.nil? || requested_attrs.include?(attr.key)
hash[attr.key] = attr.value(self)
end
end
end

module ClassMethods
def inherited(base)
super
base._attribute_procs = _attribute_procs.dup
base._attribute_keys = _attribute_keys.dup
base._attributes_data = _attributes_data.dup
end

# @example
Expand All @@ -62,26 +52,26 @@ def attributes(*attrs)
# object.edits.last(5)
# end
def attribute(attr, options = {}, &block)
_attribute_keys[attr] = options.fetch(:key, attr)
_attribute_procs[attr] = block
key = options.fetch(:key, attr)
_attributes_data[attr] = Attribute.new(attr, key, block)
end

# @api private
# keys of attributes
# @see Serializer::attribute
def _attributes
_attribute_keys.values
_attributes_data.values.map(&:key)
end

# @api private
# maps attribute value to explict key name
# @see Serializer::attribute
# @see Adapter::FragmentCache#fragment_serializer
def _attributes_keys
_attribute_keys
.each_with_object({}) do |(name, key), hash|
next if key == name
hash[name] = { key: key }
_attributes_data.values
.each_with_object({}) do |attr, hash|
next if attr.key == attr.name
hash[attr.name] = { key: attr.key }
end
end
end
Expand Down

0 comments on commit 8fe3ecf

Please sign in to comment.