Module: Teckel::Operation::ClassMethods

Defined in:
lib/teckel/operation.rb

Instance Method Summary collapse

Instance Method Details

#call(input = nil) ⇒ Object

Invoke the Operation

Parameters:

Returns:

  • Either An instance of your defined error class or output class



71
72
73
# File 'lib/teckel/operation.rb', line 71

def call(input = nil)
  runable.call(input)
end

#noneObject

Convenience method for setting input, output or error to the Contracts::None value.

Examples:

Enforcing nil input, output or error

class MyOperation
  include Teckel::Operation

  input none

  # same as
  output Teckel::Contracts::None

  error none

  def call(_) # you still need to take than +nil+ input when using `input none`
    # when using `error none`:
    # `fail!` works, but `fail!("data")` raises an error

    # when using `output none`:
    # `success!` works, but `success!("data")` raises an error
  end
end

MyOperation.call #=> nil

Returns:



164
165
166
# File 'lib/teckel/operation.rb', line 164

def none
  Contracts::None
end

#runable(settings = UNDEFINED) ⇒ Class

Note:

This method is public to make testing, stubbing and mocking easier. Your normal application code should use #with and/or #call

Constructs a Runner instance for #call and #with.

Parameters:

Returns:

  • (Class)

    The configured runner



126
127
128
129
130
131
132
133
134
# File 'lib/teckel/operation.rb', line 126

def runable(settings = UNDEFINED)
  if settings != UNDEFINED
    runner.new(self, settings)
  elsif default_settings
    runner.new(self, default_settings.call)
  else
    runner.new(self)
  end
end

#with(settings) ⇒ Class Also known as: set

Provide settings to the operation.

This method is intended to be called on the operation class outside of it’s definition, prior to invoking #call.

Examples:

Inject settings for an operation call

LOG = []

class MyOperation
  include ::Teckel::Operation

  settings Struct.new(:log)

  input none
  output none
  error none

  def call(_input)
    settings.log << "called" if settings&.log
    nil
  end
end

MyOperation.with(LOG).call
LOG #=> ["called"]

LOG.clear

MyOperation.with(false).call
MyOperation.call
LOG #=> []

Parameters:

Returns:

  • (Class)

    The configured runner



111
112
113
# File 'lib/teckel/operation.rb', line 111

def with(settings)
  runable(settings_constructor.call(settings))
end