Skip to content

Commit

Permalink
Further into the Mongoid config refactor. Switched from using singleton
Browse files Browse the repository at this point in the history
to extending self, clean up config options.
  • Loading branch information
durran committed Dec 25, 2010
1 parent b5fb0b5 commit ba7e2ea
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 81 deletions.
2 changes: 1 addition & 1 deletion lib/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class << self
#
# @return [ Config ] The configuration obejct.
def configure
config = Mongoid::Config.instance
config = Mongoid::Config
block_given? ? yield(config) : config
end
alias :config :configure
Expand Down
130 changes: 67 additions & 63 deletions lib/mongoid/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,33 @@

module Mongoid #:nodoc

# This class defines all the configuration options for Mongoid, including the
# This module defines all the configuration options for Mongoid, including the
# database connections.
#
# @todo Durran: This class needs an overhaul, remove singleton, etc.
class Config < Hash
include Singleton
# @todo Durran: This module needs an overhaul, remove singleton, etc.
module Config
extend self

class_inheritable_accessor :options
self.options = []
attr_accessor :settings
@settings = {}

class << self

# Define a configuration option with a default.
#
# @example Define the option.
# Config.option(:persist_in_safe_mode, :default => false)
#
# @param [ Symbol ] name The name of the configuration option.
# @param [ Hash ] options Extras for the option.
#
# @option options [ Object ] :default The default value.
#
# @since 2.0.0.rc.1
def option(name, options = {})
self.options << name

define_method(name) { has_key?(name) ? self[name] : options[:default] }
define_method("#{name}=") { |value| self[name] = value }
define_method("#{name}?") { send(name) }
# Define a configuration option with a default.
#
# @example Define the option.
# Config.option(:persist_in_safe_mode, :default => false)
#
# @param [ Symbol ] name The name of the configuration option.
# @param [ Hash ] options Extras for the option.
#
# @option options [ Object ] :default The default value.
#
# @since 2.0.0.rc.1
def option(name, options = {})
define_method(name) do
settings.has_key?(name) ? settings[name] : options[:default]
end
define_method("#{name}=") { |value| settings[name] = value }
define_method("#{name}?") { send(name) }
end

option :allow_dynamic_fields, :default => true
Expand All @@ -44,7 +41,6 @@ def option(name, options = {})
option :autocreate_indexes, :default => false
option :skip_version_check, :default => false
option :time_zone, :default => nil
option :logger, :default => defined?(Rails) ? Rails.logger : ::Logger.new($stdout)

