Implement Recipe::DSL#evaluate method:

Move recipe DSL evaluation in a dedicated #evaluate method, instead of
evaluating the code during the DSL instantiation.
This commit is contained in:
Thibault Jouan 2013-07-29 15:27:29 +00:00
parent 37e17d1030
commit 30e1930719
2 changed files with 55 additions and 23 deletions

View File

@ -13,7 +13,7 @@ module Producer
end end
def evaluate def evaluate
dsl = DSL.new(@code) dsl = DSL.new(@code).evaluate
dsl.tasks.each.map(&:evaluate) dsl.tasks.each.map(&:evaluate)
end end
@ -22,12 +22,18 @@ module Producer
attr_reader :tasks attr_reader :tasks
def initialize(code = nil, &block) def initialize(code = nil, &block)
@code = code
@block = block
@tasks = [] @tasks = []
if code
instance_eval code
else
instance_eval &block
end end
def evaluate
if @code
instance_eval @code
else
instance_eval &@block
end
self
end end

View File

@ -41,22 +41,47 @@ module Producer::Core
describe Recipe::DSL do describe Recipe::DSL do
let(:code) { Proc.new { } }
subject(:dsl) { Recipe::DSL.new &code } subject(:dsl) { Recipe::DSL.new &code }
describe '#initialize' do describe '#initialize' do
let(:code) { Proc.new { raise 'error from recipe' } } context 'when a string of code is given as argument' do
subject(:dsl) { Recipe::DSL.new 'nil' }
it 'returns the DSL instance' do
expect(dsl).to be_a Recipe::DSL
end
end
context 'when a code block is given as argument' do
subject(:dsl) { Recipe::DSL.new Proc.new { } }
it 'returns the DSL instance' do
expect(dsl).to be_a Recipe::DSL
end
end
end
describe '#evaluate' do
it 'evaluates its code' do it 'evaluates its code' do
expect { dsl }.to raise_error(RuntimeError, 'error from recipe') dsl = Recipe::DSL.new { raise 'error from recipe' }
expect { dsl.evaluate }.to raise_error(RuntimeError, 'error from recipe')
end
it 'returns itself' do
expect(dsl.evaluate).to eq dsl
end end
end end
context 'DSL specific methods' do
subject(:dsl) { Recipe::DSL.new(&code).evaluate }
describe '#source' do describe '#source' do
let(:code) { "source '#{fixture_path_for 'recipes/error'}'" } let(:code) { "source '#{fixture_path_for 'recipes/error'}'" }
subject(:dsl) { Recipe::DSL.new code } subject(:dsl) { Recipe::DSL.new code }
it 'sources the recipe given as argument' do it 'sources the recipe given as argument' do
expect { dsl }.to raise_error(RuntimeError, 'error from recipe') expect { dsl.evaluate }.to raise_error(RuntimeError, 'error from recipe')
end end
end end
@ -78,4 +103,5 @@ module Producer::Core
end end
end end
end end
end
end end