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:
parent
37e17d1030
commit
30e1930719
@ -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)
|
||||||
@tasks = []
|
@code = code
|
||||||
if code
|
@block = block
|
||||||
instance_eval code
|
@tasks = []
|
||||||
|
end
|
||||||
|
|
||||||
|
def evaluate
|
||||||
|
if @code
|
||||||
|
instance_eval @code
|
||||||
else
|
else
|
||||||
instance_eval &block
|
instance_eval &@block
|
||||||
end
|
end
|
||||||
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -41,39 +41,65 @@ 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
|
||||||
|
|
||||||
describe '#source' do
|
context 'DSL specific methods' do
|
||||||
let(:code) { "source '#{fixture_path_for 'recipes/error'}'" }
|
subject(:dsl) { Recipe::DSL.new(&code).evaluate }
|
||||||
subject(:dsl) { Recipe::DSL.new code }
|
|
||||||
|
|
||||||
it 'sources the recipe given as argument' do
|
describe '#source' do
|
||||||
expect { dsl }.to raise_error(RuntimeError, 'error from recipe')
|
let(:code) { "source '#{fixture_path_for 'recipes/error'}'" }
|
||||||
|
subject(:dsl) { Recipe::DSL.new code }
|
||||||
|
|
||||||
|
it 'sources the recipe given as argument' do
|
||||||
|
expect { dsl.evaluate }.to raise_error(RuntimeError, 'error from recipe')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
describe '#tasks' do
|
describe '#tasks' do
|
||||||
let(:code) { Proc.new { task(:some_task) } }
|
let(:code) { Proc.new { task(:some_task) } }
|
||||||
|
|
||||||
it 'returns registered tasks' do
|
it 'returns registered tasks' do
|
||||||
expect(dsl.tasks[0].name).to eq :some_task
|
expect(dsl.tasks[0].name).to eq :some_task
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
describe '#task' do
|
describe '#task' do
|
||||||
let(:code) { Proc.new { task(:first); task(:last) } }
|
let(:code) { Proc.new { task(:first); task(:last) } }
|
||||||
|
|
||||||
it 'registers tasks in declaration order' do
|
it 'registers tasks in declaration order' do
|
||||||
expect(dsl.tasks[0].name).to eq :first
|
expect(dsl.tasks[0].name).to eq :first
|
||||||
expect(dsl.tasks[1].name).to eq :last
|
expect(dsl.tasks[1].name).to eq :last
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user