MongoMapper: Get all models
Does anyone know of an easy way to fetch the class names of all models that have MongoMapper::Document
включено?
MongoMapper
class doesn't appear to have the equivalent of ActiveRecord::Base.subclasses
,
The best I can come up with is using MongoMapper.database.collection_names
и немного ObjectSpace
хаки.
A further complication is that I have modules (subfolders) in the app/models
папка. Так, class DS::Thingy
в моем apps/models/ds/thingy.rb
,
1 ответ
Решение
Как насчет чего-то вроде:
class MongoModels
def self.all
@@models ||= []
end
end
module MongoMapper
module Document
def self.included(klass)
MongoModels.all << klass
end
end
end
тогда MongoModels::all вернет массив моделей.
Document.rb MongoMapper делает:
included do
extend Plugins
extend Translation
end
Так что вам может понадобиться повторить это.