diff --git a/lib/vagrant/action.rb b/lib/vagrant/action.rb new file mode 100644 index 000000000..adc8eb76b --- /dev/null +++ b/lib/vagrant/action.rb @@ -0,0 +1,26 @@ +module Vagrant + class Action + class << self + # Returns the list of registered actions. + def actions + @actions ||= {} + end + + # Registers an action and associates it with a symbol. This + # symbol can then be referenced in other action builds and + # callbacks can be registered on that symbol. + # + # @param [Symbol] key + def register(key, callable) + @actions[key] = callable + end + + # Runs a registered action with the given key. + # + # @param [Symbol] key + def run(key) + @actions[key].call + end + end + end +end diff --git a/lib/vagrant/action/builder.rb b/lib/vagrant/action/builder.rb new file mode 100644 index 000000000..4be32d2fd --- /dev/null +++ b/lib/vagrant/action/builder.rb @@ -0,0 +1,27 @@ +module Vagrant + class Action + class Builder + def initialize(&block) + instance_eval(&block) if block_given? + end + + def stack + @stack ||= [] + end + + def use(middleware, *args, &block) + stack << [middleware, args, block] + end + + def to_app + inner = @ins.last + + @ins[0...-1].reverse.inject(inner) { |a,e| e.call(a) } + end + + def call(env) + to_app.call(env) + end + end + end +end diff --git a/lib/vagrant/actions/middleware_stack.rb b/lib/vagrant/actions/middleware_stack.rb new file mode 100644 index 000000000..f28bf16b7 --- /dev/null +++ b/lib/vagrant/actions/middleware_stack.rb @@ -0,0 +1,22 @@ +module Vagrant + module Actions + # Represents a middleware stack for Vagrant actions. Vagrant + # actions are created and can be extended with middlewares. + # + # The exact nature of how this will work is not set in stone. + class MiddlewareStack + # Initializes the middleware stack with the given name. + def initialize(key) + @stack = [] + end + + def use(klass) + @stack << klass + end + + def run(endpoint) + @stack << endpoint + end + end + end +end diff --git a/test/vagrant/action/builder_test.rb b/test/vagrant/action/builder_test.rb new file mode 100644 index 000000000..ad6f8d9f3 --- /dev/null +++ b/test/vagrant/action/builder_test.rb @@ -0,0 +1,42 @@ +require File.join(File.dirname(__FILE__), '..', '..', 'test_helper') + +class ActionBuilderTest < Test::Unit::TestCase + setup do + @klass = Vagrant::Action::Builder + end + + context "initializing" do + should "setup empty middleware stack" do + builder = @klass.new + assert builder.stack.empty? + end + + should "take block to setup stack" do + builder = @klass.new do + use Hash + use lambda { |i| i } + end + + assert !builder.stack.empty? + assert_equal 2, builder.stack.length + end + end + + context "with an instance" do + setup do + @instance = @klass.new + end + + context "adding to the stack" do + should "add to the end" do + @instance.use 1 + @instance.use 2 + assert_equal [2, [], nil], @instance.stack.last + end + end + + context "converting to an app" do + + end + end +end