Dynamic namespace rakes and parser classes with rails?
I have a collection of parsers that differ significantly in logic, but have the exact same methods and outputs.
I have devised somewhat of a master Rake
and I am cu开发者_Go百科rious if what I have come up with could lead to some unexpected or weird behavior.
Essentially my parse.rake
looks something like:
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 a 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 setup, I can easily invoke rake ABC:provider_task
to run a specific parser. Also the cattr_accessor
allows me to easily refer to the actual provider model. Thoughts?
I have used this with no issues for a few weeks. After reviewing with some colleagues, I found a few who previously used a similar approach with no issues. Only potential danger is naming conflicts with your ruby classes.
精彩评论