我想知道 – 是否有可能为rake任务创建私有帮助器,无论我如何尝试它们,它们最终都可以在全局范围内使用,并且可以作为任何对象的方法使用.例如: ## this is what I needmodule MyRakeHelpers
## this is what I need
module MyRakeHelpers
def helper_1
end
def helper_2
end
end
include RakeHelpers
task :sometask do
helper_1
helper_2
end
## And this should not work:
# global scope
helper_1
"a random object".helper_1
class RandomClass
def foo
helper_1
end
end
这对我有用:
module MyRakeHelpers
def helper
puts 'foo'
end
end
module MyRakeTasks
extend Rake::DSL
extend MyRakeHelpers
task :some_task do
helper
end
end
简而言之,您可以通过包含(或扩展)Rake :: DSL在不同的范围内使用Rake DSL.从source:
DSL is a module that provides #task, #desc, #namespace, etc. Use this when you’d like to use rake outside the top level scope. For a Rakefile you run from the command line this module is automatically included.
任务使用Rake :: Task#define_task引擎盖,您也可以使用它来编写自己的DSL.
感谢How To Build Custom Rake Tasks; The Right Way关于define_task的提示.
