Sha256: 3517373fe1ad8f8e257555b847373452f41f61599ef6ea8b7218cd5210d98c7a

Contents?: true

Size: 1.04 KB

Versions: 5

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module Porridge
  # {SendExtractor} is an extractor that retrieves a value from an object by simply calling a predefined method on it.
  class SendExtractor < Extractor
    # Creates a new instance of {SendExtractor} with the given method name.
    # @param method_name [String, Symbol] the name of the method to call when extracting the value.
    def initialize(method_name)
      @method_name = method_name.to_s
      super()
    end

    # Extracts the value from the given object by sending the method name ({#method_name}) to it.
    # @param object the object from which to retrieve the value.
    # @param _options [Hash] a hash of "options," which may be application-specific. These options are ignored.
    # @return the extracted value, as returned from the sent method.
    def call(object, _options)
      object.respond_to?(method_name) ? object.send(method_name) : nil
    end

    private

    # The name of the method to call when extracting the value.
    # @return [String]
    attr_reader :method_name
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
porridge-0.3.1 lib/porridge/send_extractor.rb
porridge-0.3.0 lib/porridge/send_extractor.rb
porridge-0.2.2 lib/porridge/send_extractor.rb
porridge-0.2.1 lib/porridge/send_extractor.rb
porridge-0.2.0 lib/porridge/send_extractor.rb