Call now only yields the environment

This commit is contained in:
Mitchell Hashimoto 2012-07-27 19:34:46 -07:00
parent 118377e6f0
commit e5f250121a
3 changed files with 16 additions and 39 deletions

View File

@ -3,17 +3,16 @@ module Vagrant
module Builtin module Builtin
# This middleware class allows a sort of "conditional" run within # This middleware class allows a sort of "conditional" run within
# a single middlware sequence. It takes another middleware runnable, # a single middlware sequence. It takes another middleware runnable,
# runs it with the same environment, then yields the result to a block, # runs it with the same environment, then yields the resulting env to a block,
# allowing that block to determine the next course of action in the # allowing that block to determine the next course of action in the
# middleware sequence. # middleware sequence.
# #
# The first argument to this middleware sequence is anywhere middleware # The first argument to this middleware sequence is anywhere middleware
# runnable, whether it be a class, lambda, or something else that # runnable, whether it be a class, lambda, or something else that
# responds to `call`. This middleware runnable is run with the same # responds to `call`. This middleware runnable is run with the same
# environment as this class. The "result" of the run is expected to be # environment as this class.
# placed in `env[:result]`.
# #
# After running, {Call} takes `env[:result]` and yields it to a block # After running, {Call} takes the environment and yields it to a block
# given to initialize the class, along with an instance of {Builder}. # given to initialize the class, along with an instance of {Builder}.
# The result is used to build up a new sequence on the given builder. # The result is used to build up a new sequence on the given builder.
# This builder is then run. # This builder is then run.
@ -40,7 +39,7 @@ module Vagrant
# Build our new builder based on the result # Build our new builder based on the result
builder = Builder.new builder = Builder.new
@block.call(new_env, new_env[:result], builder) @block.call(new_env, builder)
# Run the result with our new environment # Run the result with our new environment
final_env = runner.run(builder, new_env) final_env = runner.run(builder, new_env)

View File

@ -16,25 +16,25 @@ module VagrantPlugins
def self.action_destroy def self.action_destroy
Vagrant::Action::Builder.new.tap do |b| Vagrant::Action::Builder.new.tap do |b|
b.use CheckVirtualbox b.use CheckVirtualbox
b.use Call, Created do |env, created, b2| b.use Call, Created do |env1, b2|
if created if env1[:result]
# If the VM is created, then we confirm that we want to # If the VM is created, then we confirm that we want to
# destroy it. # destroy it.
message = I18n.t("vagrant.commands.destroy.confirmation", message = I18n.t("vagrant.commands.destroy.confirmation",
:name => env[:machine].name) :name => env[:machine].name)
confirm = Vagrant::Action::Builder.build(Confirm, message) confirm = Vagrant::Action::Builder.build(Confirm, message)
b2.use Call, confirm do |_env, confirmed, b3| b2.use Call, confirm do |env2, b3|
if confirmed if env2[:result]
b3.use Vagrant::Action::General::Validate b3.use Vagrant::Action::General::Validate
b3.use CheckAccessible b3.use CheckAccessible
else else
env[:ui].info I18n.t("vagrant.commands.destroy.will_not_destroy", env2[:ui].info I18n.t("vagrant.commands.destroy.will_not_destroy",
:name => env[:machine.name]) :name => env[:machine.name])
end end
end end
else else
env[:ui].info I18n.t("vagrant.commands.common.vm_not_created") env1[:ui].info I18n.t("vagrant.commands.common.vm_not_created")
end end
end end
end end

View File

@ -4,36 +4,25 @@ describe Vagrant::Action::Builtin::Call do
let(:app) { lambda { |env| } } let(:app) { lambda { |env| } }
let(:env) { {} } let(:env) { {} }
it "should yield the result to the block" do it "should yield the env to the block" do
received = nil received = nil
callable = lambda do |env| callable = lambda do |env|
env[:result] = "value" env[:result] = "value"
end end
described_class.new(app, env, callable) do |_env, result, builder| described_class.new(app, env, callable) do |env, builder|
received = result received = env[:result]
end.call({}) end.call({})
received.should == "value" received.should == "value"
end end
it "should give a nil result if no result is given" do
received = 42
callable = lambda { |env| }
described_class.new(app, env, callable) do |_env, result, builder|
received = result
end.call({})
received.should be_nil
end
it "should call the callable with the original environment" do it "should call the callable with the original environment" do
received = nil received = nil
callable = lambda { |env| received = env[:foo] } callable = lambda { |env| received = env[:foo] }
described_class.new(app, env, callable) do |_env, result, builder| described_class.new(app, env, callable) do |_env, _builder|
# Nothing. # Nothing.
end.call({ :foo => :bar }) end.call({ :foo => :bar })
@ -45,7 +34,7 @@ describe Vagrant::Action::Builtin::Call do
callable = lambda { |env| } callable = lambda { |env| }
next_step = lambda { |env| received = "value" } next_step = lambda { |env| received = "value" }
described_class.new(app, env, callable) do |_env, result, builder| described_class.new(app, env, callable) do |_env, builder|
builder.use next_step builder.use next_step
end.call({}) end.call({})
@ -57,21 +46,10 @@ describe Vagrant::Action::Builtin::Call do
callable = lambda { |env| } callable = lambda { |env| }
next_step = lambda { |env| received = env[:foo] } next_step = lambda { |env| received = env[:foo] }
described_class.new(app, env, callable) do |_env, result, builder| described_class.new(app, env, callable) do |_env, builder|
builder.use next_step builder.use next_step
end.call({ :foo => :bar }) end.call({ :foo => :bar })
received.should == :bar received.should == :bar
end end
it "should yield the environment" do
received = nil
callable = lambda { |env| env[:foo] = "value" }
described_class.new(app, env, callable) do |env, _result, _builder|
received = env[:foo]
end.call({})
received.should == "value"
end
end end