Class: BrightSerializer::AttributeRelation

Inherits:
Attribute
  • Object
show all
Defined in:
lib/bright_serializer/attribute_relation.rb

Instance Attribute Summary

Attributes inherited from Attribute

#block, #condition, #entity, #key, #transformed_key

Instance Method Summary collapse

Methods inherited from Attribute

#condition?

Constructor Details

#initialize(key, serializer, condition, entity, options, &block) ⇒ AttributeRelation

Returns a new instance of AttributeRelation.



5
6
7
8
9
10
11
# File 'lib/bright_serializer/attribute_relation.rb', line 5

def initialize(key, serializer, condition, entity, options, &block)
  @serializer = serializer
  @options = options || {}

  add_entity_ref!(entity)
  super(key, condition, entity, &block)
end

Instance Method Details

#serialize(serializer_instance, object, params) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/bright_serializer/attribute_relation.rb', line 13

def serialize(serializer_instance, object, params)
  return unless object

  merged_params = nil
  merged_params = (params || {}).merge(@options[:params] || {}) if params || @options[:params]
  value = attribute_value(serializer_instance, object, merged_params)

  class_serializer.new(value, params: merged_params, **@options).serializable_hash
end