Dynamic namespace rake and parser classes with rails?

I have a set of parsers that differ significantly in logic, but have exact methods and results.

I have developed several Rake masters, and I'm curious that what I came up with can lead to unexpected or strange behavior.

Essentially my parse.rake looks something like this:

 require 'app/models/provider' require 'config/environment.rb' Provider.all.each do |provider| require "lib/tasks/#{provider.name}.rb" Kernel.const_get(provider.name).provider = provider namespace provider.name do task :provider_task do #Parse method Kernel.const_get(provider.name).provider_task() end end end 

Since classes are constants in ruby, Kernel.const_get is what I used to access class methods from varname. My classes look like ( ABC.rb ):

 Class ABC cattr_accessor :provider def self.provider_task() #Parse and add stuff to environment end end 

With this setting, I can easily call rake ABC:provider_task to run a specific parser. Also cattr_accessor allows me to easily reference the actual provider model. Thoughts?

+3
source share
1 answer

I used this without any problems for several weeks. After a review with some colleagues, I found a few who had previously used a similar approach without problems. Only potential danger is naming conflicts with your ruby ​​classes.

+1
source

All Articles