Class: Mongar::Mongo

Inherits:
Object
  • Object
show all
Defined in:
lib/mongar/mongo.rb,
lib/mongar/mongo/collection.rb

Defined Under Namespace

Classes: Collection

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Mongo

Returns a new instance of Mongo.



14
15
16
17
18
19
20
21
22
# File 'lib/mongar/mongo.rb', line 14

def initialize(args = {})
  args.each do |key, value|
    instance_variable_set(:"@#{key}", value)
  end
  
  @host ||= '127.0.0.1'
  @port ||= 27017
  @status_collection ||= 'statuses'
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/mongar/mongo.rb', line 6

def name
  @name
end

Class Method Details

.databasesObject



9
10
11
# File 'lib/mongar/mongo.rb', line 9

def databases
  @databases ||= {}
end

Instance Method Details

#connectionObject



32
33
34
# File 'lib/mongar/mongo.rb', line 32

def connection
  ::Mongo::Connection.new(host, port)
end

#connection!Object



36
37
38
# File 'lib/mongar/mongo.rb', line 36

def connection!
  connection or raise StandardError, "Could not establish '#{name}' MongoDB connection for #{database} at #{host}:#{port}"
end

#dbObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/mongar/mongo.rb', line 40

def db
  return @db unless @db.nil?
  @db = connection!.db(database.to_s)
  unless self.user.nil?
    db = self.auth_database.nil? ? @db : connection!.db(self.auth_database)
    mechanism = self.auth_mechanism || 'SCRAM-SHA-1'
    db.authenticate(user, password, :mechanism => mechanism)
  end
  @db
end

#status_collection_accessorObject



51
52
53
# File 'lib/mongar/mongo.rb', line 51

def status_collection_accessor
  db[status_collection]
end

#time_on_serverObject



55
56
57
# File 'lib/mongar/mongo.rb', line 55

def time_on_server
  db.eval("return new Date()")
end