문제

I'm working on a Rails 3 app, and I've got a hierarchy of classes in my lib folder, e.g.:

lib
├── assets
├── tasks
│   └── import.rake
└── importer
    ├── base.rb
    └── source
        ├── facebook.rb
        ├── google.rb
        └── twitter.rb

I've updated config/application.rb to include this line:

config.autoload_paths += %W(#{config.root}/lib)

Then inside of Importer::Base, I have an instance method that attempts to load all classes in the Provider module, e.g.:

Importer::Source.constants.each do |class_name|
  Importer::Source.const_get(class_name).process
end

The three classes in lib/importer/base have a class hierarchy similar to:

module Importer
  module Source
    class Facebook
      # ...
    end
  end
end

When I call this method, Importer::Source.constants ends up returning an empty array. The classes appear to be lazy-loaded properly if I reference them by name directly, but they are not accessible in the constants call. How can I fix this?

도움이 되었습니까?

해결책

Using @apneadiving's suggestion, I was able to fix this by adding this line to the beginning of my base.rb file:

Dir[Rails.root.join('lib/importer/source/**/*.rb')].each(&method(:require))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top