Class: SoberSwag::Serializer::Array
- 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
-
#element_serializer ⇒ SoberSwag::Serializer::Base
readonly
The serializer that will be used for each element in the array.
Instance Method Summary collapse
-
#finalize_lazy_type! ⇒ Object
Delegates to #element_serializer.
-
#initialize(element_serializer) ⇒ Array
constructor
Make an array serializer out of another serializer.
-
#lazy_type ⇒ Object
Delegates to #element_serializer, wrapped in an array.
-
#lazy_type? ⇒ Boolean
Delegates to #element_serializer.
-
#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. -
#type ⇒ Object
The type of items returned from #serialize.
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.
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_serializer ⇒ SoberSwag::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_type ⇒ Object
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
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!
46 47 48 |
# File 'lib/sober_swag/serializer/array.rb', line 46 def serialize(object, = {}) object.map { |a| element_serializer.serialize(a, ) } end |
#type ⇒ Object
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 |