Rails 项目中的耙子范围?
我在我正在从事的项目中使用了许多解析器。当使用另一个 rake 中已经存在的方法名称时,并且因为它们都使用相同的环境,所以我会遇到冲突。
有没有办法限制 rake 文件在其命名空间内的范围?我认为这就是命名空间的全部意义?
示例:
namespace :test do
task :test_task => :environment do
...
end
def test_method(argument)
...
end
end
namespace :other_test do
task :test_task => :environment do
...
end
def test_method(argument, argument2)
...
end
end
在这种情况下,运行 rake test:test_task
时,我将收到无效参数数量错误。另一方面,如果我在任务本身中定义该方法,则必须按顺序将该方法保留在 rake 文件的顶部。这有点令人困惑和丑陋。
这只是一种必要的罪恶吗?
谢谢!
I have a number of parsers I run with rakes in a project I'm working on. When using a method name that already exists in another rake, and because they both use the same environment, I get a conflict.
Is there a way to limit the scope of rake files within their namespace? I thought that was the whole point of the namespace?
Example:
namespace :test do
task :test_task => :environment do
...
end
def test_method(argument)
...
end
end
namespace :other_test do
task :test_task => :environment do
...
end
def test_method(argument, argument2)
...
end
end
In this case, when running rake test:test_task
I'll receive an invalid amount of arguments error. On the other hand, if I define the method within the task itself, I have to keep the method at the top of the rake file in order. This gets kind of confusing and ugly.
Is that just a necessary evil?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为 rake 任务的命名空间与文件系统中的目录具有相同的用途:它们是关于组织而不是封装。这就是为什么数据库任务在
db:
中,Rails 任务在rails:
中,等等。Rake命名空间不是类,因此您需要问自己要添加什么类
test_method
当您在 Rake 命名空间中定义它时。答案是对象。因此,当您完成第二项任务时,Object 已经有了一个带有一个参数的test_method
方法,Ruby 正确地抱怨了。最好的解决方案是让你的 Rake 任务非常精简(就像控制器一样),并将任何支持方法(例如
test_method
)放在某个库文件中的某个合理位置。 Rake 任务通常应该只进行一些设置,然后调用库方法来完成实际工作(即与控制器相同的总体布局)。执行摘要:将所有实际工作和繁重工作放在库文件中的某个位置,并使 Rake 任务成为库的薄包装。这应该可以通过正确的代码组织来解决您的问题。
I see the namespaces for rake tasks as serving the same purpose as directories in a file system: they're about organization rather than encapsulation. That's why database tasks are in
db:
, Rails tasks inrails:
, etc.Rake namespaces are not classes so you need to ask yourself what class you're adding
test_method
to when you define it within a Rake namespace. The answer is Object. So, when you hit your second task, Object already has atest_method
method that takes one parameter and Ruby rightly complains.The best solution is to make your Rake tasks very thin (just like controllers) and put any supporting methods (such as
test_method
) off in some library file somewhere sensible. A Rake task should usually just do a bit of set up and then call a library method to do the real work (i.e. the same general layout as a controller).Executive summary: put all the real work and heavy lifting somewhere in your library files and make your Rake tasks thin wrappers for your libraries. This should make your problem go away through proper code organization.
Module.new do end
块需要您在任何定义之前self::
。为了避免这种情况,您需要命名该模块并将其删除。Module.new do end
block needs youself::
before any definition. To avoid this, you need to name the module and remove it.我找到了一种方法来命名 Rake 任务中的方法,这样同名的方法就不会发生冲突。
extend Rake::DSL
self.
)。我已经对此进行了测试,它仍然允许一个 rake 任务调用或依赖于不同模块中的另一个 rake 任务。
例子:
I've figured out a way to namespace the methods in Rake tasks so same-named methods don't collide.
extend Rake::DSL
self.
).I've tested this, and it still allows one rake task to invoke or depend on another rake task that is in a different module.
Example: