Class: Ballast::OperationsChain

Inherits:
Operation show all
Includes:
Interactor::Organizer
Defined in:
lib/ballast/operations_chain.rb

Overview

A operation made of several operation run sequentially passing the common context. The chain will stop on the first failure.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Operation

#fail!, #import_error, #import_response, #in_em_thread, #method_missing, #perform_with_handling, #resolve_error, #setup_response

Constructor Details

- (OperationsChain) initialize(operations, context)

Creates a new chain.

Parameters:

  • operations (Array)

    The list of operations to perform.

  • context (Context)

    The context for the chain.



32
33
34
35
# File 'lib/ballast/operations_chain.rb', line 32

def initialize(operations, context)
  @context = context
  @operations = operations.ensure_array
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Ballast::Operation

Instance Attribute Details

- (Array) operations (readonly)

Returns The list of operations performed.

Returns:

  • (Array)

    The list of operations performed.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ballast/operations_chain.rb', line 11

class OperationsChain < Operation
  include ::Interactor::Organizer
  attr_reader :operations

  # Performs the chain.
  #
  # @param argument [Object|Context] If is a context, then it will be the context of the operation, unless a blank a context with the object
  #   as owner will be created.
  # @param operations [Array] The list of operations to perform.
  # @param context [NilClass] The context for the operation. *Ignored if `owner_or_context` is a context.*
  # @param params [Hash] The additional parameters for the new context. *Ignored if `owner_or_context` is a context.*
  # @return [Operation] The performed chain.
  def self.perform(argument, operations, context: nil, params: {})
    argument = (context || ::Ballast::Context.build(argument, params)) if !argument.is_a?(::Ballast::Context)
    new(operations, argument).tap(&:perform)
  end

  # Creates a new chain.
  #
  # @param operations [Array] The list of operations to perform.
  # @param context [Context] The context for the chain.
  def initialize(operations, context)
    @context = context
    @operations = operations.ensure_array
  end

  private
    # Returns the operations to perform.
    #
    # @return [Array] An array of operations to perform.
    def interactors
      @operations
    end
end

Class Method Details

+ (Operation) perform(argument, operations, context: nil, params: {})

Performs the chain.

Parameters:

  • argument (Object|Context)

    If is a context, then it will be the context of the operation, unless a blank a context with the object as owner will be created.

  • operations (Array)

    The list of operations to perform.

  • context (NilClass)

    The context for the operation. Ignored if owner_or_context is a context.

  • params (Hash)

    The additional parameters for the new context. Ignored if owner_or_context is a context.

Returns:



23
24
25
26
# File 'lib/ballast/operations_chain.rb', line 23

def self.perform(argument, operations, context: nil, params: {})
  argument = (context || ::Ballast::Context.build(argument, params)) if !argument.is_a?(::Ballast::Context)
  new(operations, argument).tap(&:perform)
end