Class: SoberSwag::Serializer::Array

Inherits:
Base
  • Object
show all
Defined in:
lib/sober_swag/serializer/array.rb

Overview

Transform a serializer that works on elements to one that works on Arrays

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#array, #identifier, #meta, #optional, #serializer, #via_map

Constructor Details

#initialize(element_serializer) ⇒ Array

Make an array serializer out of another serializer.

Parameters:



9
10
11
# File 'lib/sober_swag/serializer/array.rb', line 9

def initialize(element_serializer)
  @element_serializer = element_serializer
end

Instance Attribute Details

#element_serializerSoberSwag::Serializer::Base (readonly)

The serializer that will be used for each element in the array.



17
18
19
# File 'lib/sober_swag/serializer/array.rb', line 17

def element_serializer
  @element_serializer
end

Instance Method Details

#finalize_lazy_type!Object

Delegates to #element_serializer



33
34
35
# File 'lib/sober_swag/serializer/array.rb', line 33

def finalize_lazy_type!
  @element_serializer.finalize_lazy_type!
end

#lazy_typeObject

Delegates to #element_serializer, wrapped in an array



27
28
29
# File 'lib/sober_swag/serializer/array.rb', line 27

def lazy_type
  SoberSwag::Types::Array.of(@element_serializer.lazy_type)
end

#lazy_type?Boolean

Delegates to #element_serializer

Returns:

  • (Boolean)


21
22
23
# File 'lib/sober_swag/serializer/array.rb', line 21

def lazy_type?
  @element_serializer.lazy_type?
end

#serialize(object, options = {}) ⇒ Array<Object>

Serialize an array of objects that can be serialized with #element_serializer by calling element_serializer.serialize for each item in this array.

Note: since ruby is duck-typed, anything that responds to the #map method from Enumerable should work!

Parameters:

  • object (Array<Object>, #map)

    collection of objects to serialize

Returns:

  • (Array<Object>)

    JSON-compatible array



46
47
48
# File 'lib/sober_swag/serializer/array.rb', line 46

def serialize(object, options = {})
  object.map { |a| element_serializer.serialize(a, options) }
end

#typeObject

The type of items returned from #serialize



52
53
54
# File 'lib/sober_swag/serializer/array.rb', line 52

def type
  SoberSwag::Types::Array.of(element_serializer.type)
end