mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Merge pull request #1370 from beauby/simplify-attributes
Simplify attributes handling.
This commit is contained in:
commit
b51a43262f
@ -35,6 +35,7 @@ Fixes:
|
|||||||
- [#1358](https://github.com/rails-api/active_model_serializers/pull/1358) Handle serializer file paths with spaces (@rwstauner, @bf4)
|
- [#1358](https://github.com/rails-api/active_model_serializers/pull/1358) Handle serializer file paths with spaces (@rwstauner, @bf4)
|
||||||
|
|
||||||
Misc:
|
Misc:
|
||||||
|
- [#1370](https://github.com/rails-api/active_model_serializers/pull/1370) Simplify attributes handling via a mixin (@beauby)
|
||||||
- [#1233](https://github.com/rails-api/active_model_serializers/pull/1233) Top-level meta and meta_key options no longer handled at serializer level (@beauby)
|
- [#1233](https://github.com/rails-api/active_model_serializers/pull/1233) Top-level meta and meta_key options no longer handled at serializer level (@beauby)
|
||||||
- [#1232](https://github.com/rails-api/active_model_serializers/pull/1232) fields option no longer handled at serializer level (@beauby)
|
- [#1232](https://github.com/rails-api/active_model_serializers/pull/1232) fields option no longer handled at serializer level (@beauby)
|
||||||
- [#1178](https://github.com/rails-api/active_model_serializers/pull/1178) env CAPTURE_STDERR=false lets devs see hard failures (@bf4)
|
- [#1178](https://github.com/rails-api/active_model_serializers/pull/1178) env CAPTURE_STDERR=false lets devs see hard failures (@bf4)
|
||||||
|
|||||||
13
lib/active_model/serializer/attribute.rb
Normal file
13
lib/active_model/serializer/attribute.rb
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
module ActiveModel
|
||||||
|
class Serializer
|
||||||
|
Attribute = Struct.new(:name, :block) do
|
||||||
|
def value(serializer)
|
||||||
|
if block
|
||||||
|
serializer.instance_eval(&block)
|
||||||
|
else
|
||||||
|
serializer.read_attribute_for_serialization(name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -1,55 +1,24 @@
|
|||||||
module ActiveModel
|
module ActiveModel
|
||||||
class Serializer
|
class Serializer
|
||||||
module Attributes
|
module Attributes
|
||||||
# @api private
|
|
||||||
class Attribute
|
|
||||||
delegate :call, to: :reader
|
|
||||||
|
|
||||||
attr_reader :name, :reader
|
|
||||||
|
|
||||||
def initialize(name)
|
|
||||||
@name = name
|
|
||||||
@reader = :no_reader
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.build(name, block)
|
|
||||||
if block
|
|
||||||
AttributeBlock.new(name, block)
|
|
||||||
else
|
|
||||||
AttributeReader.new(name)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
# @api private
|
|
||||||
class AttributeReader < Attribute
|
|
||||||
def initialize(name)
|
|
||||||
super(name)
|
|
||||||
@reader = ->(instance) { instance.read_attribute_for_serialization(name) }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
# @api private
|
|
||||||
class AttributeBlock < Attribute
|
|
||||||
def initialize(name, block)
|
|
||||||
super(name)
|
|
||||||
@reader = ->(instance) { instance.instance_eval(&block) }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
included do
|
included do
|
||||||
with_options instance_writer: false, instance_reader: false do |serializer|
|
with_options instance_writer: false, instance_reader: false do |serializer|
|
||||||
serializer.class_attribute :_attribute_mappings # @api private : maps attribute key names to names to names of implementing methods, @see #attribute
|
serializer.class_attribute :_attributes_data # @api private
|
||||||
self._attribute_mappings ||= {}
|
self._attributes_data ||= {}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
extend ActiveSupport::Autoload
|
||||||
|
autoload :Attribute
|
||||||
|
|
||||||
# Return the +attributes+ of +object+ as presented
|
# Return the +attributes+ of +object+ as presented
|
||||||
# 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._attribute_mappings.each_with_object({}) do |(key, attribute_mapping), hash|
|
@attributes ||= self.class._attributes_data.each_with_object({}) do |(key, attr), hash|
|
||||||
next unless requested_attrs.nil? || requested_attrs.include?(key)
|
next unless requested_attrs.nil? || requested_attrs.include?(key)
|
||||||
hash[key] = attribute_mapping.call(self)
|
hash[key] = attr.value(self)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -57,7 +26,7 @@ module ActiveModel
|
|||||||
module ClassMethods
|
module ClassMethods
|
||||||
def inherited(base)
|
def inherited(base)
|
||||||
super
|
super
|
||||||
base._attribute_mappings = _attribute_mappings.dup
|
base._attributes_data = _attributes_data.dup
|
||||||
end
|
end
|
||||||
|
|
||||||
# @example
|
# @example
|
||||||
@ -85,14 +54,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)
|
||||||
_attribute_mappings[key] = Attribute.build(attr, block)
|
_attributes_data[key] = Attribute.new(attr, block)
|
||||||
end
|
end
|
||||||
|
|
||||||
# @api private
|
# @api private
|
||||||
# names of attribute methods
|
# keys of attributes
|
||||||
# @see Serializer::attribute
|
# @see Serializer::attribute
|
||||||
def _attributes
|
def _attributes
|
||||||
_attribute_mappings.keys
|
_attributes_data.keys
|
||||||
end
|
end
|
||||||
|
|
||||||
# @api private
|
# @api private
|
||||||
@ -100,10 +69,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
|
||||||
_attribute_mappings
|
_attributes_data
|
||||||
.each_with_object({}) do |(key, attribute_mapping), hash|
|
.each_with_object({}) do |(key, attr), hash|
|
||||||
next if key == attribute_mapping.name
|
next if key == attr.name
|
||||||
hash[attribute_mapping.name] = { key: key }
|
hash[attr.name] = { key: key }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user