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



68
69
70
71
72
73
74
75
76
# File 'lib/teckel/operation.rb', line 68

def call(input = nil)
  default_settings = self.default_settings

  if default_settings
    runner.new(self, default_settings.call)
  else
    runner.new(self)
  end.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:



147
148
149
# File 'lib/teckel/operation.rb', line 147

def none
  Teckel::Contracts::None
end

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

Provide settings to the running operation.

This method is intended to be called on the operation class outside of it’s definition, prior to running #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



114
115
116
# File 'lib/teckel/operation.rb', line 114

def with(input)
  runner.new(self, settings_constructor.call(input))
end