Class: Procto

Inherits:
Module
  • Object
show all
Defined in:
lib/procto.rb,
lib/procto/version.rb

Constant Summary collapse

DEFAULT_NAME =

The default name of the instance method to be called

:call
VERSION =

Gem version

'0.0.2'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a new instance

Parameters:

  • name (Symbol)

    the name of the instance method to call



61
62
63
# File 'lib/procto.rb', line 61

def initialize(name)
  @block = ->(*args) { new(*args).public_send(name) }
end

Class Method Details

.call(name = DEFAULT_NAME) ⇒ Procto

Return a module that turns the host into a method object

Examples:

without a name


class Greeter
  include Procto.call

  def initialize(text)
    @text = text
  end

  def call
    "Hello #{text}"
  end
end

Greeter.call('world') # => "Hello world"

with a name


class Printer
  include Procto.call(:print)

  def initialize(text)
    @text = text
  end

  def print
    "Hello #{text}"
  end
end

Printer.call('world') # => "Hello world"

Parameters:

  • name (#to_sym) (defaults to: DEFAULT_NAME)

    the name of the instance method to call

Returns:



49
50
51
# File 'lib/procto.rb', line 49

def self.call(name = DEFAULT_NAME)
  new(name.to_sym)
end