mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Simplify reflections handling.
This commit is contained in:
parent
dff607dc41
commit
762f298c03
@ -26,26 +26,12 @@ module ActiveModel
|
|||||||
# So you can inspect reflections in your Adapters.
|
# So you can inspect reflections in your Adapters.
|
||||||
#
|
#
|
||||||
Reflection = Struct.new(:name, :options, :block) do
|
Reflection = Struct.new(:name, :options, :block) do
|
||||||
delegate :call, to: :reader
|
|
||||||
|
|
||||||
attr_reader :reader
|
|
||||||
|
|
||||||
def initialize(*)
|
|
||||||
super
|
|
||||||
@reader = self.class.build_reader(name, block)
|
|
||||||
end
|
|
||||||
|
|
||||||
# @api private
|
# @api private
|
||||||
def value(instance)
|
def value(instance)
|
||||||
call(instance)
|
|
||||||
end
|
|
||||||
|
|
||||||
# @api private
|
|
||||||
def self.build_reader(name, block)
|
|
||||||
if block
|
if block
|
||||||
->(instance) { instance.read_attribute_for_serialization(name).instance_eval(&block) }
|
instance.read_attribute_for_serialization(name).instance_eval(&block)
|
||||||
else
|
else
|
||||||
->(instance) { instance.read_attribute_for_serialization(name) }
|
instance.read_attribute_for_serialization(name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user