Class: EasySerializer::AttributeSerializer

Inherits:
Struct
  • Object
show all
Includes:
Helpers
Defined in:
lib/easy_serializer/attribute_serializer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#option_to_value

Instance Attribute Details

#metadataObject

Returns the value of attribute metadata

Returns:

  • (Object)

    the current value of metadata



2
3
4
# File 'lib/easy_serializer/attribute_serializer.rb', line 2

def 
  @metadata
end

#serializerObject

Returns the value of attribute serializer

Returns:

  • (Object)

    the current value of serializer



2
3
4
# File 'lib/easy_serializer/attribute_serializer.rb', line 2

def serializer
  @serializer
end

Class Method Details

.call(serializer, metadata) ⇒ Object



4
5
6
# File 'lib/easy_serializer/attribute_serializer.rb', line 4

def self.call(serializer, )
  new(serializer, ).value
end

Instance Method Details

#attr_serializerObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/easy_serializer/attribute_serializer.rb', line 26

def attr_serializer
  value = cache_or_attribute

  return value.output if value.is_a?(CacheOutput)
  return value unless .nested_serializer?
  if .is_a? Collection
    ary = value.is_a?(Array) ? value : (value == nil ? [] : [value])
    ary.map { |o| nested_serialization!(o) }
  else
    nested_serialization!(value)
  end
end

#cache_or_attributeObject



39
40
41
42
43
44
45
# File 'lib/easy_serializer/attribute_serializer.rb', line 39

def cache_or_attribute
  if EasySerializer.perform_caching && .catch?
    Cacher.new(serializer, ).execute
  else
    serializer.instance_exec object, &.get_value
  end
end

#nested_serialization!(value) ⇒ Object



47
48
49
50
# File 'lib/easy_serializer/attribute_serializer.rb', line 47

def nested_serialization!(value)
  return unless value
  .serialize!(value, serializer)
end

#objectObject



8
9
10
# File 'lib/easy_serializer/attribute_serializer.rb', line 8

def object
  serializer.object
end

#valueObject



12
13
14
# File 'lib/easy_serializer/attribute_serializer.rb', line 12

def value
  value_or_default
end

#value_or_defaultObject



16
17
18
19
20
21
22
23
24
# File 'lib/easy_serializer/attribute_serializer.rb', line 16

def value_or_default
  value = attr_serializer
  # Check if value is nil
  # eval option default ( in case is block)
  if value.nil?
    return .get_default(object, serializer)
  end
  value
end