Class: Porridge::Extractor

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

Overview

Extractor is the nominal base class for all porridge value extractors.

A (value) extractor is an object that is capable of retrieving a value from an object, given a set of “options”, which may be application-specific. You are encouraged, but not required, to have your extractors derive from this class. Currently, any object that implements a #call method is a valid extractor.

Direct Known Subclasses

SendExtractor, SerializingExtractor

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ensure_valid!(*objects) ⇒ Boolean

Ensures that all the provided objects are valid extractors, raising InvalidExtractorError if not.

Parameters:

  • objects (Array)

    the splatted array of objects to validate.

Returns:

  • (Boolean)

    true if all the objects were valid; raises an error otherwise.

Raises:



22
23
24
25
# File 'lib/porridge/extractor.rb', line 22

def self.ensure_valid!(*objects)
  objects.each { |object| raise InvalidExtractorError unless valid?(object) }
  true
end

.valid?(object) ⇒ Boolean

Determines whether the given object is a valid porridge extractor. Currently, any object that responds to the #call method is valid.

Parameters:

  • object

    the object to check.

Returns:

  • (Boolean)

    true if the object is a valid extractor; false otherwise.



14
15
16
# File 'lib/porridge/extractor.rb', line 14

def self.valid?(object)
  object.respond_to? :call
end

Instance Method Details

#call(object, options) ⇒ Object

Should extract a value from the given object with the given options. Subclasses should override this method.

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.



31
# File 'lib/porridge/extractor.rb', line 31

def call(object, options); end