Class: ActiveModel::Serializer::LazyAssociation Private

Inherits:
Struct
  • Object
show all
Defined in:
lib/active_model/serializer/lazy_association.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

REFLECTION_OPTIONS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%i(key links polymorphic meta serializer virtual_value namespace).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#association_optionsObject

Returns the value of attribute association_options

Returns:

  • (Object)

    the current value of association_options


4
5
6
# File 'lib/active_model/serializer/lazy_association.rb', line 4

def association_options
  @association_options
end

#reflectionObject

Returns the value of attribute reflection

Returns:

  • (Object)

    the current value of reflection


4
5
6
# File 'lib/active_model/serializer/lazy_association.rb', line 4

def reflection
  @reflection
end

Instance Method Details

#include_data?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

21
22
23
24
25
26
# File 'lib/active_model/serializer/lazy_association.rb', line 21

def include_data?
  eval_reflection_block if reflection.block
  reflection.include_data?(
    association_options.fetch(:include_slice)
  )
end

#objectObject Also known as: eval_reflection_block

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


13
14
15
16
17
18
# File 'lib/active_model/serializer/lazy_association.rb', line 13

def object
  @object ||= reflection.value(
    association_options.fetch(:parent_serializer),
    association_options.fetch(:include_slice)
  )
end

#reflection_optionsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


9
10
11
# File 'lib/active_model/serializer/lazy_association.rb', line 9

def reflection_options
  @reflection_options ||= reflection.options.dup.reject { |k, _| !REFLECTION_OPTIONS.include?(k) }
end

#serializerActiveModel::Serializer?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:


29
30
31
32
33
34
35
36
37
# File 'lib/active_model/serializer/lazy_association.rb', line 29

def serializer
  return @serializer if defined?(@serializer)
  if serializer_class
    serialize_object!(object)
  elsif !object.nil? && !object.instance_of?(Object)
    cached_result[:virtual_value] = object
  end
  @serializer = cached_result[:serializer]
end

#serializer_classObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


43
44
45
46
47
48
# File 'lib/active_model/serializer/lazy_association.rb', line 43

def serializer_class
  return @serializer_class if defined?(@serializer_class)
  serializer_for_options = { namespace: namespace }
  serializer_for_options[:serializer] = reflection_options[:serializer] if reflection_options.key?(:serializer)
  @serializer_class = association_options.fetch(:parent_serializer).class.serializer_for(object, serializer_for_options)
end

#virtual_valueObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


39
40
41
# File 'lib/active_model/serializer/lazy_association.rb', line 39

def virtual_value
  cached_result[:virtual_value] || reflection_options[:virtual_value]
end