Class: Substation::Chain::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/substation/chain/definition.rb

Overview

Encapsulates an ordered list of Processor instances to be used within an instance of Substation::Chain

Constant Summary collapse

EMPTY =

An empty instance

new(nil, EMPTY_ARRAY).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, processors) ⇒ 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:

  • processors (Enumerable<#call>)

    the processors to be used within this instance


36
37
38
39
# File 'lib/substation/chain/definition.rb', line 36

def initialize(name, processors)
  @name, @processors = name, []
  processors.each(&method(:<<))
end

Instance Attribute Details

#nameSymbol (readonly)

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.

The name of the chain

Returns:

  • (Symbol)

18
19
20
# File 'lib/substation/chain/definition.rb', line 18

def name
  @name
end

Instance Method Details

#<<(processor) ⇒ self

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.

Append processor to #processors

Parameters:

  • processor (#call)

    the processor to append

Returns:

  • (self)

49
50
51
52
53
# File 'lib/substation/chain/definition.rb', line 49

def <<(processor)
  raise_duplicate_processor_error([processor]) if include?(processor)
  processors << processor
  self
end

#prepend(other) ⇒ Definition

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.

Returns a new instance with other's processors prepended

Parameters:

  • other (Definition)

    the definition to prepend

Returns:


85
86
87
88
89
# File 'lib/substation/chain/definition.rb', line 85

def prepend(other)
  duplicates = processors & other.processors
  raise_duplicate_processor_error(duplicates) if duplicates.any?
  self.class.new(name, other.processors | processors)
end

#replace_failure_chain(processor_name, failure_chain) ⇒ self

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.

Replace the failure chain of the processor identified by name

Parameters:

  • processor_name (Symbol)

    the name of the processor

  • failure_chain (Chain)

    the failure chain to use in the replaced processor

Returns:

  • (self)

71
72
73
74
75
# File 'lib/substation/chain/definition.rb', line 71

def replace_failure_chain(processor_name, failure_chain)
  idx = fetch(processor_name)
  processors[idx] = processors.at(idx).with_failure_chain(failure_chain)
  self
end