Permalink
Browse files

Adding Mongoid::Config singleton to handle global options

  • Loading branch information...
1 parent a6a66af commit 12ef03f50a9ff3ec97ffe4b62203d410a5b82afa @durran durran committed Dec 28, 2009
Showing with 93 additions and 40 deletions.
  1. +7 −22 lib/mongoid.rb
  2. +25 −0 lib/mongoid/config.rb
  3. +4 −1 lib/mongoid/criteria.rb
  4. +0 −1 spec/spec_helper.rb
  5. +51 −0 spec/unit/mongoid/config_spec.rb
  6. +6 −16 spec/unit/mongoid_spec.rb
View
@@ -41,6 +41,7 @@
require "mongoid/associations/options"
require "mongoid/attributes"
require "mongoid/commands"
+require "mongoid/config"
require "mongoid/complex_criterion"
require "mongoid/criteria"
require "mongoid/dynamic_finder"
@@ -52,29 +53,13 @@
require "mongoid/versioning"
require "mongoid/document"
-module Mongoid
+module Mongoid #:nodoc
- # Sets the Mongo::DB to be used.
- def self.database=(db)
- raise Errors::InvalidDatabase.new("Database should be a Mongo::DB, not #{db.class.name}") unless db.kind_of?(Mongo::DB)
- @@database = db
- end
-
- # Returns the Mongo::DB to use or raise an error if none was set.
- def self.database
- @@database || (raise Errors::InvalidDatabase.new("No database has been set, please use Mongoid.database="))
- end
-
- # Sets whether or not an Errors::DocumentNotFound error is raised when
- # finding a document by its id returns none.
- def self.raise_not_found_error=(error)
- @@raise_not_found_error = error
- end
-
- # Return whether or not an error is raised when finding by an id does not
- # return anything from the database.
- def self.raise_not_found_error
- @@raise_not_found_error
+ class << self
+ #direct all calls to the configuration
+ def method_missing(name, *args)
+ Config.instance.send(name, *args)
+ end
end
end
View
@@ -0,0 +1,25 @@
+module Mongoid #:nodoc
+ class Config #:nodoc
+ include Singleton
+
+ attr_accessor :raise_not_found_error
+
+ def initialize
+ @raise_not_found_error = true
+ end
+
+ # Sets the Mongo::DB to be used.
+ def database=(db)
+ raise Errors::InvalidDatabase.new(
+ "Database should be a Mongo::DB, not #{db.class.name}"
+ ) unless db.kind_of?(Mongo::DB)
+ @database = db
+ end
+
+ # Returns the Mongo::DB to use or raise an error if none was set.
+ def database
+ @database || (raise Errors::InvalidDatabase.new("No database has been set, please use Mongoid.database="))
+ end
+
+ end
+end
View
@@ -440,7 +440,10 @@ def self.translate(*args)
params = args[1] || {}
if params.is_a?(String)
document = new(klass).id(params).one
- return document ? document : (raise Errors::DocumentNotFound.new(klass, params))
+ if Mongoid.raise_not_found_error
+ raise Errors::DocumentNotFound.new(klass, params) unless document
+ end
+ return document
end
return new(klass).where(params.delete(:conditions) || {}).extras(params)
end
View
@@ -13,7 +13,6 @@
Spec::Runner.configure do |config|
config.mock_with :mocha
- Mocha::Configuration.prevent(:stubbing_non_existent_method)
end
class MixedDrink
@@ -0,0 +1,51 @@
+require "spec_helper"
+
+describe Mongoid::Config do
+
+ after :all do
+ config.raise_not_found_error = true
+ end
+
+ let(:config) { Mongoid::Config.instance }
+
+ describe ".database=" do
+
+ context "when object provided is not a Mongo::DB" do
+
+ it "raises an error" do
+ lambda { config.database = "Test" }.should raise_error
+ end
+
+ end
+
+ end
+
+ describe ".raise_not_found_error=" do
+
+ context "when setting to true" do
+
+ before do
+ config.raise_not_found_error = true
+ end
+
+ it "sets the value" do
+ config.raise_not_found_error.should == true
+ end
+
+ end
+
+ context "when setting to false" do
+
+ before do
+ config.raise_not_found_error = false
+ end
+
+ it "sets the value" do
+ config.raise_not_found_error.should == false
+ end
+
+ end
+
+ end
+
+end
View
@@ -2,26 +2,16 @@
describe Mongoid do
- describe ".database=" do
-
- context "when object provided is not a Mongo::DB" do
-
- it "raises an error" do
- lambda { Mongoid.database = "Test" }.should raise_error
- end
-
- end
-
- end
-
- describe ".raise_not_found_error" do
+ describe ".method_missing" do
before do
- Mongoid.raise_not_found_error = false
+ @config = mock
+ Mongoid::Config.expects(:instance).returns(@config)
end
- it "sets the not found error flag" do
- Mongoid.raise_not_found_error.should == false
+ it "delegates all calls to the config singleton" do
+ @config.expects(:raise_not_found_error=).with(false)
+ Mongoid.raise_not_found_error = false
end
end

0 comments on commit 12ef03f

Please sign in to comment.