Sha256: a21911b744a2a02fcd6f7aca1663786028507820229c82679a38f6c5c8cf6d53

Contents?: true

Size: 853 Bytes

Versions: 1

Compression:

Stored size: 853 Bytes

Contents

# frozen_string_literal: true

module Neco
  # Container has two purposes.
  # One is to store commands and execute rollbacks when one command raises an exception.
  # Another is to store environment hash for those commands so that commands can pass data between them.
  class Container
    def initialize(commands: [], environment: {})
      @commands = commands.map {|command| command.new(container: self) }
      @environment = environment
      @called = []
    end

    def call(*args, **params)
      @environment.merge!(params)
      @commands.each do |command|
        result = command.call(*args, **@environment)
        if result.success?
          @called << command
        else
          @called.reverse_each(&:revert)
          break
        end
      end
    end

    def set(key, value)
      @environment[key] = value
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
neco-0.2.0 lib/neco/container.rb