Skip to content
Browse files

Changed configuration options to module attributes

  • Loading branch information...
1 parent 1441536 commit 5b7f6ef0f9583e2a346fee3ecaf1c357fb62c3ab @SFEley committed Mar 21, 2010
Showing with 98 additions and 90 deletions.
  1. +0 −1 lib/candy/collection.rb
  2. +74 −43 lib/candy/crunch.rb
  3. +0 −1 lib/candy/piece.rb
  4. +6 −7 spec/candy/collection_spec.rb
  5. +17 −36 spec/candy/crunch_spec.rb
  6. +1 −2 spec/spec_helper.rb
View
1 lib/candy/collection.rb
@@ -12,7 +12,6 @@ module Collection
UP_SORTS = [Mongo::ASCENDING, 'ascending', 'asc', :ascending, :asc, 1, :up]
DOWN_SORTS = [Mongo::DESCENDING, 'descending', 'desc', :descending, :desc, -1, :down]
- include Crunch::InstanceMethods
include Enumerable
module ClassMethods
View
117 lib/candy/crunch.rb
@@ -2,47 +2,83 @@
require 'etc' # To get the current username for database default
module Candy
+ # Our option accessors live here so that someone could include just the
+ # 'candy/crunch' module and make it standalone.
+
+ # Overrides the host and resets the connection, db, and collection.
+ def self.host=(val)
+ @connection = nil
+ @host = val
+ end
+
+ # Overrides the port and resets the connection, db, and collection.
+ def self.port=(val)
+ @connection = nil
+ @port = val
+ end
+
+ # Overrides the options hash and resets the connection, db, and collection.
+ def self.connection_options=(val)
+ @connection = nil
+ @connection_options = val
+ end
+
+ # Passed to the default connection. If not set, Mongo's default of localhost will be used.
+ def self.host
+ @host
+ end
+
+ # Passed to the default connection. If not set, Mongo's default of 27017 will be used.
+ def self.port
+ @port
+ end
+
+ # A hash passed to the default connection. See the Mongo::Connection documentation for valid options.
+ def self.connection_options
+ @connection_options ||= {}
+ end
+
+ # First clears any collection and database we're talking to, then accepts a connection you provide.
+ # You're responsible for your own host, port and options if you use this.
+ def self.connection=(val)
+ self.db = nil
+ @connection = val
+ end
+
+ # Returns the connection you gave, or creates a default connection to the default host and port.
+ def self.connection
+ @connection ||= Mongo::Connection.new(host, port, connection_options)
+ end
+
+ # Accepts a database you provide. You can provide a Mongo::DB object or a string with the database
+ # name. If you provide a Mongo::DB object, the default connection is not used, and the :strict flag
+ # should be false or default collection lookup will fail.
+ def self.db=(val)
+ case val
+ when Mongo::DB
+ @db = val
+ when String
+ @db = Mongo::DB.new(val, connection)
+ when nil
+ @db = nil
+ else
+ raise ConnectionError, "The db attribute needs a Mongo::DB object or a name string."
+ end
+ end
+
+ # Returns the database you gave, or creates a default database named for your username (or 'candy' if it
+ # can't find a username).
+ def self.db
+ @db ||= Mongo::DB.new(Etc.getlogin || 'candy', connection, :strict => false)
+ end
# All of the hard crunchy bits that connect us to a collection within a Mongo database.
module Crunch
module ClassMethods
-
- # Passed to the default connection. It uses the $MONGO_HOST global if that's set and you don't override it.
- def host
- @host ||= $MONGO_HOST
- end
-
- # Passed to the default connection. It uses the $MONGO_PORT global if that's set and you don't override it.
- def port
- @port ||= $MONGO_PORT
- end
-
- # A hash passed to the default connection. It uses the $MONGO_OPTIONS global if that's set and you don't override it.
- def options
- @options ||= ($MONGO_OPTIONS || {})
- end
-
- # Overrides the host and resets the connection, db, and collection.
- def host=(val)
- @connection = nil
- @host = val
- end
-
- # Overrides the port and resets the connection, db, and collection.
- def port=(val)
- @connection = nil
- @port = val
- end
-
- # Overrides the options hash and resets the connection, db, and collection.
- def options=(val)
- @connection = nil
- @options = val
- end
-
- # Returns the connection you gave, or creates a default connection to the default host and port.
+
+ # Returns the connection you gave, or uses the application-level Candy collection.
def connection
- @connection ||= Mongo::Connection.new(host, port, options)
+ @connection ||= Candy.connection
end
# First clears any collection and database we're talking to, then accepts a connection you provide.
@@ -69,10 +105,9 @@ def db=(val)
end
end
- # Returns the database you gave, or creates a default database named for your username (or 'candy' if it
- # can't find a username).
+ # Returns the database you gave, or uses the application-level Candy database.
def db
- @db ||= Mongo::DB.new($MONGO_DB || Etc.getlogin || 'candy', connection, :strict => false)
+ @db ||= Candy.db
end
# Accepts either a Mongo::Collection object or a string with the collection name. If you provide a
@@ -109,13 +144,9 @@ def index(property, direction=:asc)
end
end
- module InstanceMethods
-
- end
def self.included(receiver)
receiver.extend ClassMethods
- receiver.send :include, InstanceMethods
end
end
end
View
1 lib/candy/piece.rb
@@ -27,7 +27,6 @@ def method_missing(name, *args, &block)
end
- include Crunch::InstanceMethods
# We push ourselves into the DB before going on with our day.
View
13 spec/candy/collection_spec.rb
@@ -14,19 +14,18 @@
@the_other.color = "blue"
@the_other.pieces = 7
@the_other.weight = 0
- sleep(0.1) # We need to wait for the DB to catch up - hmph on consistency
- end
-
- it "can get all objects in a collection" do
- sleep(2) # Seriously, this is nuts.
- those = Zagnuts.all
- those.count.should == 3
end
+
it "can get all objects matching a search condition" do
those = Zagnuts.color("red")
those.count.should == 2
end
+
+ it "can get all objects in a collection" do
+ those = Zagnuts.all
+ those.count.should == 3
+ end
it "still returns if nothing matches" do
Zagnuts.color("green").to_a.should == []
View
53 spec/candy/crunch_spec.rb
@@ -11,7 +11,7 @@ class PeanutBrittle
describe "connection" do
before(:each) do
# Make sure we don't waste time making bogus connections
- PeanutBrittle.options[:connect] = false
+ Candy.connection_options[:connect] = false
end
it "takes yours if you give it one" do
@@ -24,39 +24,22 @@ class PeanutBrittle
PeanutBrittle.connection.nodes.should == [["localhost", 27017]]
end
- it "uses the host you provide" do
- PeanutBrittle.host = 'example.org'
- PeanutBrittle.connection.nodes.should == [["example.org", 27017]]
- end
-
- it "uses the port you provide" do
- PeanutBrittle.host = 'localhost'
- PeanutBrittle.port = 3000
- PeanutBrittle.connection.nodes.should == [["localhost", 3000]]
- end
- it "uses any options you provide" do
- l = Logger.new(STDOUT)
- PeanutBrittle.options[:logger] = l
- PeanutBrittle.connection.logger.should == l
- end
-
- it "uses the $MONGO_HOST setting if you don't override it" do
- $MONGO_HOST = 'example.net'
+ it "uses the Candy.host setting if you don't override it" do
+ Candy.host = 'example.net'
PeanutBrittle.connection.nodes.should == [["example.net", 27017]]
end
- it "uses the $MONGO_PORT setting if you don't override it" do
- PeanutBrittle.host = 'localhost'
- $MONGO_PORT = 33333
+ it "uses the Candy.port setting if you don't override it" do
+ Candy.host = 'localhost'
+ Candy.port = 33333
PeanutBrittle.connection.nodes.should == [["localhost", 33333]]
end
- it "uses the $MONGO_OPTIONS setting if you don't override it" do
+ it "uses the Candy.connection_options setting if you don't override it" do
l = Logger.new(STDOUT)
- PeanutBrittle.options = nil # Gotta be careful of our order on this one
- $MONGO_OPTIONS = {:logger => l, :connect => false}
- PeanutBrittle.connection.logger.should == $MONGO_OPTIONS[:logger]
+ Candy.connection_options = {:logger => l, :connect => false}
+ PeanutBrittle.connection.logger.should == Candy.connection_options[:logger]
end
it "clears the database when you set it" do
@@ -66,18 +49,16 @@ class PeanutBrittle
end
after(:each) do
- $MONGO_HOST = nil
- $MONGO_PORT = nil
- $MONGO_OPTIONS = nil
- PeanutBrittle.host = nil
- PeanutBrittle.port = nil
- PeanutBrittle.options = nil
+ Candy.host = nil
+ Candy.port = nil
+ Candy.connection_options = nil
+ PeanutBrittle.connection = nil
end
end
describe "database" do
before(:each) do
- $MONGO_DB = nil
+ Candy.db = nil
end
it "takes yours if you give it one" do
@@ -95,8 +76,8 @@ class PeanutBrittle
lambda{PeanutBrittle.db = 5}.should raise_error(Candy::ConnectionError, "The db attribute needs a Mongo::DB object or a name string.")
end
- it "uses the $MONGO_DB setting if you don't override it" do
- $MONGO_DB = 'foobar'
+ it "uses the Candy.db setting if you don't override it" do
+ Candy.db = 'foobar'
PeanutBrittle.db.name.should == 'foobar'
end
@@ -118,7 +99,7 @@ class PeanutBrittle
end
after(:all) do
- $MONGO_DB = 'candy_test' # Get back to our starting point
+ Candy.db = 'candy_test' # Get back to our starting point
end
end
View
3 spec/spec_helper.rb
@@ -7,14 +7,13 @@
# Support methods
-$MONGO_DB = 'candy_test'
+Candy.db = 'candy_test'
Dir[File.join(File.dirname(__FILE__), 'support', '*.rb')].each {|f| require f}
Spec::Runner.configure do |config|
config.mock_with :mocha
config.before(:all) do
- $MONGO_DB = 'candy_test'
end
config.after(:all) do

0 comments on commit 5b7f6ef

Please sign in to comment.
Something went wrong with that request. Please try again.