Rake volume in rails project?

I have a number of parsers that I run with a rake in the project I'm working on. When using a method name that already exists in another rake, and since both of them use the same environment, I get a conflict.

Is there a way to limit the size of rake files in their namespace? I thought that was the meaning 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 I run rake test:test_task I get an invalid number of arguments. On the other hand, if I define a method inside the task itself, I need to save the method at the top of the rake file in order. It gets confusing and ugly.

Is this just a necessary evil?

Thanks!

+7
source share
3 answers

I see namespaces for rake tasks that serve the same purpose as directories in the file system: they relate to organization, not encapsulation. Therefore, the database tasks are in db: the Rails tasks in rails: etc.

Rake namespaces are not classes, so you need to ask yourself which class you add test_method when you define it in the Rake namespace. The answer is Object. So, when you click on the second task, Object already has a test_method method that takes one parameter, and Ruby complains correctly.

The best solution is to make your Rake tasks very thin (like controllers) and put any supporting methods (like test_method ) in some kind of library file somewhere reasonably. The Rake task usually needs to just tweak a bit and then call the library method to do the real work (i.e., the same general layout as the controller).

Summary: put all the real work and hard work somewhere in your library files and make your Rake tasks a thin shell for your libraries. This should make your problem go away through proper code organization.

+8
source
 module Anonymous namespace :test do # brabra end end self.class.send(:remove_const, :Anonymous) module Anonymous namespace :another_test do # brabra end end self.class.send(:remove_const, :Anonymous) 
Block

Module.new do end needs you self:: before any definition. To avoid this, you need to name the module and remove it.

0
source

I figured out a way to use namespaces in Rake tasks, so methods with the same name do not collide.

  • Wrap each external namespace in the module of the same name.
  • extend Rake::DSL
  • Change methods to class methods (using self. ).

I tested this, and it still allows one rake task to call or depend on another rake task, which is in another module.

Example:

 module Test extend Rake::DSL namespace :test do task :test_task => :environment do ... end def self.test_method(argument) ... end end end module OtherTest extend Rake::DSL namespace :other_test do task :test_task => :environment do ... end def self.test_method(argument, argument2) ... end end end 
0
source

All Articles