module Teckel::Operation::ClassMethods

Public Instance Methods

call(input = nil) click to toggle source

Invoke the Operation

@param input Any form of input your {Teckel::Operation::Config#input input} class can handle via the given

{Teckel::Operation::Config#input_constructor input_constructor}

@return Either An instance of your defined {Teckel::Operation::Config#error error} class or

{Teckel::Operation::Config#output output} class

@!visibility public

# 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
none() click to toggle source

Convenience method for setting {Teckel::Operation::Config#input input}, {Teckel::Operation::Config#output output} or {Teckel::Operation::Config#error error} to the {Teckel::Contracts::None} value.

@return [Object] The {Teckel::Contracts::None} class.

@example 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
# File lib/teckel/operation.rb, line 147
def none
  Teckel::Contracts::None
end
set(input)
Alias for: with
with(input) click to toggle source

Provide {InstanceMethods#settings() 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}.

@param input Any form of input your {Teckel::Operation::Config#settings settings} class can handle via the given

{Teckel::Operation::Config#settings_constructor settings_constructor}

@return [Class] The configured {Teckel::Operation::Config#runner runner} @!visibility public

@example 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 #=> []
# File lib/teckel/operation.rb, line 114
def with(input)
  runner.new(self, settings_constructor.call(input))
end
Also aliased as: set