Module: MongoFe::MongoDB

Defined in:
lib/mongo_fe.rb

Defined Under Namespace

Classes: Database, SearchDocuments

Constant Summary collapse

@@user_db =
nil

Class Method Summary collapse

Class Method Details

.available_databasesObject



145
146
147
148
149
150
151
# File 'lib/mongo_fe.rb', line 145

def self.available_databases
  if @@user_db
    [@@user_db]
  else
    self.connection.database_names - %w[admin local slave]
  end
end

.connectionObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/mongo_fe.rb', line 124

def self.connection
  begin
    unless defined?(@@connection)
      uri = @@uri.to_s
      @@connection = Mongo::Connection.from_uri(uri) 
      if @@uri.user && @@uri.password
        if !@@uri.path.nil?
          @@user_db = @@uri.path.gsub(/^\//, '')
          @@connection.add_auth(@@user_db, @@uri.user, @@uri.password)
        else
          raise "username and password provided but the db name is missing. Please verify the config uri."
        end
      end
    end
  
    @@connection
  rescue Mongo::ConnectionFailure => e
    raise "Please verify that you have a MongoDB started at: #{@@host}:#{@@port} error: #{e.message}"
  end
end

.uriObject



109
110
111
112
# File 'lib/mongo_fe.rb', line 109

def self.uri
  @@uri = nil unless defined?(@@uri) 
  @@uri
end

.uri=(uri) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/mongo_fe.rb', line 114

def self.uri=(uri)
  begin
    @@uri  = URI.parse(uri)
    @@host = @@uri.host
    @@port = @@uri.port        
  rescue => e
    raise "#{e.message}; #{uri}"
  end      
end

.use(db_name = nil) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/mongo_fe.rb', line 101

def self.use(db_name=nil)
  if !@@user_db.nil? && db_name != @@user_db
    raise 'Invalid database name. Not authorized, maybe?!'
  end
  
  Database.new(self.connection.db(db_name))
end