Added the Confirm built-in middleware
This middleware asks the user a question and expects a Y/N answer. This middleware can be used with the Call middleware.
This commit is contained in:
parent
556a53d48d
commit
e20326d577
|
@ -16,6 +16,7 @@ module Vagrant
|
||||||
# and are thus available to all plugins as a "standard library" of sorts.
|
# and are thus available to all plugins as a "standard library" of sorts.
|
||||||
module Builtin
|
module Builtin
|
||||||
autoload :Call, "vagrant/action/builtin/call"
|
autoload :Call, "vagrant/action/builtin/call"
|
||||||
|
autoload :Confirm, "vagrant/action/builtin/confirm"
|
||||||
end
|
end
|
||||||
|
|
||||||
module Env
|
module Env
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
module Vagrant
|
||||||
|
module Action
|
||||||
|
module Builtin
|
||||||
|
# This class asks the user to confirm some sort of question with
|
||||||
|
# a "Y/N" question. The only parameter is the text to ask the user.
|
||||||
|
# The result is placed in `env[:result]` so that it can be used
|
||||||
|
# with the {Call} class.
|
||||||
|
class Confirm
|
||||||
|
# For documentation, read the description of the {Confirm} class.
|
||||||
|
#
|
||||||
|
# @param [String] message The message to ask the user.
|
||||||
|
def initialize(app, env, message)
|
||||||
|
@app = app
|
||||||
|
@message = message
|
||||||
|
end
|
||||||
|
|
||||||
|
def call(env)
|
||||||
|
# Ask the user the message and store the result
|
||||||
|
choice = nil
|
||||||
|
choice = env[:ui].ask(@message)
|
||||||
|
env[:result] = choice && choice.upcase == "Y"
|
||||||
|
|
||||||
|
@app.call(env)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,21 @@
|
||||||
|
require File.expand_path("../../../../base", __FILE__)
|
||||||
|
|
||||||
|
describe Vagrant::Action::Builtin::Confirm do
|
||||||
|
let(:app) { lambda { |env| } }
|
||||||
|
let(:env) { { :ui => double("ui") } }
|
||||||
|
let(:message) { "foo" }
|
||||||
|
|
||||||
|
["y", "Y"].each do |valid|
|
||||||
|
it "should set the result to true if '#{valid}' is given" do
|
||||||
|
env[:ui].should_receive(:ask).with(message).and_return(valid)
|
||||||
|
described_class.new(app, env, message).call(env)
|
||||||
|
env[:result].should be
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should set result to false if anything else is given" do
|
||||||
|
env[:ui].should_receive(:ask).with(message).and_return("nope")
|
||||||
|
described_class.new(app, env, message).call(env)
|
||||||
|
env[:result].should_not be
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue