Class: Porridge::SerializerForExtracted

Inherits:
Serializer
  • Object
show all
Defined in:
lib/porridge/serializer_for_extracted.rb

Overview

SerializerForExtracted is a serializer that wraps another serializer and passes it an object that is extracted from the initial object using an Extractor.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Serializer

ensure_valid!, valid?

Constructor Details

#initialize(base, extractor) ⇒ SerializerForExtracted

Creates a new instance of Porridge::SerializerForExtracted with the given base serializer and extractor.

Parameters:

  • base (Serializer, #call)

    the base serializer to wrap.

  • extractor (Extractor, #call)

    the extractor to use to extract a value from the object before passing it to the base serializer.

Raises:



13
14
15
16
17
18
19
# File 'lib/porridge/serializer_for_extracted.rb', line 13

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

Instance Attribute Details

#baseSerializer, #call (readonly, private)

The base serializer to wrap.

Returns:



37
38
39
# File 'lib/porridge/serializer_for_extracted.rb', line 37

def base
  @base
end

#extractorExtractor, #call (readonly, private)

The extractor to use to extract a value from the object before passing it to the base serializer.

Returns:



41
42
43
# File 'lib/porridge/serializer_for_extracted.rb', line 41

def extractor
  @extractor
end

Instance Method Details

#call(object, input, options) ⇒ Object

Serializes the given input for the given object with the given options by first extracted a value from the given object, then passing that value, along with the given input and options, to the base serializer (#base).

Parameters:

  • object

    the object for which to transform the input. A value will be extracted and that value will be passed to the base serializer.

  • input

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

  • options (Hash)

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

Returns:

  • the transformed output, typically either a hash or an array, as returned from the base serializer.



28
29
30
31
# File 'lib/porridge/serializer_for_extracted.rb', line 28

def call(object, input, options)
  extracted_value = extractor.call(object, options)
  base.call(extracted_value, input, options)
end