lib/neco/container.rb
# 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