Modify recipe DSL API to take env on initialization

This commit is contained in:
Thibault Jouan 2014-01-08 22:11:38 +00:00
parent 1d6ae126ed
commit 352fcd37f8
3 changed files with 19 additions and 19 deletions

View File

@ -3,7 +3,7 @@ module Producer
class Recipe
class << self
def evaluate_from_file(filepath, env)
dsl = DSL.new(File.read(filepath)).evaluate(env)
dsl = DSL.new(env, File.read(filepath)).evaluate
Recipe.new(dsl.tasks)
end
end

View File

@ -2,16 +2,16 @@ module Producer
module Core
class Recipe
class DSL
attr_reader :tasks
attr_reader :env, :tasks
def initialize(code = nil, &block)
def initialize(env, code = nil, &block)
@env = env
@code = code
@block = block
@tasks = []
end
def evaluate(env)
@env = env
def evaluate
if @code
instance_eval @code
else
@ -22,10 +22,6 @@ module Producer
private
def env
@env
end
def source(filepath)
instance_eval File.read("./#{filepath}.rb"), "#{filepath}.rb"
end

View File

@ -6,16 +6,20 @@ module Producer::Core
let(:code) { proc { :some_recipe_code } }
let(:env) { double('env').as_null_object }
subject(:dsl) { Recipe::DSL.new(&code) }
subject(:dsl) { Recipe::DSL.new(env, &code) }
describe '#initialize' do
it 'assigns the given env' do
expect(dsl.env).to be env
end
it 'assigns no task' do
expect(dsl.instance_eval { @tasks }).to be_empty
end
context 'when a string of code is given as argument' do
let(:code) { 'some_code' }
subject(:dsl) { Recipe::DSL.new(code) }
subject(:dsl) { Recipe::DSL.new(env, code) }
it 'assigns the string of code' do
expect(dsl.instance_eval { @code }).to eq code
@ -33,41 +37,41 @@ module Producer::Core
let(:code) { proc { task(:some_task) { } } }
it 'returns registered tasks' do
dsl.evaluate env
dsl.evaluate
expect(dsl.tasks[0].name).to eq :some_task
end
end
describe '#evaluate' do
it 'evaluates its code' do
dsl = Recipe::DSL.new { throw :recipe_code }
expect { dsl.evaluate env }.to throw_symbol :recipe_code
dsl = Recipe::DSL.new(env) { throw :recipe_code }
expect { dsl.evaluate }.to throw_symbol :recipe_code
end
it 'returns itself' do
expect(dsl.evaluate env).to eq dsl
expect(dsl.evaluate).to eq dsl
end
end
context 'DSL specific methods' do
subject(:dsl) { Recipe::DSL.new(&code).evaluate(env) }
subject(:dsl) { Recipe::DSL.new(env, &code).evaluate }
describe '#env' do
let(:code) { proc { env.some_message } }
it 'returns the current environment' do
expect(env).to receive :some_message
dsl.evaluate env
dsl.evaluate
end
end
describe '#source' do
let(:filepath) { fixture_path_for 'recipes/throw' }
let(:code) { "source '#{filepath}'" }
subject(:dsl) { Recipe::DSL.new(code) }
subject(:dsl) { Recipe::DSL.new(env, code) }
it 'sources the recipe given as argument' do
expect { dsl.evaluate env }.to throw_symbol :recipe_code
expect { dsl.evaluate }.to throw_symbol :recipe_code
end
end