Permalink
Browse files

Add support for configuration

  • Loading branch information...
1 parent df63ca3 commit 1ce492e80eb64957d2ec16c7cf7dc415f2318a03 @robertomiranda committed Jan 31, 2013
Showing with 73 additions and 22 deletions.
  1. +4 −1 examples/post.rb
  2. +1 −0 lib/mini_mongo.rb
  3. +34 −0 lib/mini_mongo/configuration.rb
  4. +12 −20 lib/mini_mongo/connection.rb
  5. +17 −0 spec/configutation_spec.rb
  6. +5 −1 spec/spec_helper.rb
View
@@ -1,5 +1,8 @@
require "mini_mongo"
-MiniMongo.database_url = "mongodb://:@localhost:27017/posts"
+MiniMongo.configure do|config|
+ config.database_url = "mongodb://:@localhost:27017/posts"
+end
+
class Post < MiniMongo::Base
maps :posts
end
View
@@ -1,3 +1,4 @@
+require "mini_mongo/configuration"
module MiniMongo
autoload :Connection, 'mini_mongo/connection'
autoload :MiniMongoError, 'mini_mongo/errors'
@@ -0,0 +1,34 @@
+module MiniMongo
+ # Stores runtime configuration information.
+ #
+ # @example Standard settings
+ # MiniMongo.configure do |config|
+ # config.database_url = "mongodb://user:pass@localhost:27017/posts"
+ # end
+ #
+ # @see MiniMongo.configure
+ class << self
+ attr_accessor :configuration
+ end
+
+ def self.configure
+ self.configuration ||= Configuration.new
+ yield(configuration)
+ end
+
+ class Configuration
+ attr_accessor :slave_ok, :db_name, :db_user, :db_host, :db_port, :db_password
+ attr_reader :database_url
+
+ def database_url=(url)
+ @database_url = url
+ db_info = URI.parse(@database_url)
+ @db_name = db_info.path.gsub(/^\//, '')
+ @db_user = db_info.user
+ @db_password = db_info.password
+ @db_host = db_info.host
+ @db_port = db_info.port
+ end
+
+ end
+end
@@ -4,32 +4,24 @@
module MiniMongo
module Connection
- @@slave_ok = false
-
- def slave_ok=(attr)
- @@slave_ok = attr
- end
-
- def database_url=(url)
- @@database_url = url
- @@db_info = URI.parse(@@database_url)
- @@db_name = @@db_info.path.gsub(/^\//, '')
- @@db_user = @@db_info.user
- @@db_password = @@db_info.password
- end
-
- def db_name
- @@db_name
- end
-
def db_connection
- @@db_connection ||= Mongo::Connection.new(@@db_info.host, @@db_info.port, :slave_ok => @@slave_ok).db(@@db_name)
- @@db_connection.authenticate(@@db_info.user, @@db_info.password) if @@db_info.user && !@@db_info.user.empty?
+ @@db_connection ||= Mongo::Connection.new(
+ MiniMongo.configuration.db_host,
+ MiniMongo.configuration.db_port,
+ :slave_ok => MiniMongo.configuration.slave_ok)
+ .db(MiniMongo.configuration.db_name)
+ authenticate_user
@@db_connection
end
def drop
self.db_connection.drop_database(@@db_name)
end
+
+ def authenticate_user
+ @@db_connection.authenticate(
+ MiniMongo.configuration.db_user,
+ MiniMongo.configuration.db_password)if MiniMongo.configuration.db_user&& !MiniMongo.configuration.db_user.empty?
+ end
end
end
View
@@ -0,0 +1,17 @@
+require "spec_helper"
+
+describe "Configuration" do
+ it "should set the configuration vars from database url" do
+ MiniMongo.configure do |config|
+ config.database_url = "mongodb://:@localhost:27017/posts"
+ end
+ MiniMongo.configuration.db_name.should == "posts"
+ end
+
+ it "should set configuration vars" do
+ MiniMongo.configure do |config|
+ config.db_user = "roberto"
+ end
+ MiniMongo.configuration.db_user.should == "roberto"
+ end
+end
View
@@ -5,7 +5,11 @@
require 'bundler/setup'
Dir["./lib/*.rb"].each {|file| require file }
-MiniMongo.database_url = "mongodb://:@localhost:27017/posts"
+
+MiniMongo.configure do |config|
+ config.database_url = "mongodb://:@localhost:27017/posts"
+end
+
Dir["./spec/models/*.rb"].each {|file| require file }
RSpec.configure do |config|

0 comments on commit 1ce492e

Please sign in to comment.