Dynamic namespace rakes and parser classes with rails?

这一生的挚爱 提交于 2019-12-10 20:22:28

问题


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 curious 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?


回答1:


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.



来源:https://stackoverflow.com/questions/6183367/dynamic-namespace-rakes-and-parser-classes-with-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!