mongoid-with-auth 1.9.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. data/MIT_LICENSE +20 -0
  2. data/README.rdoc +49 -0
  3. data/lib/mongoid.rb +122 -0
  4. data/lib/mongoid/associations.rb +300 -0
  5. data/lib/mongoid/associations/belongs_to_related.rb +58 -0
  6. data/lib/mongoid/associations/embedded_in.rb +72 -0
  7. data/lib/mongoid/associations/embeds_many.rb +254 -0
  8. data/lib/mongoid/associations/embeds_one.rb +96 -0
  9. data/lib/mongoid/associations/has_many_related.rb +181 -0
  10. data/lib/mongoid/associations/has_one_related.rb +85 -0
  11. data/lib/mongoid/associations/meta_data.rb +29 -0
  12. data/lib/mongoid/associations/options.rb +57 -0
  13. data/lib/mongoid/associations/proxy.rb +24 -0
  14. data/lib/mongoid/attributes.rb +204 -0
  15. data/lib/mongoid/callbacks.rb +23 -0
  16. data/lib/mongoid/collection.rb +120 -0
  17. data/lib/mongoid/collections.rb +41 -0
  18. data/lib/mongoid/collections/cyclic_iterator.rb +34 -0
  19. data/lib/mongoid/collections/master.rb +29 -0
  20. data/lib/mongoid/collections/operations.rb +41 -0
  21. data/lib/mongoid/collections/slaves.rb +45 -0
  22. data/lib/mongoid/components.rb +27 -0
  23. data/lib/mongoid/concern.rb +31 -0
  24. data/lib/mongoid/config.rb +205 -0
  25. data/lib/mongoid/contexts.rb +25 -0
  26. data/lib/mongoid/contexts/enumerable.rb +151 -0
  27. data/lib/mongoid/contexts/ids.rb +25 -0
  28. data/lib/mongoid/contexts/mongo.rb +285 -0
  29. data/lib/mongoid/contexts/paging.rb +50 -0
  30. data/lib/mongoid/criteria.rb +239 -0
  31. data/lib/mongoid/criterion/complex.rb +21 -0
  32. data/lib/mongoid/criterion/exclusion.rb +65 -0
  33. data/lib/mongoid/criterion/inclusion.rb +110 -0
  34. data/lib/mongoid/criterion/optional.rb +136 -0
  35. data/lib/mongoid/cursor.rb +81 -0
  36. data/lib/mongoid/deprecation.rb +22 -0
  37. data/lib/mongoid/dirty.rb +253 -0
  38. data/lib/mongoid/document.rb +311 -0
  39. data/lib/mongoid/errors.rb +108 -0
  40. data/lib/mongoid/extensions.rb +101 -0
  41. data/lib/mongoid/extensions/array/accessors.rb +17 -0
  42. data/lib/mongoid/extensions/array/aliasing.rb +4 -0
  43. data/lib/mongoid/extensions/array/assimilation.rb +26 -0
  44. data/lib/mongoid/extensions/array/conversions.rb +29 -0
  45. data/lib/mongoid/extensions/array/parentization.rb +13 -0
  46. data/lib/mongoid/extensions/big_decimal/conversions.rb +19 -0
  47. data/lib/mongoid/extensions/binary/conversions.rb +17 -0
  48. data/lib/mongoid/extensions/boolean/conversions.rb +22 -0
  49. data/lib/mongoid/extensions/date/conversions.rb +24 -0
  50. data/lib/mongoid/extensions/datetime/conversions.rb +12 -0
  51. data/lib/mongoid/extensions/float/conversions.rb +20 -0
  52. data/lib/mongoid/extensions/hash/accessors.rb +38 -0
  53. data/lib/mongoid/extensions/hash/assimilation.rb +39 -0
  54. data/lib/mongoid/extensions/hash/conversions.rb +45 -0
  55. data/lib/mongoid/extensions/hash/criteria_helpers.rb +20 -0
  56. data/lib/mongoid/extensions/hash/scoping.rb +12 -0
  57. data/lib/mongoid/extensions/integer/conversions.rb +20 -0
  58. data/lib/mongoid/extensions/nil/assimilation.rb +17 -0
  59. data/lib/mongoid/extensions/object/conversions.rb +33 -0
  60. data/lib/mongoid/extensions/objectid/conversions.rb +15 -0
  61. data/lib/mongoid/extensions/proc/scoping.rb +12 -0
  62. data/lib/mongoid/extensions/string/conversions.rb +15 -0
  63. data/lib/mongoid/extensions/string/inflections.rb +97 -0
  64. data/lib/mongoid/extensions/symbol/inflections.rb +36 -0
  65. data/lib/mongoid/extensions/time_conversions.rb +35 -0
  66. data/lib/mongoid/extras.rb +61 -0
  67. data/lib/mongoid/factory.rb +20 -0
  68. data/lib/mongoid/field.rb +59 -0
  69. data/lib/mongoid/fields.rb +65 -0
  70. data/lib/mongoid/finders.rb +136 -0
  71. data/lib/mongoid/identity.rb +39 -0
  72. data/lib/mongoid/indexes.rb +30 -0
  73. data/lib/mongoid/javascript.rb +21 -0
  74. data/lib/mongoid/javascript/functions.yml +37 -0
  75. data/lib/mongoid/matchers.rb +36 -0
  76. data/lib/mongoid/matchers/all.rb +11 -0
  77. data/lib/mongoid/matchers/default.rb +26 -0
  78. data/lib/mongoid/matchers/exists.rb +13 -0
  79. data/lib/mongoid/matchers/gt.rb +11 -0
  80. data/lib/mongoid/matchers/gte.rb +11 -0
  81. data/lib/mongoid/matchers/in.rb +11 -0
  82. data/lib/mongoid/matchers/lt.rb +11 -0
  83. data/lib/mongoid/matchers/lte.rb +11 -0
  84. data/lib/mongoid/matchers/ne.rb +11 -0
  85. data/lib/mongoid/matchers/nin.rb +11 -0
  86. data/lib/mongoid/matchers/size.rb +11 -0
  87. data/lib/mongoid/memoization.rb +33 -0
  88. data/lib/mongoid/named_scope.rb +37 -0
  89. data/lib/mongoid/observable.rb +30 -0
  90. data/lib/mongoid/paths.rb +62 -0
  91. data/lib/mongoid/persistence.rb +222 -0
  92. data/lib/mongoid/persistence/command.rb +39 -0
  93. data/lib/mongoid/persistence/insert.rb +50 -0
  94. data/lib/mongoid/persistence/insert_embedded.rb +38 -0
  95. data/lib/mongoid/persistence/remove.rb +39 -0
  96. data/lib/mongoid/persistence/remove_all.rb +37 -0
  97. data/lib/mongoid/persistence/remove_embedded.rb +50 -0
  98. data/lib/mongoid/persistence/update.rb +63 -0
  99. data/lib/mongoid/scope.rb +75 -0
  100. data/lib/mongoid/state.rb +39 -0
  101. data/lib/mongoid/timestamps.rb +27 -0
  102. data/lib/mongoid/version.rb +4 -0
  103. data/lib/mongoid/versioning.rb +27 -0
  104. metadata +284 -0
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Callbacks
4
+ def self.included(base)
5
+ base.class_eval do
6
+ include ActiveSupport::Callbacks
7
+
8
+ # Define all the callbacks that are accepted by the document.
9
+ define_callbacks \
10
+ :before_create,
11
+ :after_create,
12
+ :before_destroy,
13
+ :after_destroy,
14
+ :before_save,
15
+ :after_save,
16
+ :before_update,
17
+ :after_update,
18
+ :before_validation,
19
+ :after_validation
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,120 @@
1
+ # encoding: utf-8
2
+ require "mongoid/collections/operations"
3
+ require "mongoid/collections/cyclic_iterator"
4
+ require "mongoid/collections/master"
5
+ require "mongoid/collections/slaves"
6
+
7
+ module Mongoid #:nodoc
8
+ # The Mongoid wrapper to the Mongo Ruby driver's collection object.
9
+ class Collection
10
+ attr_reader :counter, :name
11
+
12
+ # All write operations should delegate to the master connection. These
13
+ # operations mimic the methods on a Mongo:Collection.
14
+ #
15
+ # Example:
16
+ #
17
+ # <tt>collection.save({ :name => "Al" })</tt>
18
+ Collections::Operations::PROXIED.each do |name|
19
+ define_method(name) { |*args| master.send(name, *args) }
20
+ end
21
+
22
+ # Determines where to send the next read query. If the slaves are not
23
+ # defined then send to master. If the read counter is under the configured
24
+ # maximum then return the master. In any other case return the slaves.
25
+ #
26
+ # Example:
27
+ #
28
+ # <tt>collection.directed</tt>
29
+ #
30
+ # Return:
31
+ #
32
+ # Either a +Master+ or +Slaves+ collection.
33
+ def directed(options = {})
34
+ options.delete(:cache)
35
+ enslave = options.delete(:enslave) || @klass.enslaved?
36
+ enslave ? master_or_slaves : master
37
+ end
38
+
39
+ # Find documents from the database given a selector and options.
40
+ #
41
+ # Options:
42
+ #
43
+ # selector: A +Hash+ selector that is the query.
44
+ # options: The options to pass to the db.
45
+ #
46
+ # Example:
47
+ #
48
+ # <tt>collection.find({ :test => "value" })</tt>
49
+ def find(selector = {}, options = {})
50
+ cursor = Mongoid::Cursor.new(@klass, self, directed(options).find(selector, options))
51
+ if block_given?
52
+ yield cursor; cursor.close
53
+ else
54
+ cursor
55
+ end
56
+ end
57
+
58
+ # Find the first document from the database given a selector and options.
59
+ #
60
+ # Options:
61
+ #
62
+ # selector: A +Hash+ selector that is the query.
63
+ # options: The options to pass to the db.
64
+ #
65
+ # Example:
66
+ #
67
+ # <tt>collection.find_one({ :test => "value" })</tt>
68
+ def find_one(selector = {}, options = {})
69
+ directed(options).find_one(selector, options)
70
+ end
71
+
72
+ # Initialize a new Mongoid::Collection, setting up the master, slave, and
73
+ # name attributes. Masters will be used for writes, slaves for reads.
74
+ #
75
+ # Example:
76
+ #
77
+ # <tt>Mongoid::Collection.new(masters, slaves, "test")</tt>
78
+ def initialize(klass, name)
79
+ @klass, @name = klass, name
80
+ end
81
+
82
+ # Perform a map/reduce on the documents.
83
+ #
84
+ # Options:
85
+ #
86
+ # map: The map javascript funcdtion.
87
+ # reduce: The reduce javascript function.
88
+ def map_reduce(map, reduce, options = {})
89
+ directed(options).map_reduce(map, reduce, options)
90
+ end
91
+
92
+ alias :mapreduce :map_reduce
93
+
94
+ # Return the object responsible for writes to the database. This will
95
+ # always return a collection associated with the Master DB.
96
+ #
97
+ # Example:
98
+ #
99
+ # <tt>collection.writer</tt>
100
+ def master
101
+ @master ||= Collections::Master.new(Mongoid.master, @name)
102
+ end
103
+
104
+ # Return the object responsible for reading documents from the database.
105
+ # This is usually the slave databases, but in their absence the master will
106
+ # handle the task.
107
+ #
108
+ # Example:
109
+ #
110
+ # <tt>collection.reader</tt>
111
+ def slaves
112
+ @slaves ||= Collections::Slaves.new(Mongoid.slaves, @name)
113
+ end
114
+
115
+ protected
116
+ def master_or_slaves
117
+ slaves.empty? ? master : slaves
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc
3
+ # The collections module is used for providing functionality around setting
4
+ # up and updating collections.
5
+ module Collections
6
+ extend ActiveSupport::Concern
7
+ included do
8
+ cattr_accessor :_collection, :collection_name
9
+ self.collection_name = self.name.collectionize
10
+ delegate :collection, :to => "self.class"
11
+ end
12
+
13
+ module ClassMethods #:nodoc:
14
+ # Returns the collection associated with this +Document+. If the
15
+ # document is embedded, there will be no collection associated
16
+ # with it.
17
+ #
18
+ # Returns: <tt>Mongo::Collection</tt>
19
+ def collection
20
+ raise Errors::InvalidCollection.new(self) if embedded?
21
+ self._collection || set_collection
22
+ add_indexes; self._collection
23
+ end
24
+
25
+ # Macro for setting the collection name to store in.
26
+ #
27
+ # Example:
28
+ #
29
+ # <tt>Person.store_in :populdation</tt>
30
+ def store_in(name)
31
+ self.collection_name = name.to_s
32
+ set_collection
33
+ end
34
+
35
+ protected
36
+ def set_collection
37
+ self._collection = Mongoid::Collection.new(self, self.collection_name)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Collections #:nodoc:
4
+ class CyclicIterator
5
+
6
+ attr_reader :counter
7
+
8
+ # Performs iteration over an array, if the array gets to the end then loop
9
+ # back to the first.
10
+ #
11
+ # Example:
12
+ #
13
+ # <tt>CyclicIterator.new([ first, second ])</tt>
14
+ def initialize(array)
15
+ @array, @counter = array, -1
16
+ end
17
+
18
+ # Get the next element in the array. If the element is the last in the
19
+ # array then return the first.
20
+ #
21
+ # Example:
22
+ #
23
+ # <tt>iterator.next</tt>
24
+ #
25
+ # Returns:
26
+ #
27
+ # The next element in the array.
28
+ def next
29
+ (@counter == @array.size - 1) ? @counter = 0 : @counter = @counter + 1
30
+ @array[@counter]
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Collections #:nodoc:
4
+ class Master
5
+
6
+ attr_reader :collection
7
+
8
+ # All read and write operations should delegate to the master connection.
9
+ # These operations mimic the methods on a Mongo:Collection.
10
+ #
11
+ # Example:
12
+ #
13
+ # <tt>collection.save({ :name => "Al" })</tt>
14
+ Operations::ALL.each do |name|
15
+ define_method(name) { |*args| collection.send(name, *args) }
16
+ end
17
+
18
+ # Create the new database writer. Will create a collection from the
19
+ # master database.
20
+ #
21
+ # Example:
22
+ #
23
+ # <tt>Master.new(master, "mongoid_people")</tt>
24
+ def initialize(master, name)
25
+ @collection = master.collection(name)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Collections #:nodoc:
4
+ module Operations #:nodoc:
5
+ # Constant definining all the read operations available for a
6
+ # Mongo:Collection. This is used in delegation.
7
+ READ = [
8
+ :[],
9
+ :db,
10
+ :count,
11
+ :distinct,
12
+ :find,
13
+ :find_one,
14
+ :group,
15
+ :index_information,
16
+ :map_reduce,
17
+ :mapreduce,
18
+ :options
19
+ ]
20
+
21
+ # Constant definining all the write operations available for a
22
+ # Mongo:Collection. This is used in delegation.
23
+ WRITE = [
24
+ :<<,
25
+ :create_index,
26
+ :drop,
27
+ :drop_index,
28
+ :drop_indexes,
29
+ :insert,
30
+ :remove,
31
+ :rename,
32
+ :save,
33
+ :update
34
+ ]
35
+
36
+ # Convenience constant for getting back all collection operations.
37
+ ALL = (READ + WRITE)
38
+ PROXIED = ALL - [ :find, :find_one, :map_reduce, :mapreduce ]
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Collections #:nodoc:
4
+ class Slaves
5
+
6
+ attr_reader :iterator
7
+
8
+ # All read operations should delegate to the slave connections.
9
+ # These operations mimic the methods on a Mongo:Collection.
10
+ #
11
+ # Example:
12
+ #
13
+ # <tt>collection.save({ :name => "Al" })</tt>
14
+ Operations::READ.each do |name|
15
+ define_method(name) { |*args| collection.send(name, *args) }
16
+ end
17
+
18
+ # Is the collection of slaves empty or not?
19
+ #
20
+ # Return:
21
+ #
22
+ # True is the iterator is not set, false if not.
23
+ def empty?
24
+ @iterator.nil?
25
+ end
26
+
27
+ # Create the new database reader. Will create a collection from the
28
+ # slave databases and cycle through them on each read.
29
+ #
30
+ # Example:
31
+ #
32
+ # <tt>Reader.new(slaves, "mongoid_people")</tt>
33
+ def initialize(slaves, name)
34
+ unless slaves.blank?
35
+ @iterator = CyclicIterator.new(slaves.collect { |db| db.collection(name) })
36
+ end
37
+ end
38
+
39
+ protected
40
+ def collection
41
+ @iterator.next
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc
3
+ module Components #:nodoc
4
+ extend ActiveSupport::Concern
5
+ included do
6
+ # All modules that a +Document+ is composed of are defined in this
7
+ # module, to keep the document class from getting too cluttered.
8
+ include Mongoid::Associations
9
+ include Mongoid::Attributes
10
+ include Mongoid::Callbacks
11
+ include Mongoid::Collections
12
+ include Mongoid::Dirty
13
+ include Mongoid::Extras
14
+ include Mongoid::Fields
15
+ include Mongoid::Indexes
16
+ include Mongoid::Matchers
17
+ include Mongoid::Memoization
18
+ include Mongoid::Observable
19
+ include Mongoid::Paths
20
+ include Mongoid::Persistence
21
+ include Mongoid::State
22
+ include Validatable
23
+ extend Mongoid::Finders
24
+ extend Mongoid::NamedScope
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,31 @@
1
+ unless defined?(ActiveSupport::Concern)
2
+ module ActiveSupport
3
+ module Concern
4
+ def self.extended(base)
5
+ base.instance_variable_set("@_dependencies", [])
6
+ end
7
+
8
+ def append_features(base)
9
+ if base.instance_variable_defined?("@_dependencies")
10
+ base.instance_variable_get("@_dependencies") << self
11
+ return false
12
+ else
13
+ return false if base < self
14
+ @_dependencies.each { |dep| base.send(:include, dep) }
15
+ super
16
+ base.extend const_get("ClassMethods") if const_defined?("ClassMethods")
17
+ base.send :include, const_get("InstanceMethods") if const_defined?("InstanceMethods")
18
+ base.class_eval(&@_included_block) if instance_variable_defined?("@_included_block")
19
+ end
20
+ end
21
+
22
+ def included(base = nil, &block)
23
+ if base.nil?
24
+ @_included_block = block
25
+ else
26
+ super
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,205 @@
1
+ # encoding: utf-8
2
+ require "uri"
3
+
4
+ module Mongoid #:nodoc
5
+ class Config #:nodoc
6
+ include Singleton
7
+
8
+ attr_accessor \
9
+ :allow_dynamic_fields,
10
+ :reconnect_time,
11
+ :parameterize_keys,
12
+ :persist_in_safe_mode,
13
+ :raise_not_found_error,
14
+ :use_object_ids
15
+
16
+ # Defaults the configuration options to true.
17
+ def initialize
18
+ reset
19
+ end
20
+
21
+ # Sets whether the times returned from the database are in UTC or local time.
22
+ # If you omit this setting, then times will be returned in
23
+ # the local time zone.
24
+ #
25
+ # Example:
26
+ #
27
+ # <tt>Config.use_utc = true</tt>
28
+ #
29
+ # Returns:
30
+ #
31
+ # A boolean
32
+ def use_utc=(value)
33
+ @use_utc = value || false
34
+ end
35
+
36
+ # Returns whether times are return from the database in UTC. If
37
+ # this setting is false, then times will be returned in the local time zone.
38
+ #
39
+ # Example:
40
+ #
41
+ # <tt>Config.use_utc</tt>
42
+ #
43
+ # Returns:
44
+ #
45
+ # A boolean
46
+ attr_reader :use_utc
47
+ alias_method :use_utc?, :use_utc
48
+
49
+ # Sets the Mongo::DB master database to be used. If the object trying to be
50
+ # set is not a valid +Mongo::DB+, then an error will be raised.
51
+ #
52
+ # Example:
53
+ #
54
+ # <tt>Config.master = Mongo::Connection.db("test")</tt>
55
+ #
56
+ # Returns:
57
+ #
58
+ # The Master DB instance.
59
+ def master=(db)
60
+ check_database!(db)
61
+ @master = db
62
+ end
63
+
64
+ # Returns the master database, or if none has been set it will raise an
65
+ # error.
66
+ #
67
+ # Example:
68
+ #
69
+ # <tt>Config.master</tt>
70
+ #
71
+ # Returns:
72
+ #
73
+ # The master +Mongo::DB+
74
+ def master
75
+ @master || (raise Errors::InvalidDatabase.new(nil))
76
+ end
77
+
78
+ alias :database :master
79
+ alias :database= :master=
80
+
81
+ # Sets the Mongo::DB slave databases to be used. If the objects provided
82
+ # are not valid +Mongo::DBs+ an error will be raised.
83
+ #
84
+ # Example:
85
+ #
86
+ # <tt>Config.slaves = [ Mongo::Connection.db("test") ]</tt>
87
+ #
88
+ # Returns:
89
+ #
90
+ # The slave DB instances.
91
+ def slaves=(dbs)
92
+ return unless dbs
93
+ dbs.each do |db|
94
+ check_database!(db)
95
+ end
96
+ @slaves = dbs
97
+ end
98
+
99
+ # Returns the slave databases, or if none has been set nil
100
+ #
101
+ # Example:
102
+ #
103
+ # <tt>Config.slaves</tt>
104
+ #
105
+ # Returns:
106
+ #
107
+ # The slave +Mongo::DBs+
108
+ def slaves
109
+ @slaves
110
+ end
111
+
112
+ # Confiure mongoid from a hash that was usually parsed out of yml.
113
+ #
114
+ # Example:
115
+ #
116
+ # <tt>Mongoid::Config.instance.from_hash({})</tt>
117
+ def from_hash(settings)
118
+ _master(settings)
119
+ _slaves(settings)
120
+ settings.except("database").each_pair do |name, value|
121
+ send("#{name}=", value) if respond_to?(name)
122
+ end
123
+ end
124
+
125
+ # Reset the configuration options to the defaults.
126
+ #
127
+ # Example:
128
+ #
129
+ # <tt>config.reset</tt>
130
+ def reset
131
+ @allow_dynamic_fields = true
132
+ @parameterize_keys = true
133
+ @persist_in_safe_mode = true
134
+ @raise_not_found_error = true
135
+ @reconnect_time = 3
136
+ @use_object_ids = false
137
+ @time_zone = nil
138
+ end
139
+
140
+ protected
141
+
142
+ # Check if the database is valid and the correct version.
143
+ #
144
+ # Example:
145
+ #
146
+ # <tt>config.check_database!</tt>
147
+ def check_database!(database)
148
+ raise Errors::InvalidDatabase.new(database) unless database.kind_of?(Mongo::DB)
149
+ version = database.connection.server_version
150
+ raise Errors::UnsupportedVersion.new(version) if version < Mongoid::MONGODB_VERSION
151
+ end
152
+
153
+ # Get a Rails logger or stdout logger.
154
+ #
155
+ # Example:
156
+ #
157
+ # <tt>config.logger</tt>
158
+ def logger
159
+ defined?(Rails) ? Rails.logger : Logger.new($stdout)
160
+ end
161
+
162
+ # Get a master database from settings.
163
+ #
164
+ # Example:
165
+ #
166
+ # <tt>config._master({}, "test")</tt>
167
+ def _master(settings)
168
+ self.master = database_from_hash(settings)
169
+ end
170
+
171
+ # Get a bunch-o-slaves from settings and names.
172
+ #
173
+ # Example:
174
+ #
175
+ # <tt>config._slaves({}, "test")</tt>
176
+ def _slaves(settings)
177
+ self.slaves = settings["slaves"].to_a.map do |slave|
178
+ database_from_hash({"database" => master.name}.merge(slave), :slave_ok => true)
179
+ end
180
+ end
181
+
182
+ def database_from_hash(settings, connection_options={})
183
+ mongo_uri = settings["uri"].present? ? URI.parse(settings["uri"]) : OpenStruct.new
184
+
185
+ name = settings["database"] || mongo_uri.path.to_s.sub("/", "")
186
+ host = settings["host"] || mongo_uri.host || "localhost"
187
+ port = settings["port"] || mongo_uri.port || 27017
188
+ pool_size = settings["pool_size"] || 1
189
+ username = settings["username"] || mongo_uri.user
190
+ password = settings["password"] || mongo_uri.password
191
+
192
+ local_options = {
193
+ :logger => logger,
194
+ :pool_size => pool_size
195
+ }.merge(connection_options)
196
+
197
+ Mongo::Connection.new(host, port, local_options).tap do |connection|
198
+ if username || password
199
+ connection.add_auth(name, username, password)
200
+ connection.apply_saved_authentication
201
+ end
202
+ end.db(name)
203
+ end
204
+ end
205
+ end