Remove most of task evaluation code from Task class, rely on Task::DSL.evaluate to get an evaluated task. * Task: remove #evaluate, change constructor prototype to accept actions instead of a block, implement .evaluate(name, env &block); * Implement Task::DSL.evaluate method; * Recipe::DSL: remove tasks evaluation from#evaluate, modify #task to use Task.evaluate to return the new task to be rigstered.
49 lines
905 B
Ruby
49 lines
905 B
Ruby
module Producer
|
|
module Core
|
|
class Recipe
|
|
class DSL
|
|
attr_reader :tasks
|
|
|
|
def self.evaluate(code, env)
|
|
dsl = new(code).evaluate(env)
|
|
Recipe.new(dsl.tasks)
|
|
end
|
|
|
|
def initialize(code = nil, &block)
|
|
@code = code
|
|
@block = block
|
|
@tasks = []
|
|
end
|
|
|
|
def evaluate(env)
|
|
@env = env
|
|
if @code
|
|
instance_eval @code
|
|
else
|
|
instance_eval &@block
|
|
end
|
|
self
|
|
end
|
|
|
|
private
|
|
|
|
def env
|
|
@env
|
|
end
|
|
|
|
def source(filepath)
|
|
instance_eval File.read("./#{filepath}.rb"), "#{filepath}.rb"
|
|
end
|
|
|
|
def target(hostname)
|
|
env.target = hostname
|
|
end
|
|
|
|
def task(name, &block)
|
|
@tasks << Task.evaluate(name, env, &block)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|