Sha256: 4e136b055a52806b5989cd3b3a27db8bfe2a7c58be84893ee1988c8945f3ce11

Contents?: true

Size: 813 Bytes

Versions: 1

Compression:

Stored size: 813 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|
        command.call(*args, **@environment)
        @called << command
      rescue StandardError
        @called.reverse_each(&:revert)
        break
      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.1.0 lib/neco/container.rb