Initial foundation building of new action style
This commit is contained in:
parent
febfd81b68
commit
6eefc8e874
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
Loading…
Reference in New Issue