Skip to content

Instantly share code, notes, and snippets.

@mklbtz
Last active February 22, 2017 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mklbtz/d31dcfebb519e9239c67c9fd0891e830 to your computer and use it in GitHub Desktop.
Save mklbtz/d31dcfebb519e9239c67c9fd0891e830 to your computer and use it in GitHub Desktop.
Extend a module with AutoRake for convenient rake task calling.
module AutoRake
require 'rake'
Rails.application.load_tasks if Rake::Task.tasks.empty?
def const_missing(const)
mod = Module.new
mod.extend AutoRake
const_set(const, mod)
end
def invoke
task.invoke
end
def task(suffix=nil)
Rake::Task[task_name(suffix)]
end
def task_name(suffix=nil)
prefix = name.gsub('::', ':').downcase
suffix ? "#{prefix}:#{suffix.downcase}" : prefix
end
end
## Example Usage
module DB
extend AutoRake
end
DB::Clean.invoke
DB::Seed.invoke
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment