Class: Porridge::SerializingExtractor

Inherits:
Extractor
  • Object
show all
Defined in:
lib/porridge/serializing_extractor.rb

Overview

SerializingExtractor is an extractor that wraps another extractor and serializes for its output using a provided serializer. Note that SerializingExtractor passes the output of the base extractor as the object for the serializer, not the input.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Extractor

ensure_valid!, valid?

Constructor Details

#initialize(base, serializer) ⇒ SerializingExtractor

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

Parameters:

  • base (Extractor, #call)

    the extractor to wrap, whose output will be serialized for before being returned.

  • serializer (Serializer, #call)

    the serializer to use to transform the output of the extractor.

Raises:



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

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

Instance Attribute Details

#baseExtractor, #call (readonly, private)

The base extractor.

Returns:



36
37
38
# File 'lib/porridge/serializing_extractor.rb', line 36

def base
  @base
end

#serializerSerializer, #call (readonly, private)

The serializer to use to serialize for the output of the base extractor.

Returns:



40
41
42
# File 'lib/porridge/serializing_extractor.rb', line 40

def serializer
  @serializer
end

Instance Method Details

#call(object, options) ⇒ Object

Extracts a value from the given object for the given options by:

1. Using the base extractor ({#extractor}) to extract a value from the object with the given options; and
2. Passing that value as the object to {#serializer#call}. A blank hash is given as the input, and the
   given options are passed along.

Parameters:

  • object

    the object from which to retrieve the value.

  • options (Hash)

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

Returns:

  • the extracted value.



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

def call(object, options)
  serializer.call(base.call(object, options), {}, options)
end