views:

105

answers:

2

If my Rakefile does not find a task with a particular name, I'd like rake to instead create a new task by that name according to certain rules, if a file with the missing task name exists. But if it doesn't, I want to fall back to the default ("Don't know how to build task 'foo'!").

In short, is there a method_missing for Rake?

A: 

In short, is there a method_missing for Rake?

In short, no. Rake is not a programming language, it's about dependencies. It's a tool to help you run automate task and it assumes you already created these tasks.

Simone Carletti
Your answer is true, but I can't give this a +1, as I strenuously disagree that Rake is not suited to metaprogramming. In fact, the rdoc even speaks about it: "Sometimes it is useful to manipulate tasks programmatically in a Rakefile. [...] The ability to programmatically manipulate tasks gives `rake` very powerful meta-programming capabilities w.r.t. task execution."
John Feminella
In my mind, "manipulate tasks programmatically in a Rakefile" means you can create/manipulate tasks using Ruby, like Echoe or Jeweler does. It doesn't necessary mean that rake can/should manipulate itself. Of course, I might be wrong, but the end of the story is that Rails doesn't support any method_missing mechanism.
Simone Carletti
Wouldn't you say that Rake is, in fact, a DSL, i.e. specialized subset language, of Ruby? When you're writing Rake tasks, you *are* writing Ruby.
Jason M
+2  A: 

I haven't tried it, but a quick search revealed this.

If you define a rule with an empty string, you can catch any task that hasn’t been defined elsewhere. This makes it easy to dynamically create rake tasks. Essentially, this is method_missing for rake!

rule "" do |t|
  t.name 
  # ... do something with the name of the task  
end
Jason M
works perfectly for me
opsb