Remove key from Attribute class.

This commit is contained in:
Lucas Hosseini 2015-12-29 22:45:41 +01:00
parent 7d24cbfd3d
commit a586a45863
2 changed files with 10 additions and 10 deletions

View File

@ -1,6 +1,6 @@
module ActiveModel module ActiveModel
class Serializer class Serializer
Attribute = Struct.new(:name, :key, :block) do Attribute = Struct.new(:name, :block) do
def value(serializer) def value(serializer)
if block if block
serializer.instance_eval(&block) serializer.instance_eval(&block)

View File

@ -15,9 +15,9 @@ module ActiveModel
# by the serializer. # by the serializer.
def attributes(requested_attrs = nil, reload = false) def attributes(requested_attrs = nil, reload = false)
@attributes = nil if reload @attributes = nil if reload
@attributes ||= self.class._attributes_data.values.each_with_object({}) do |attr, hash| @attributes ||= self.class._attributes_data.each_with_object({}) do |(key, attr), hash|
next unless requested_attrs.nil? || requested_attrs.include?(attr.key) next unless requested_attrs.nil? || requested_attrs.include?(key)
hash[attr.key] = attr.value(self) hash[key] = attr.value(self)
end end
end end
end end
@ -53,14 +53,14 @@ module ActiveModel
# end # end
def attribute(attr, options = {}, &block) def attribute(attr, options = {}, &block)
key = options.fetch(:key, attr) key = options.fetch(:key, attr)
_attributes_data[attr] = Attribute.new(attr, key, block) _attributes_data[key] = Attribute.new(attr, block)
end end
# @api private # @api private
# keys of attributes # keys of attributes
# @see Serializer::attribute # @see Serializer::attribute
def _attributes def _attributes
_attributes_data.values.map(&:key) _attributes_data.keys
end end
# @api private # @api private
@ -68,10 +68,10 @@ module ActiveModel
# @see Serializer::attribute # @see Serializer::attribute
# @see Adapter::FragmentCache#fragment_serializer # @see Adapter::FragmentCache#fragment_serializer
def _attributes_keys def _attributes_keys
_attributes_data.values _attributes_data
.each_with_object({}) do |attr, hash| .each_with_object({}) do |(key, attr), hash|
next if attr.key == attr.name next if key == attr.name
hash[attr.name] = { key: attr.key } hash[attr.name] = { key: key }
end end
end end
end end