Class: Porridge::SendExtractor

Inherits:
Extractor show all
Defined in:
lib/porridge/send_extractor.rb

Overview

SendExtractor is an extractor that retrieves a value from an object by simply calling a predefined method on it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Extractor

ensure_valid!, valid?

Constructor Details

#initialize(method_name) ⇒ SendExtractor

Creates a new instance of Porridge::SendExtractor with the given method name.

Parameters:

  • method_name (String, Symbol)

    the name of the method to call when extracting the value.



8
9
10
11
# File 'lib/porridge/send_extractor.rb', line 8

def initialize(method_name)
  @method_name = method_name.to_s
  super()
end

Instance Attribute Details

#method_nameString (readonly, private)

The name of the method to call when extracting the value.

Returns:

  • (String)


25
26
27
# File 'lib/porridge/send_extractor.rb', line 25

def method_name
  @method_name
end

Instance Method Details

#call(object, _options) ⇒ Object

Extracts the value from the given object by sending the method name (#method_name) to it.

Parameters:

  • object

    the object from which to retrieve the value.

  • _options (Hash)

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

Returns:

  • the extracted value, as returned from the sent method.



17
18
19
# File 'lib/porridge/send_extractor.rb', line 17

def call(object, _options)
  object.respond_to?(method_name) ? object.send(method_name) : nil
end