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



6
7
8
# File 'lib/active_model/serializer/lazy_association.rb', line 6

def association_options
  @association_options
end

#reflectionObject

Returns the value of attribute reflection

Returns:

  • (Object)

    the current value of reflection



6
7
8
# File 'lib/active_model/serializer/lazy_association.rb', line 6

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)


24
25
26
27
28
29
# File 'lib/active_model/serializer/lazy_association.rb', line 24

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.



15
16
17
18
19
20
21
# File 'lib/active_model/serializer/lazy_association.rb', line 15

def object
  return @object if defined?(@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.



11
12
13
# File 'lib/active_model/serializer/lazy_association.rb', line 11

def reflection_options
  @reflection_options ||= reflection.options.select { |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:



32
33
34
35
36
37
38
39
40
# File 'lib/active_model/serializer/lazy_association.rb', line 32

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.



46
47
48
49
50
51
# File 'lib/active_model/serializer/lazy_association.rb', line 46

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.



42
43
44
# File 'lib/active_model/serializer/lazy_association.rb', line 42

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