Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a namespace in a Rake task dependency?

When defining rake tasks, it is possible to use namespaces, like this:

namespace :demolition do   task :fire_bazooka do     puts "kaboom!"   end end 

This could be called like rake demolition:fire_bazooka.

It is also possible to specify prerequisites for a task, like this:

# Single prerequisite task :fire_bazooka => :load_bazooka do ....  # Multiple prerequisites task :fire_bazooka => [:safety_check, :load_bazooka] 

But how can I use a namespaced task as a prerequisite? This, obviously, does not work:

task :photograph_destruction => :demolition:fire_bazooka 
like image 537
Nathan Long Avatar asked Nov 10 '11 16:11

Nathan Long


People also ask

How do I run a rake task?

Go to Websites & Domains and click Ruby. After gems installation you can try to run a Rake task by clicking Run rake task. In the opened dialog, you can provide some parameters and click OK - this will be equivalent to running the rake utility with the specified parameters in the command line.

Where do I put Rake tasks?

rake extension and are placed in Rails. root/lib/tasks . You can create these custom rake tasks with the bin/rails generate task command. If your need to interact with your application models, perform database queries and so on, your task should depend on the environment task, which will load your application code.


1 Answers

You already found the solution (name as a string).

You may extend this answer. There is no need to define namespaces and tasks with symbols. You can use Strings.

Doing this, you have the advantage of same type for definition and usage of task names.

Your example looks like this:

namespace 'demolition' do   task 'fire_bazooka' do     puts "kaboom!"   end end  task 'photograph_destruction' => "demolition:fire_bazooka" do   puts "snapping pics! yay!" end 
like image 144
knut Avatar answered Sep 24 '22 01:09

knut