Sha256: e5da95195c9472ac4fb1b8579535773fedbc16dd6de95ba7da868c91d153c69a

Contents?: true

Size: 1.02 KB

Versions: 62

Compression:

Stored size: 1.02 KB

Contents

module Workarea
  class Payment
    class Operation
      attr_reader :transactions, :errors

      def initialize(transactions, options = {})
        @transactions = transactions
        @options = options
        @errors = []
      end

      def success?
        errors.empty?
      end

      def complete!
        begin
          complete_each_transaction!
        rescue Exception => e
          rollback!
          raise e
        ensure
          add_transaction_errors
        end
      end

      def rollback!
        transactions
          .select(&:success?)
          .reject(&:canceled?)
          .each { |t| t.cancel!(@options) }
      end

      private

      def complete_each_transaction!
        transactions.each do |transaction|
          transaction.complete!(@options)
          rollback! and break if transaction.failure?
        end
      end

      def add_transaction_errors
        transactions.select(&:failure?).each do |transaction|
          errors << transaction.message
        end
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.15 app/models/workarea/payment/operation.rb
workarea-core-3.4.36 app/models/workarea/payment/operation.rb
workarea-core-3.5.14 app/models/workarea/payment/operation.rb
workarea-core-3.4.35 app/models/workarea/payment/operation.rb
workarea-core-3.5.13 app/models/workarea/payment/operation.rb
workarea-core-3.4.34 app/models/workarea/payment/operation.rb
workarea-core-3.5.12 app/models/workarea/payment/operation.rb
workarea-core-3.4.33 app/models/workarea/payment/operation.rb
workarea-core-3.5.11 app/models/workarea/payment/operation.rb
workarea-core-3.5.10 app/models/workarea/payment/operation.rb
workarea-core-3.4.32 app/models/workarea/payment/operation.rb
workarea-core-3.5.9 app/models/workarea/payment/operation.rb
workarea-core-3.4.31 app/models/workarea/payment/operation.rb
workarea-core-3.5.8 app/models/workarea/payment/operation.rb
workarea-core-3.4.30 app/models/workarea/payment/operation.rb
workarea-core-3.5.7 app/models/workarea/payment/operation.rb
workarea-core-3.4.29 app/models/workarea/payment/operation.rb
workarea-core-3.5.6 app/models/workarea/payment/operation.rb
workarea-core-3.4.28 app/models/workarea/payment/operation.rb
workarea-core-3.5.5 app/models/workarea/payment/operation.rb