Prev: ruby scripting on waitr
Next: ruby
From: Caleb Clausen on 23 Apr 2010 11:50 On 4/22/10, James Wenton <kkaitan(a)gmail.com> wrote: > Specifically, that pattern is the following: > > namespace :foobar do > desc "Frozz the foobar." > task :frozzify do > unless Rake.application.lookup('_frozzify') > require 'tasks/foobar' > Foobar.new.frozzify > end > Rake.application['_frozzify'].invoke > end > > # Above pattern repeats many times. > end > > # Several namespaces, each with tasks that follow this pattern. There may be some feature of rake that helps dry up code like this... I wouldn't know. But if not, maybe something like this would help: (untested, you need to customize it anyway) def declare_tasks namespace, *tasks eval %{ namespace :#{namespace.downcase} do #{tasks.map{|task| <<-END desc("#{task.capitalize} the #{namespace}") task :#{task}ify do unless Rake.application.lookup('_#{task}ify') require 'tasks/#{namespace.downcase}' #{namespace}.new.#{task}ify end Rake.application['_#{task}ify'].invoke end END }.join } end } end declare_task "Foobar", "frozz", "frotz", "xyzzy" Eval is the heavy guns of meta-programming. Instead of downcase, you need to use the rails camel-case-to-snake-case conversion method. I forget the name. Also, you could dry it up more by scanning the tasks/ directory for namespaces and tasks to define.... but that might be kind of hard.
|
Pages: 1 Prev: ruby scripting on waitr Next: ruby |