Class: UsageTrackerServer::Adapters::Mongodb

Inherits:
Object
  • Object
show all
Defined in:
lib/usage_tracker_server/adapters/mongodb.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ Mongodb

Returns a new instance of Mongodb.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/usage_tracker_server/adapters/mongodb.rb', line 9

def initialize (settings)
  @database =
    db = Mongo::Connection.new(settings.database['host'], settings.database['port']).db(settings.database['name'])

    if settings.database['username'] || settings.database['password']
      db.authenticate(settings.database['username'], settings.database['password'])
    end

    @collection = db[settings.database['collection']]
    db
rescue Errno::ECONNREFUSED, Mongo::ConnectionError => e
  raise "Unable to connect to database #{settings.database['name']} with #{settings.adapter} adapter: #{e.message}"
end

Instance Attribute Details

#databaseObject

Returns the value of attribute database.



8
9
10
# File 'lib/usage_tracker_server/adapters/mongodb.rb', line 8

def database
  @database
end

Instance Method Details

#save_doc(doc) ⇒ Object



23
24
25
# File 'lib/usage_tracker_server/adapters/mongodb.rb', line 23

def save_doc(doc)
  @collection.insert(doc)
end