开发者

MongoMapper: Get all models

Does anyone know of an easy way to fetch the class names of all models that have MongoMapper::Document included?

The 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 and some ObjectSpace hacks.

A further complication is that I have modules (subfolders) in the app/models folder. So, class DS::Thingy is in my apps/models/ds/thingy.rb.


How about something like:

class MongoModels
  def self.all
    @@models ||= []
  end
end

module MongoMapper
  module Document
    def self.included(klass) 
      MongoModels.all << klass
    end
  end
end

then MongoModels::all would return an Array of models.

MongoMapper's document.rb does:

included do
  extend Plugins
  extend Translation
end

So you might need to replicate that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