Module: Tengine::Support::Mongoid
Instance Method Summary collapse
-
#create_indexes(pattern, connect_to = nil) ⇒ Object
Create indexes for each model given provided file_path directory and the class is not embedded.
- #determine_model(file) ⇒ Object
Instance Method Details
#create_indexes(pattern, connect_to = nil) ⇒ Object
Create indexes for each model given provided file_path directory and the class is not embedded.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/tengine/support/mongoid.rb', line 24 def create_indexes(pattern, connect_to=nil) logger = Logger.new($stdout) connect_to ||= 'localhost:27017/tengine_production' host, port, db_name = connect_to.split('/').map{|s| s.split(':')}.flatten Mongoid.configure do |c| if Mongoid::VERSION < '3.0.0' c.master = Mongo::Connection.new(host, port).db(db_name) else c.sessions = { default: { database: db_name, hosts: [ "#{host}:#{port}" ] } } end end Dir.glob("#{pattern}/**/*.rb").each do |file| if model = determine_model(file) model.create_indexes logger.info "Generated indexes for #{model}" end end end |
#determine_model(file) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/tengine/support/mongoid.rb', line 53 def determine_model(file) if file =~ /lib\/(.*).rb$/ model_path = $1.split('/') begin parts = model_path.map { |path| path.camelize } name = parts.join('::') klass = name.constantize rescue NameError, LoadError => e begin klass = parts.last.constantize rescue => e return nil end end end if klass.ancestors.include?(::Mongoid::Document) && !klass. return klass end end |