Last active
February 22, 2017 16:43
-
-
Save mklbtz/d31dcfebb519e9239c67c9fd0891e830 to your computer and use it in GitHub Desktop.
Extend a module with AutoRake for convenient rake task calling.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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