Implement Worker class:

* Move recipe processing code in the worker;
* Refactor CLI and use the the worker;
* Implement Recipe#tasks and remove tasks application during evaluation,
  tasks are now applied by the worker after all evaluations are done.
This commit is contained in:
Thibault Jouan
2013-08-08 01:55:55 +00:00
parent ec44d01c36
commit 0904fa1fc9
9 changed files with 158 additions and 76 deletions

View File

@@ -12,16 +12,6 @@ module Producer
def run!
check_arguments!
evaluate_recipe_file
end
def check_arguments!
print_usage_and_exit(64) unless @arguments.length == 1
end
def evaluate_recipe_file
recipe = Recipe.from_file(@arguments.first)
env = Env.new(recipe)
begin
recipe.evaluate env
rescue RecipeEvaluationError => e
@@ -31,6 +21,23 @@ module Producer
exit 70
end
worker.process recipe.tasks
end
def check_arguments!
print_usage_and_exit(64) unless @arguments.length == 1
end
def env
@env ||= Env.new(recipe)
end
def recipe
@recipe ||= Recipe.from_file(@arguments.first)
end
def worker
@worker ||= Worker.new
end
private

View File

@@ -1,7 +1,7 @@
module Producer
module Core
class Recipe
attr_reader :code, :filepath
attr_reader :code, :filepath, :tasks
def self.from_file(filepath)
new(File.read(filepath), filepath)
@@ -10,11 +10,13 @@ module Producer
def initialize(code, filepath = nil)
@code = code
@filepath = filepath
@tasks = []
end
def evaluate(env)
dsl = DSL.new(@code).evaluate(env)
dsl.tasks.map { |e| e.evaluate env }
@tasks = dsl.tasks
end
end
end

View File

@@ -1,17 +1,18 @@
module Producer
module Core
class Task
attr_reader :name
attr_reader :name, :actions
def initialize(name, &block)
@name = name
@block = block
@name = name
@block = block
@actions = []
end
def evaluate(env)
dsl = DSL.new(&@block)
dsl.evaluate(env)
dsl.actions.map(&:apply)
@actions = dsl.actions
end
end
end

View File

@@ -0,0 +1,13 @@
module Producer
module Core
class Worker
def process(tasks)
tasks.each { |t| process_task t }
end
def process_task(task)
task.actions.each(&:apply)
end
end
end
end