Module: Teckel::Chain::ClassMethods

Defined in:
lib/teckel/chain.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#runner()Class (readonly, protected)

Returns The Runner class.

Returns:

  • (Class)

    The Runner class



# File 'lib/teckel/chain.rb', line 323

Instance Method Details

#around(callable = nil, &block) ⇒ Proc, {#call}

Set or get the optional around hook. A Hook might be given as a block or anything callable. The execution of the chain is yielded to this hook. The first argument being the callable chain (Runner) and the second argument the input data. The hook also needs to return the result.

Examples:

Around hook with block

OUTPUTS = []

class Echo
  include ::Teckel::Operation::Results

  input Hash
  output input

  def call(hsh)
    hsh
  end
end

class MyChain
  include Teckel::Chain

  around do |chain, input|
    OUTPUTS << "before start"
    result = chain.call(input)
    OUTPUTS << "after start"
    result
  end

  step :noop, Echo
end

result = MyChain.call(some: 'test')
OUTPUTS #=> ["before start", "after start"]
result.success #=> { some: "test" }

Parameters:

  • callable (Proc, {#call}) (defaults to: nil)

    The hook to pass chain execution control to. (nil)

Returns:

  • (Proc, {#call})

    The configured hook



319
320
321
# File 'lib/teckel/chain.rb', line 319

def around(callable = nil, &block)
  @config.for(:around, callable || block)
end

#call(input) ⇒ Teckel::Result, Teckel::Chain::StepFailure

The primary interface to call the chain with the given input.

Parameters:

  • input

    Any form of input the first steps input class can handle

Returns:



341
342
343
344
345
346
347
348
# File 'lib/teckel/chain.rb', line 341

def call(input)
  runner = self.runner.new(@steps)
  if around
    around.call(runner, input)
  else
    runner.call(input)
  end
end

#cloneObject



370
371
372
373
374
375
376
377
378
379
# File 'lib/teckel/chain.rb', line 370

def clone
  if frozen?
    super
  else
    super.tap do |copy|
      copy.instance_variable_set(:@steps, @steps.dup)
      copy.instance_variable_set(:@config, @config.dup)
    end
  end
end

#dupObject



362
363
364
365
366
367
# File 'lib/teckel/chain.rb', line 362

def dup
  super.tap do |copy|
    copy.instance_variable_set(:@steps, @steps.dup)
    copy.instance_variable_set(:@config, @config.dup)
  end
end

#errors<Class>

List of all possible errors

Returns:

  • (<Class>)

    List of all steps Operation.errors



262
263
264
265
266
267
# File 'lib/teckel/chain.rb', line 262

def errors
  @steps.each_with_object([]) do |e, m|
    err = e.last&.error
    m << err if err
  end
end

#finalize!self

Disallow any further changes to this Chain.

Returns:

  • (self)

    Frozen self



354
355
356
357
358
359
# File 'lib/teckel/chain.rb', line 354

def finalize!
  freeze
  @steps.freeze
  @config.freeze
  self
end

#inputClass

The expected input for this chain

Returns:

  • (Class)

    The Operation.input of the first step



250
251
252
# File 'lib/teckel/chain.rb', line 250

def input
  @steps.first&.last&.input
end

#outputClass

The expected output for this chain

Returns:

  • (Class)

    The Operation.output of the last step



256
257
258
# File 'lib/teckel/chain.rb', line 256

def output
  @steps.last&.last&.output
end

#runner(klass = nil) ⇒ Object (protected)

Overwrite the default runner

Parameters:

  • klass (Class) (defaults to: nil)

    A class like the Runner



330
331
332
# File 'lib/teckel/chain.rb', line 330

def runner(klass = nil)
  @config.for(:runner, klass) { Runner }
end

#step(name, operation) ⇒ Object

Declare a Operation as a named step

Parameters:

  • name (String, Symbol)

    The name of the operation. This name is used in an error case to let you know which step failed.

  • operation (Operation::Results)

    The operation to call. Must return a Result object.



275
276
277
# File 'lib/teckel/chain.rb', line 275

def step(name, operation)
  @steps << [name, operation]
end