Sha256: c339819e50fe1062872000cef3e7698189b7ae36571995d34be44ab698ee1ceb

Contents?: true

Size: 1.75 KB

Versions: 7

Compression:

Stored size: 1.75 KB

Contents

#
# This file is part of the ballast gem. Copyright (C) 2013 and above Shogun <shogun@cowtech.it>.
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
#

module Ballast
  # A operation made of several operation run sequentially passing the common context. The chain will stop on the first failure.
  #
  # @attribute [r] operations
  #   @return [Array] The list of operations performed.
  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
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ballast-1.9.3 lib/ballast/operations_chain.rb
ballast-1.9.2 lib/ballast/operations_chain.rb
ballast-1.9.1 lib/ballast/operations_chain.rb
ballast-1.9.0 lib/ballast/operations_chain.rb
ballast-1.8.0 lib/ballast/operations_chain.rb
ballast-1.7.0 lib/ballast/operations_chain.rb
ballast-1.6.0 lib/ballast/operations_chain.rb