2

I have a series of rake tasks that seeds several objects and perform certain actions based on data in a spreadsheet. The very first task in the series, though, validates the spreadsheet and checks that all the data is in the correct form. I have a task that runs all the tasks in the series one by one, but I want to alter it so that it will abort all tasks after the sheet validation if the sheet is invalid, how can I do this?

I essentially want to have some form of communication between the sheet validation task and the overarching task running all tasks. What's the ruby way to do this?

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214

1 Answers1

0

You can abort the rake execution by calling abort in any task. By using dependencies the following tasks never get executed if any of the preceding ones fails.

task :validate do
  abort "validation failed"
end

task :second do
  puts "never executed"
end

task :default =>  [:validate, :second]

EDIT

Concerning the arguments, i suggest using a class that keeps the state so you don't have to pass arguments around:

class Spreadsheet
  def initialize(args)
    # ...
  end

  def validate
    false
  end
end

task :init do
  @spreadsheet = Spreadsheet.new("something")
end

task :first do
  unless @spreadsheet.validate
    abort "validation failed"
  end
end

task :second do
  puts "never executed"
end

task :default =>  [:init, :first, :second]
koffeinfrei
  • 1,985
  • 13
  • 19