Modify task DSL API to take env on initialization

This commit is contained in:
Thibault Jouan 2014-01-08 21:55:33 +00:00
parent 356d778746
commit 1d6ae126ed
4 changed files with 20 additions and 16 deletions

View File

@ -3,8 +3,8 @@ module Producer
class Task
class << self
def evaluate(name, env, *args, &block)
dsl = DSL.new(&block)
dsl.evaluate(env, *args)
dsl = DSL.new(env, &block)
dsl.evaluate(*args)
Task.new(name, dsl.actions, dsl.condition)
end
end

View File

@ -15,16 +15,16 @@ module Producer
define_action :file_write, Actions::FileWriter
attr_accessor :actions
attr_accessor :env, :actions
def initialize(&block)
def initialize(env, &block)
@env = env
@block = block
@actions = []
@condition = true
end
def evaluate(env, *args)
@env = env
def evaluate(*args)
instance_exec *args, &@block
end

View File

@ -4,7 +4,7 @@ module Producer::Core
describe Task::DSL do
let(:block) { proc { } }
let(:env) { double 'env' }
subject(:dsl) { Task::DSL.new(&block) }
subject(:dsl) { Task::DSL.new(env, &block) }
%w[echo sh file_write].each do |action|
it "has `#{action}' action defined" do
@ -20,6 +20,10 @@ module Producer::Core
end
describe '#initialize' do
it 'assigns the given env' do
expect(dsl.env).to be env
end
it 'assigns no action' do
expect(dsl.actions).to be_empty
end
@ -49,7 +53,7 @@ module Producer::Core
let(:block) { proc { throw :task_code } }
it 'evaluates its code' do
expect { dsl.evaluate env }
expect { dsl.evaluate }
.to throw_symbol :task_code
end
@ -57,7 +61,7 @@ module Producer::Core
let(:block) { proc { |e| throw e } }
it 'passes arguments as block parameters' do
expect { dsl.evaluate env, :some_argument }
expect { dsl.evaluate :some_argument }
.to throw_symbol :some_argument
end
end
@ -68,7 +72,7 @@ module Producer::Core
before do
Task::DSL.define_action(:some_action, some_action_class)
dsl.evaluate env
dsl.evaluate
end
it 'registers the action' do
@ -82,7 +86,7 @@ module Producer::Core
end
context 'DSL specific methods' do
subject(:dsl) { Task::DSL.new(&block).evaluate(env) }
subject(:dsl) { Task::DSL.new(env, &block).evaluate }
describe '#condition' do
context 'when a block is given' do
@ -90,7 +94,7 @@ module Producer::Core
it 'builds a new evaluated condition' do
expect(Condition)
.to receive(:evaluate).with(env) do |&b|
.to receive :evaluate do |&b|
expect(b.call).to eq :some_value
end
dsl

View File

@ -12,19 +12,19 @@ module Producer::Core
let(:args) { [:some, :arguments] }
let(:block) { proc { :some_task_code } }
it 'builds a new DSL sandbox with given code' do
it 'builds a new DSL sandbox with given env and code' do
dsl = double('dsl').as_null_object
expect(Task::DSL).to receive(:new).with(no_args) do |&b|
expect(Task::DSL).to receive(:new).with(env) do |&b|
expect(b).to be block
dsl
end
Task.evaluate(name, env, *args, &block)
end
it 'evaluates the DSL sandbox code with given environment' do
it 'evaluates the DSL sandbox code with given arguments' do
dsl = double('dsl').as_null_object
allow(Task::DSL).to receive(:new) { dsl }
expect(dsl).to receive(:evaluate).with(env, *args)
expect(dsl).to receive(:evaluate).with(*args)
Task.evaluate(name, env, *args, &block)
end