lib/low/mongo.rb in low-0.0.3 vs lib/low/mongo.rb in low-0.0.4

- old
+ new

@@ -2,10 +2,12 @@ module Low # The `Mongo` module defines an interface for a Mongo helper # class. It also includes two basic classes, `Local` and `Remote`. module Mongo + autoload :Util, 'low/mongo/util' + # Simple access to a Mongo::Collection instance. def [](collection) db[collection] end @@ -31,10 +33,16 @@ # The database `#db` will use - must be overriden. def database end + def username + end + + def password + end + # Force a new connection the next time one is needed def reset_connection! @grid = nil @db = nil @connection = nil @@ -42,17 +50,19 @@ # For connecting to Mongo on localhost. class Local include Mongo - attr_reader :host, :database + attr_reader :host, :database, :username, :password # Specify the database upon initialization and # assume that the host is localhost (unless told otherwise). - def initialize(database, host = nil) + def initialize(database, opts = {}) @database = database - @host = host || 'localhost' + @host = opts[:host] || 'localhost' + @username = opts[:username] + @password = opts[:password] end end # For connecting to Mongo via a URI. class Remote @@ -68,12 +78,26 @@ # and use it to connect. def connection @connection ||= ::Mongo::Connection.from_uri(uri) end - # The database can be extracted from the URI. + # The database can be extracted from the URI, def database - uri.match(/.*\/(.*)$/)[1] + @uri.match(/.*\/(.*)$/)[1] + end + + # as can the username, + def username + if match = @uri.match(/^.*:\/\/(.*?):.*/) + match[1] + end + end + + # and password. + def password + if match = @uri.match(/^.*:\/\/.*?:(.*?)@.*/) + match[1] + end end end end end