From f3861a6dfb362046fa17642aa60efcc5b20189b3 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Fri, 4 Sep 2015 14:33:53 -0500 Subject: [PATCH] Add Invoking Rake Tasks Multiple Times as a ruby til. --- README.md | 1 + ruby/invoking-rake-tasks-multiple-times.md | 32 ++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 ruby/invoking-rake-tasks-multiple-times.md diff --git a/README.md b/README.md index 9f757c7..f102fc1 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,7 @@ smart people at [Hashrocket](http://hashrocket.com/). - [Evaluating One-Off Commands](ruby/evaluating-one-off-commands.md) - [FactoryGirl Sequences](ruby/factory-girl-sequences.md) - [Finding The Source of Ruby Methods](ruby/finding-the-source-of-ruby-methods.md) +- [Invoking Rake Tasks Multiple Times](ruby/invoking-rake-tasks-multiple-times.md) - [Last Raised Exception In The Call Stack](ruby/last-raised-exception-in-the-call-stack.md) - [Limit Split](ruby/limit-split.md) - [Listing Local Variables](ruby/listing-local-variables.md) diff --git a/ruby/invoking-rake-tasks-multiple-times.md b/ruby/invoking-rake-tasks-multiple-times.md new file mode 100644 index 0000000..0598209 --- /dev/null +++ b/ruby/invoking-rake-tasks-multiple-times.md @@ -0,0 +1,32 @@ +# Invoking Rake Tasks Multiple Times + +I have a rake task, `build`, that builds a single record for development +purposes. I want a supplemental rake task, `build:all`, that builds a bunch +of different records. To keep things dry, `build:all` should just invoke +`build` a number of times. + +```ruby +namespace :build do + task :all do + predefined_list.each do |data| + Rake::Task["build"].invoke(data) + end + end +end +``` + +This doesn't work though. No matter how many items are in the list, the +`build` task only seems to get run once. This is because by default tasks +can only be invoked once in a given context. To get around this, the task +needs to be reenabled after each invocation. + +```ruby +namespace :build do + task :all do + predefined_list.each do |data| + Rake::Task["build"].invoke(data) + Rake::Task["build"].reenable + end + end +end +```