Class: Porridge::ArraySerializer

Inherits:
Serializer show all
Defined in:
lib/porridge/array_serializer.rb

Overview

ArraySerializer is a serializer that wraps another serializer, calling it for every element of the input array, if an array was given, or simply passing it the input if not.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Serializer

ensure_valid!, valid?

Constructor Details

#initialize(base) ⇒ ArraySerializer

Creates a new instance of Porridge::ArraySerializer with the given base serializer.

Parameters:

  • base (Serializer, #call)

    the base serializer to call for any input, or all elements of that input if the input is an array.

Raises:



11
12
13
14
15
# File 'lib/porridge/array_serializer.rb', line 11

def initialize(base)
  Serializer.ensure_valid!(base)
  @base = base
  super()
end

Instance Attribute Details

#baseSerializer, #call (readonly, private)

The base serializer, which will be called for the object, or each object, if an array is given.

Returns:



53
54
55
# File 'lib/porridge/array_serializer.rb', line 53

def base
  @base
end

Instance Method Details

#array?(object) ⇒ Boolean (protected)

Determines whether the given object is an array for the purposes of this Porridge::ArraySerializer instance. The default implementation simple checks to see if the object implements the map method. You may override this method to change the default behavior, if, for example, you have a non-array that implements map.

Parameters:

  • object

    the object to check.

Returns:

  • (Boolean)

    true if the given object functions like an array; false if otherwise.



45
46
47
# File 'lib/porridge/array_serializer.rb', line 45

def array?(object)
  object.is_a? Array
end

#call(object_or_objects, input, options) ⇒ Object+

Serializes the given object, which may be an array, for the given input with the given options. If the object is an array (according to #array?), the base serializer #base will be called for each element, and an array with each result will be returned. If the object is not an array, will simply delegate to #base.

The given object and options will be given to the base serializer for every element. Note that the options are not cloned or duplicated. Therefore the base serializer must not mutate the options object or else the other invocations will also receive the mutated version.

Parameters:

  • object_or_objects (Object, Array<Object>)

    the object or array of objects for which to transform the input.

  • input

    the object being transformed, typically either a hash or an array.

  • options (Hash)

    a hash of “options,” which may be application specific.

Returns:

  • (Object, Array<Object>)

    the transformed output if the object was not an array, or an array of all transformed outputs if the object was an array.



30
31
32
33
34
35
36
# File 'lib/porridge/array_serializer.rb', line 30

def call(object_or_objects, input, options)
  if array?(object_or_objects)
    object_or_objects.map { |object| base.call(object, input, options) }
  else
    base.call(object_or_objects, input, options)
  end
end