# Adds a new I18n locale file to the load path.
#
Expand All @@ -56,12 +52,10 @@ def option(name, options = {})
#
# @param [ String ] language_code The language to add.
def add_language(language_code = nil)
Dir[ File.join(
File.dirname(__FILE__),
"..",
"config",
"locales",
"#{language_code}.yml")
Dir[
File.join(
File.dirname(__FILE__), "..", "config", "locales", "#{language_code}.yml"
)
].each do |file|
I18n.load_path << File.expand_path(file)
end
Expand Down Expand Up @@ -89,13 +83,33 @@ def destructive_fields
# @example Configure Mongoid.
# config.from_hash({})
#
# @param [ Hash ] settings The settings to use.
def from_hash(settings)
settings.except("database", "slaves").each_pair do |name, value|
# @param [ Hash ] options The settings to use.
def from_hash(options = {})
options.except("database", "slaves").each_pair do |name, value|
send("#{name}=", value) if respond_to?("#{name}=")
end
_master(settings)
_slaves(settings)
_master(options)
_slaves(options)
end

# Returns the logger, or defaults to Rails logger or stdout logger.
#
# @example Get the logger.
# config.logger
#
# @return [ Logger ] The desired logger.
def logger
@logger ||= defined?(Rails) ? Rails.logger : ::Logger.new($stdout)
end

# Sets the logger for Mongoid to use.
#
# @example Set the logger.
# config.logger = Logger.new($stdout, :warn)
#
# @return [ Logger ] The newly set logger.
def logger=(logger)
@logger = logger
end

# Sets the Mongo::DB master database to be used. If the object trying to be
Expand Down Expand Up @@ -134,16 +148,6 @@ def master
end
alias :database :master

# Get the list of defined options in the configuration.
#
# @example Get the options.
# config.options
#
# @return [ Array ] The list of options.
def options
self.class.options
end

# Convenience method for connecting to the master database after forking a
# new process.
#
Expand All @@ -166,7 +170,7 @@ def reconnect!(now = true)
# @example Reset the configuration options.
# config.reset
def reset
options.each { |option| delete(option) }
settings.clear
end

# Sets the Mongo::DB slave databases to be used. If the objects provided
Expand Down Expand Up @@ -246,16 +250,16 @@ def check_database!(database)
# @example Configure the master db.
# config._master({}, "test")
#
# @param [ Hash ] settings The settings to use.
def _master(settings)
mongo_uri = settings["uri"].present? ? URI.parse(settings["uri"]) : OpenStruct.new
# @param [ Hash ] options The options to use.
def _master(options)
mongo_uri = options["uri"].present? ? URI.parse(options["uri"]) : OpenStruct.new

name = settings["database"] || mongo_uri.path.to_s.sub("/", "")
host = settings["host"] || mongo_uri.host || "localhost"
port = settings["port"] || mongo_uri.port || 27017
pool_size = settings["pool_size"] || 1
username = settings["username"] || mongo_uri.user
password = settings["password"] || mongo_uri.password
name = options["database"] || mongo_uri.path.to_s.sub("/", "")
host = options["host"] || mongo_uri.host || "localhost"
port = options["port"] || mongo_uri.port || 27017
pool_size = options["pool_size"] || 1
username = options["username"] || mongo_uri.user
password = options["password"] || mongo_uri.password

connection = Mongo::Connection.new(host, port, :logger => Mongoid::Logger.new, :pool_size => pool_size)
if username || password
Expand All @@ -265,17 +269,17 @@ def _master(settings)
self.master = connection.db(name)
end

# Get a bunch-o-slaves from settings and names.
# Get a bunch-o-slaves from options and names.
#
# @example Configure the slaves.
# config._slaves({}, "test")
#
# @param [ Hash ] settings The settings to use.
def _slaves(settings)
mongo_uri = settings["uri"].present? ? URI.parse(settings["uri"]) : OpenStruct.new
name = settings["database"] || mongo_uri.path.to_s.sub("/", "")
# @param [ Hash ] options The options to use.
def _slaves(options)
mongo_uri = options["uri"].present? ? URI.parse(options["uri"]) : OpenStruct.new
name = options["database"] || mongo_uri.path.to_s.sub("/", "")
self.slaves = []
slaves = settings["slaves"]
slaves = options["slaves"]
slaves.to_a.each do |slave|
slave_uri = slave["uri"].present? ? URI.parse(slave["uri"]) : OpenStruct.new
slave_username = slave["username"] || slave_uri.user
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/extensions/date/conversions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Date #:nodoc:
module Conversions #:nodoc:
def get(value)
return nil if value.blank?
if Mongoid::Config.instance.use_utc?
if Mongoid::Config.use_utc?
value.to_date
else
::Date.new(value.year, value.month, value.day)
Expand Down
4 changes: 2 additions & 2 deletions lib/mongoid/extensions/time_conversions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def set(value)

def get(value)
return nil if value.blank?
if Mongoid::Config.instance.use_utc?
if Mongoid::Config.use_utc?
value
else
value.getlocal
Expand All @@ -32,4 +32,4 @@ def convert_to_time(value)
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/mongoid/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Railtie < Rails::Railtie #:nodoc:
# config.mongoid.reconnect_time = 10
# end
# end
config.mongoid = ::Mongoid::Config.instance
config.mongoid = ::Mongoid::Config

# Initialize Mongoid. This will look for a mongoid.yml in the config
# directory and configure mongoid appropriately.
Expand Down
12 changes: 11 additions & 1 deletion spec/unit/mongoid/config_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
require "spec_helper"

describe Mongoid::Config do
let(:config) { Class.new(Mongoid::Config).instance }
let(:config) { described_class }

before do
config.reset
end

after(:all) do
Mongoid.configure do |config|
name = "mongoid_test"
host = "localhost"
config.master = Mongo::Connection.new.db(name)
config.logger = nil
end
end

describe "#database=" do

context "when object provided is not a Mongo::DB" do
Expand Down Expand Up @@ -287,6 +296,7 @@
describe "#master" do
before do
config.send(:instance_variable_set, :@master, master)
config.send(:instance_variable_set, :@reconnect, false)
end

context "when the database has not been configured" do
Expand Down
10 changes: 5 additions & 5 deletions spec/unit/mongoid/extensions/date/conversions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@

context "when the time zone is not defined" do
before do
Mongoid::Config.instance.use_utc = false
Mongoid::Config.use_utc = false
end

context "when the local time is not observing daylight saving" do
Expand All @@ -102,8 +102,8 @@
end

context "when the time zone is defined as UTC" do
before { Mongoid::Config.instance.use_utc = true }
after { Mongoid::Config.instance.use_utc = false }
before { Mongoid::Config.use_utc = true }
after { Mongoid::Config.use_utc = false }

it "returns the same day" do
Date.get(@time.dup.utc).day.should == @time.day
Expand All @@ -120,7 +120,7 @@
describe "round trip - set then get" do
context "when the time zone is not defined" do
before do
Mongoid::Config.instance.use_utc = false
Mongoid::Config.use_utc = false
Time.zone = "Stockholm"
end
after { Time.zone = nil }
Expand All @@ -142,4 +142,4 @@
end
end
end
end
end
12 changes: 6 additions & 6 deletions spec/unit/mongoid/extensions/time_conversions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@

describe ".get" do
context "when the time zone is not defined" do
before { Mongoid::Config.instance.use_utc = false }
before { Mongoid::Config.use_utc = false }

context "when the local time is not observing daylight saving" do
before { @time = Time.utc(2010, 11, 19) }
Expand Down Expand Up @@ -101,8 +101,8 @@
end

context "when the time zone is defined as UTC" do
before { Mongoid::Config.instance.use_utc = true }
after { Mongoid::Config.instance.use_utc = false }
before { Mongoid::Config.use_utc = true }
after { Mongoid::Config.use_utc = false }

it "returns utc" do
Time.get(@time.dup.utc).utc_offset.should == 0
Expand All @@ -118,7 +118,7 @@

describe "round trip - set then get" do
context "when the time zone is not defined" do
before { Mongoid::Config.instance.use_utc = false }
before { Mongoid::Config.use_utc = false }

context "when the local time is not observing daylight saving" do
before { @time = Time.set(Time.local(2010, 11, 19)) }
Expand Down Expand Up @@ -149,8 +149,8 @@
end

context "when the time zone is defined as UTC" do
before { Mongoid::Config.instance.use_utc = true }
after { Mongoid::Config.instance.use_utc = false }
before { Mongoid::Config.use_utc = true }
after { Mongoid::Config.use_utc = false }

context "when the local time is not observing daylight saving" do
before { @time = Time.set(Time.local(2010, 11, 19)) }
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/mongoid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
context "when no block supplied" do

it "returns the config singleton" do
Mongoid.configure.should == Mongoid::Config.instance
Mongoid.configure.should == Mongoid::Config
end
end

Expand Down

0 comments on commit ba7e2ea

Please sign in to comment.