mongoid 1.2.14 → 1.9.0

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 (200) hide show
  1. data/lib/mongoid.rb +10 -3
  2. data/lib/mongoid/associations.rb +133 -97
  3. data/lib/mongoid/associations/belongs_to_related.rb +2 -3
  4. data/lib/mongoid/associations/{belongs_to.rb → embedded_in.rb} +14 -6
  5. data/lib/mongoid/associations/{has_many.rb → embeds_many.rb} +89 -31
  6. data/lib/mongoid/associations/{has_one.rb → embeds_one.rb} +8 -7
  7. data/lib/mongoid/associations/has_many_related.rb +52 -7
  8. data/lib/mongoid/associations/has_one_related.rb +8 -4
  9. data/lib/mongoid/associations/meta_data.rb +2 -1
  10. data/lib/mongoid/associations/options.rb +6 -1
  11. data/lib/mongoid/associations/proxy.rb +14 -21
  12. data/lib/mongoid/attributes.rb +27 -12
  13. data/lib/mongoid/collection.rb +4 -3
  14. data/lib/mongoid/collections.rb +41 -0
  15. data/lib/mongoid/collections/master.rb +3 -2
  16. data/lib/mongoid/collections/slaves.rb +3 -2
  17. data/lib/mongoid/components.rb +21 -19
  18. data/lib/mongoid/concern.rb +31 -0
  19. data/lib/mongoid/config.rb +117 -12
  20. data/lib/mongoid/contexts.rb +1 -1
  21. data/lib/mongoid/contexts/enumerable.rb +1 -1
  22. data/lib/mongoid/contexts/mongo.rb +1 -1
  23. data/lib/mongoid/contexts/paging.rb +10 -2
  24. data/lib/mongoid/criterion/inclusion.rb +17 -0
  25. data/lib/mongoid/criterion/optional.rb +1 -1
  26. data/lib/mongoid/dirty.rb +253 -0
  27. data/lib/mongoid/document.rb +81 -52
  28. data/lib/mongoid/errors.rb +32 -1
  29. data/lib/mongoid/extensions.rb +11 -9
  30. data/lib/mongoid/extensions/big_decimal/conversions.rb +2 -2
  31. data/lib/mongoid/extensions/boolean/conversions.rb +8 -2
  32. data/lib/mongoid/extensions/date/conversions.rb +13 -4
  33. data/lib/mongoid/extensions/datetime/conversions.rb +1 -6
  34. data/lib/mongoid/extensions/float/conversions.rb +5 -1
  35. data/lib/mongoid/extensions/hash/assimilation.rb +12 -3
  36. data/lib/mongoid/extensions/hash/conversions.rb +34 -4
  37. data/lib/mongoid/extensions/integer/conversions.rb +5 -1
  38. data/lib/mongoid/extensions/nil/assimilation.rb +4 -0
  39. data/lib/mongoid/extensions/object/conversions.rb +1 -1
  40. data/lib/mongoid/extensions/string/conversions.rb +1 -1
  41. data/lib/mongoid/extensions/symbol/inflections.rb +1 -1
  42. data/lib/mongoid/extensions/time_conversions.rb +35 -0
  43. data/lib/mongoid/extras.rb +6 -9
  44. data/lib/mongoid/factory.rb +2 -1
  45. data/lib/mongoid/field.rb +9 -2
  46. data/lib/mongoid/fields.rb +1 -0
  47. data/lib/mongoid/identity.rb +3 -3
  48. data/lib/mongoid/indexes.rb +3 -3
  49. data/lib/mongoid/memoization.rb +8 -2
  50. data/lib/mongoid/named_scope.rb +0 -3
  51. data/lib/mongoid/observable.rb +30 -0
  52. data/lib/mongoid/paths.rb +62 -0
  53. data/lib/mongoid/persistence.rb +222 -0
  54. data/lib/mongoid/persistence/command.rb +39 -0
  55. data/lib/mongoid/persistence/insert.rb +50 -0
  56. data/lib/mongoid/persistence/insert_embedded.rb +38 -0
  57. data/lib/mongoid/persistence/remove.rb +39 -0
  58. data/lib/mongoid/persistence/remove_all.rb +37 -0
  59. data/lib/mongoid/persistence/remove_embedded.rb +50 -0
  60. data/lib/mongoid/persistence/update.rb +63 -0
  61. data/lib/mongoid/state.rb +28 -21
  62. data/lib/mongoid/timestamps.rb +5 -8
  63. data/lib/mongoid/version.rb +4 -0
  64. data/lib/mongoid/versioning.rb +6 -7
  65. metadata +81 -300
  66. data/.gitignore +0 -6
  67. data/.watchr +0 -29
  68. data/Rakefile +0 -53
  69. data/VERSION +0 -1
  70. data/caliper.yml +0 -4
  71. data/lib/mongoid/collections/mimic.rb +0 -46
  72. data/lib/mongoid/commands.rb +0 -174
  73. data/lib/mongoid/commands/create.rb +0 -21
  74. data/lib/mongoid/commands/delete.rb +0 -16
  75. data/lib/mongoid/commands/delete_all.rb +0 -23
  76. data/lib/mongoid/commands/deletion.rb +0 -18
  77. data/lib/mongoid/commands/destroy.rb +0 -19
  78. data/lib/mongoid/commands/destroy_all.rb +0 -23
  79. data/lib/mongoid/commands/save.rb +0 -27
  80. data/lib/mongoid/extensions/time/conversions.rb +0 -18
  81. data/mongoid.gemspec +0 -395
  82. data/perf/benchmark.rb +0 -77
  83. data/spec/integration/mongoid/associations_spec.rb +0 -340
  84. data/spec/integration/mongoid/attributes_spec.rb +0 -22
  85. data/spec/integration/mongoid/commands_spec.rb +0 -218
  86. data/spec/integration/mongoid/contexts/enumerable_spec.rb +0 -33
  87. data/spec/integration/mongoid/criteria_spec.rb +0 -272
  88. data/spec/integration/mongoid/document_spec.rb +0 -593
  89. data/spec/integration/mongoid/extensions_spec.rb +0 -26
  90. data/spec/integration/mongoid/finders_spec.rb +0 -119
  91. data/spec/integration/mongoid/inheritance_spec.rb +0 -137
  92. data/spec/integration/mongoid/named_scope_spec.rb +0 -46
  93. data/spec/models/address.rb +0 -39
  94. data/spec/models/animal.rb +0 -6
  95. data/spec/models/callbacks.rb +0 -18
  96. data/spec/models/comment.rb +0 -8
  97. data/spec/models/country_code.rb +0 -6
  98. data/spec/models/employer.rb +0 -5
  99. data/spec/models/game.rb +0 -7
  100. data/spec/models/inheritance.rb +0 -56
  101. data/spec/models/location.rb +0 -5
  102. data/spec/models/mixed_drink.rb +0 -4
  103. data/spec/models/name.rb +0 -13
  104. data/spec/models/namespacing.rb +0 -11
  105. data/spec/models/patient.rb +0 -4
  106. data/spec/models/person.rb +0 -99
  107. data/spec/models/pet.rb +0 -7
  108. data/spec/models/pet_owner.rb +0 -6
  109. data/spec/models/phone.rb +0 -7
  110. data/spec/models/post.rb +0 -15
  111. data/spec/models/translation.rb +0 -5
  112. data/spec/models/vet_visit.rb +0 -5
  113. data/spec/spec.opts +0 -3
  114. data/spec/spec_helper.rb +0 -31
  115. data/spec/unit/mongoid/associations/belongs_to_related_spec.rb +0 -145
  116. data/spec/unit/mongoid/associations/belongs_to_spec.rb +0 -193
  117. data/spec/unit/mongoid/associations/has_many_related_spec.rb +0 -420
  118. data/spec/unit/mongoid/associations/has_many_spec.rb +0 -519
  119. data/spec/unit/mongoid/associations/has_one_related_spec.rb +0 -179
  120. data/spec/unit/mongoid/associations/has_one_spec.rb +0 -282
  121. data/spec/unit/mongoid/associations/meta_data_spec.rb +0 -88
  122. data/spec/unit/mongoid/associations/options_spec.rb +0 -192
  123. data/spec/unit/mongoid/associations_spec.rb +0 -595
  124. data/spec/unit/mongoid/attributes_spec.rb +0 -507
  125. data/spec/unit/mongoid/callbacks_spec.rb +0 -55
  126. data/spec/unit/mongoid/collection_spec.rb +0 -187
  127. data/spec/unit/mongoid/collections/cyclic_iterator_spec.rb +0 -75
  128. data/spec/unit/mongoid/collections/master_spec.rb +0 -41
  129. data/spec/unit/mongoid/collections/mimic_spec.rb +0 -43
  130. data/spec/unit/mongoid/collections/slaves_spec.rb +0 -81
  131. data/spec/unit/mongoid/commands/create_spec.rb +0 -30
  132. data/spec/unit/mongoid/commands/delete_all_spec.rb +0 -58
  133. data/spec/unit/mongoid/commands/delete_spec.rb +0 -38
  134. data/spec/unit/mongoid/commands/destroy_all_spec.rb +0 -23
  135. data/spec/unit/mongoid/commands/destroy_spec.rb +0 -50
  136. data/spec/unit/mongoid/commands/save_spec.rb +0 -105
  137. data/spec/unit/mongoid/commands_spec.rb +0 -275
  138. data/spec/unit/mongoid/config_spec.rb +0 -172
  139. data/spec/unit/mongoid/contexts/enumerable_spec.rb +0 -421
  140. data/spec/unit/mongoid/contexts/mongo_spec.rb +0 -682
  141. data/spec/unit/mongoid/contexts_spec.rb +0 -25
  142. data/spec/unit/mongoid/criteria_spec.rb +0 -824
  143. data/spec/unit/mongoid/criterion/complex_spec.rb +0 -19
  144. data/spec/unit/mongoid/criterion/exclusion_spec.rb +0 -91
  145. data/spec/unit/mongoid/criterion/inclusion_spec.rb +0 -219
  146. data/spec/unit/mongoid/criterion/optional_spec.rb +0 -319
  147. data/spec/unit/mongoid/cursor_spec.rb +0 -74
  148. data/spec/unit/mongoid/deprecation_spec.rb +0 -24
  149. data/spec/unit/mongoid/document_spec.rb +0 -952
  150. data/spec/unit/mongoid/errors_spec.rb +0 -103
  151. data/spec/unit/mongoid/extensions/array/accessors_spec.rb +0 -50
  152. data/spec/unit/mongoid/extensions/array/assimilation_spec.rb +0 -24
  153. data/spec/unit/mongoid/extensions/array/conversions_spec.rb +0 -35
  154. data/spec/unit/mongoid/extensions/array/parentization_spec.rb +0 -20
  155. data/spec/unit/mongoid/extensions/big_decimal/conversions_spec.rb +0 -22
  156. data/spec/unit/mongoid/extensions/binary/conversions_spec.rb +0 -22
  157. data/spec/unit/mongoid/extensions/boolean/conversions_spec.rb +0 -49
  158. data/spec/unit/mongoid/extensions/date/conversions_spec.rb +0 -102
  159. data/spec/unit/mongoid/extensions/datetime/conversions_spec.rb +0 -70
  160. data/spec/unit/mongoid/extensions/float/conversions_spec.rb +0 -61
  161. data/spec/unit/mongoid/extensions/hash/accessors_spec.rb +0 -184
  162. data/spec/unit/mongoid/extensions/hash/assimilation_spec.rb +0 -46
  163. data/spec/unit/mongoid/extensions/hash/conversions_spec.rb +0 -21
  164. data/spec/unit/mongoid/extensions/hash/criteria_helpers_spec.rb +0 -17
  165. data/spec/unit/mongoid/extensions/hash/scoping_spec.rb +0 -14
  166. data/spec/unit/mongoid/extensions/integer/conversions_spec.rb +0 -61
  167. data/spec/unit/mongoid/extensions/nil/assimilation_spec.rb +0 -24
  168. data/spec/unit/mongoid/extensions/object/conversions_spec.rb +0 -57
  169. data/spec/unit/mongoid/extensions/proc/scoping_spec.rb +0 -34
  170. data/spec/unit/mongoid/extensions/string/conversions_spec.rb +0 -17
  171. data/spec/unit/mongoid/extensions/string/inflections_spec.rb +0 -208
  172. data/spec/unit/mongoid/extensions/symbol/inflections_spec.rb +0 -91
  173. data/spec/unit/mongoid/extensions/time/conversions_spec.rb +0 -70
  174. data/spec/unit/mongoid/extras_spec.rb +0 -102
  175. data/spec/unit/mongoid/factory_spec.rb +0 -31
  176. data/spec/unit/mongoid/field_spec.rb +0 -143
  177. data/spec/unit/mongoid/fields_spec.rb +0 -181
  178. data/spec/unit/mongoid/finders_spec.rb +0 -404
  179. data/spec/unit/mongoid/identity_spec.rb +0 -109
  180. data/spec/unit/mongoid/indexes_spec.rb +0 -93
  181. data/spec/unit/mongoid/javascript_spec.rb +0 -48
  182. data/spec/unit/mongoid/matchers/all_spec.rb +0 -27
  183. data/spec/unit/mongoid/matchers/default_spec.rb +0 -27
  184. data/spec/unit/mongoid/matchers/exists_spec.rb +0 -56
  185. data/spec/unit/mongoid/matchers/gt_spec.rb +0 -39
  186. data/spec/unit/mongoid/matchers/gte_spec.rb +0 -49
  187. data/spec/unit/mongoid/matchers/in_spec.rb +0 -27
  188. data/spec/unit/mongoid/matchers/lt_spec.rb +0 -39
  189. data/spec/unit/mongoid/matchers/lte_spec.rb +0 -49
  190. data/spec/unit/mongoid/matchers/ne_spec.rb +0 -27
  191. data/spec/unit/mongoid/matchers/nin_spec.rb +0 -27
  192. data/spec/unit/mongoid/matchers/size_spec.rb +0 -27
  193. data/spec/unit/mongoid/matchers_spec.rb +0 -329
  194. data/spec/unit/mongoid/memoization_spec.rb +0 -75
  195. data/spec/unit/mongoid/named_scope_spec.rb +0 -123
  196. data/spec/unit/mongoid/scope_spec.rb +0 -240
  197. data/spec/unit/mongoid/state_spec.rb +0 -83
  198. data/spec/unit/mongoid/timestamps_spec.rb +0 -25
  199. data/spec/unit/mongoid/versioning_spec.rb +0 -41
  200. data/spec/unit/mongoid_spec.rb +0 -46
@@ -46,6 +46,7 @@ module Mongoid #:nodoc:
46
46
  send("#{key}=", value)
47
47
  end
48
48
  end
49
+ setup_modifications
49
50
  end
50
51
 
51
52
  # Read a value from the +Document+ attributes. If the value does not exist
@@ -60,7 +61,7 @@ module Mongoid #:nodoc:
60
61
  # <tt>person.read_attribute(:title)</tt>
61
62
  def read_attribute(name)
62
63
  access = name.to_s
63
- fields[access].get(@attributes[access])
64
+ accessed(access, fields[access].get(@attributes[access]))
64
65
  end
65
66
 
66
67
  # Remove a value from the +Document+ attributes. If the value does not exist
@@ -74,7 +75,8 @@ module Mongoid #:nodoc:
74
75
  #
75
76
  # <tt>person.remove_attribute(:title)</tt>
76
77
  def remove_attribute(name)
77
- @attributes.delete(name.to_s)
78
+ access = name.to_s
79
+ modify(access, @attributes.delete(access), nil)
78
80
  end
79
81
 
80
82
  # Returns the object type. This corresponds to the name of the class that
@@ -106,8 +108,8 @@ module Mongoid #:nodoc:
106
108
  # there is any.
107
109
  def write_attribute(name, value)
108
110
  access = name.to_s
109
- @attributes[access] = fields[access].set(value)
110
- notify unless id.blank?
111
+ modify(access, @attributes[access], fields[access].set(value))
112
+ notify if !id.blank? && new_record?
111
113
  end
112
114
 
113
115
  # Writes the supplied attributes +Hash+ to the +Document+. This will only
@@ -126,13 +128,24 @@ module Mongoid #:nodoc:
126
128
  # there is any.
127
129
  def write_attributes(attrs = nil)
128
130
  process(attrs || {})
129
- identify if id.blank?
130
- notify
131
+ identified = !id.blank?
132
+ if new_record? && !identified
133
+ identify; notify
134
+ end
131
135
  end
132
136
  alias :attributes= write_attributes
133
137
 
134
138
  protected
135
- # Return true is dynamic field setting is enabled.
139
+ # apply default values to attributes - calling procs as required
140
+ def default_attributes
141
+ default_values = defaults
142
+ default_values.each_pair do |key, val|
143
+ default_values[key] = val.call if val.respond_to?(:call)
144
+ end
145
+ default_values || {}
146
+ end
147
+
148
+ # Return true if dynamic field setting is enabled.
136
149
  def set_allowed?(key)
137
150
  Mongoid.allow_dynamic_fields && !respond_to?("#{key}=")
138
151
  end
@@ -150,8 +163,10 @@ module Mongoid #:nodoc:
150
163
 
151
164
  # Return true if writing to the given field is allowed
152
165
  def write_allowed?(key)
153
- return true unless fields[key.to_s]
154
- fields[key.to_s].accessible?
166
+ name = key.to_s
167
+ existing = fields[name]
168
+ return true unless existing
169
+ existing.accessible?
155
170
  end
156
171
  end
157
172
 
@@ -163,8 +178,8 @@ module Mongoid #:nodoc:
163
178
  #
164
179
  # class Person
165
180
  # include Mongoid::Document
166
- # has_one :name
167
- # has_many :addresses
181
+ # embeds_one :name
182
+ # embeds_many :addresses
168
183
  #
169
184
  # accepts_nested_attributes_for :name, :addresses
170
185
  # end
@@ -176,7 +191,7 @@ module Mongoid #:nodoc:
176
191
  reject(attrs, options)
177
192
  association = send(name)
178
193
  if association
179
- update(association, true)
194
+ observe(association, true)
180
195
  association.nested_build(attrs)
181
196
  else
182
197
  send("build_#{name}", attrs)
@@ -1,13 +1,12 @@
1
1
  # encoding: utf-8
2
2
  require "mongoid/collections/operations"
3
3
  require "mongoid/collections/cyclic_iterator"
4
- require "mongoid/collections/mimic"
5
4
  require "mongoid/collections/master"
6
5
  require "mongoid/collections/slaves"
7
6
 
8
7
  module Mongoid #:nodoc
8
+ # The Mongoid wrapper to the Mongo Ruby driver's collection object.
9
9
  class Collection
10
- include Collections::Mimic
11
10
  attr_reader :counter, :name
12
11
 
13
12
  # All write operations should delegate to the master connection. These
@@ -16,7 +15,9 @@ module Mongoid #:nodoc
16
15
  # Example:
17
16
  #
18
17
  # <tt>collection.save({ :name => "Al" })</tt>
19
- proxy(:master, Collections::Operations::PROXIED)
18
+ Collections::Operations::PROXIED.each do |name|
19
+ define_method(name) { |*args| master.send(name, *args) }
20
+ end
20
21
 
21
22
  # Determines where to send the next read query. If the slaves are not
22
23
  # defined then send to master. If the read counter is under the configured
@@ -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
@@ -2,7 +2,6 @@
2
2
  module Mongoid #:nodoc:
3
3
  module Collections #:nodoc:
4
4
  class Master
5
- include Mimic
6
5
 
7
6
  attr_reader :collection
8
7
 
@@ -12,7 +11,9 @@ module Mongoid #:nodoc:
12
11
  # Example:
13
12
  #
14
13
  # <tt>collection.save({ :name => "Al" })</tt>
15
- proxy(:collection, Operations::ALL)
14
+ Operations::ALL.each do |name|
15
+ define_method(name) { |*args| collection.send(name, *args) }
16
+ end
16
17
 
17
18
  # Create the new database writer. Will create a collection from the
18
19
  # master database.
@@ -2,7 +2,6 @@
2
2
  module Mongoid #:nodoc:
3
3
  module Collections #:nodoc:
4
4
  class Slaves
5
- include Mimic
6
5
 
7
6
  attr_reader :iterator
8
7
 
@@ -12,7 +11,9 @@ module Mongoid #:nodoc:
12
11
  # Example:
13
12
  #
14
13
  # <tt>collection.save({ :name => "Al" })</tt>
15
- proxy(:collection, Operations::READ)
14
+ Operations::READ.each do |name|
15
+ define_method(name) { |*args| collection.send(name, *args) }
16
+ end
16
17
 
17
18
  # Is the collection of slaves empty or not?
18
19
  #
@@ -1,25 +1,27 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc
3
3
  module Components #:nodoc
4
- def self.included(base)
5
- base.class_eval 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::Commands
12
- include Mongoid::Extras
13
- include Mongoid::Fields
14
- include Mongoid::Indexes
15
- include Mongoid::Matchers
16
- include Mongoid::Memoization
17
- include Mongoid::State
18
- include Observable
19
- include Validatable
20
- extend Mongoid::Finders
21
- extend Mongoid::NamedScope
22
- end
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
23
25
  end
24
26
  end
25
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
@@ -1,4 +1,6 @@
1
1
  # encoding: utf-8
2
+ require "uri"
3
+
2
4
  module Mongoid #:nodoc
3
5
  class Config #:nodoc
4
6
  include Singleton
@@ -8,23 +10,44 @@ module Mongoid #:nodoc
8
10
  :reconnect_time,
9
11
  :parameterize_keys,
10
12
  :persist_in_safe_mode,
11
- :persist_types,
12
13
  :raise_not_found_error,
13
14
  :use_object_ids
14
15
 
15
16
  # Defaults the configuration options to true.
16
17
  def initialize
17
- @allow_dynamic_fields = true
18
- @parameterize_keys = true
19
- @persist_in_safe_mode = true
20
- @persist_types = true
21
- @raise_not_found_error = true
22
- @reconnect_time = 3
23
- @use_object_ids = false
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
24
34
  end
25
35
 
26
- # Sets the Mongo::DB master database to be used. If the object trying to me
27
- # set is not a valid +Mongo::DB+, then an error will be raise.
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.
28
51
  #
29
52
  # Example:
30
53
  #
@@ -34,7 +57,7 @@ module Mongoid #:nodoc
34
57
  #
35
58
  # The Master DB instance.
36
59
  def master=(db)
37
- raise Errors::InvalidDatabase.new(db) unless db.kind_of?(Mongo::DB)
60
+ check_database!(db)
38
61
  @master = db
39
62
  end
40
63
 
@@ -66,7 +89,9 @@ module Mongoid #:nodoc
66
89
  #
67
90
  # The slaves DB instances.
68
91
  def slaves=(dbs)
69
- dbs.each { |db| raise Errors::InvalidDatabase.new(db) unless db.kind_of?(Mongo::DB) }
92
+ dbs.each do |db|
93
+ check_database!(db)
94
+ end
70
95
  @slaves = dbs
71
96
  end
72
97
 
@@ -82,5 +107,85 @@ module Mongoid #:nodoc
82
107
  def slaves
83
108
  @slaves
84
109
  end
110
+
111
+ # Confiure mongoid from a hash that was usually parsed out of yml.
112
+ #
113
+ # Example:
114
+ #
115
+ # <tt>Mongoid::Config.instance.from_hash({})</tt>
116
+ def from_hash(settings)
117
+ _master(settings)
118
+ _slaves(settings)
119
+ settings.except("database").each_pair do |name, value|
120
+ send("#{name}=", value) if respond_to?(name)
121
+ end
122
+ end
123
+
124
+ # Reset the configuration options to the defaults.
125
+ #
126
+ # Example:
127
+ #
128
+ # <tt>config.reset</tt>
129
+ def reset
130
+ @allow_dynamic_fields = true
131
+ @parameterize_keys = true
132
+ @persist_in_safe_mode = true
133
+ @raise_not_found_error = true
134
+ @reconnect_time = 3
135
+ @use_object_ids = false
136
+ @time_zone = nil
137
+ end
138
+
139
+ protected
140
+
141
+ # Check if the database is valid and the correct version.
142
+ #
143
+ # Example:
144
+ #
145
+ # <tt>config.check_database!</tt>
146
+ def check_database!(database)
147
+ raise Errors::InvalidDatabase.new(database) unless database.kind_of?(Mongo::DB)
148
+ version = database.connection.server_version
149
+ raise Errors::UnsupportedVersion.new(version) if version < Mongoid::MONGODB_VERSION
150
+ end
151
+
152
+ # Get a Rails logger or stdout logger.
153
+ #
154
+ # Example:
155
+ #
156
+ # <tt>config.logger</tt>
157
+ def logger
158
+ defined?(Rails) ? Rails.logger : Logger.new($stdout)
159
+ end
160
+
161
+ # Get a master database from settings.
162
+ #
163
+ # Example:
164
+ #
165
+ # <tt>config._master({}, "test")</tt>
166
+ def _master(settings)
167
+ name = settings["database"]
168
+ host = settings.delete("host") || "localhost"
169
+ port = settings.delete("port") || 27017
170
+ self.master = Mongo::Connection.new(host, port, :logger => logger).db(name)
171
+ end
172
+
173
+ # Get a bunch-o-slaves from settings and names.
174
+ #
175
+ # Example:
176
+ #
177
+ # <tt>config._slaves({}, "test")</tt>
178
+ def _slaves(settings)
179
+ name = settings["database"]
180
+ self.slaves = []
181
+ slaves = settings.delete("slaves")
182
+ slaves.to_a.each do |slave|
183
+ self.slaves << Mongo::Connection.new(
184
+ slave["host"],
185
+ slave["port"],
186
+ :slave_ok => true
187
+ ).db(name)
188
+ end
189
+ end
85
190
  end
86
191
  end
@@ -15,7 +15,7 @@ module Mongoid
15
15
  #
16
16
  # <tt>Contexts.context_for(criteria)</tt>
17
17
  def self.context_for(criteria)
18
- if criteria.klass.embedded
18
+ if criteria.klass.embedded?
19
19
  return Contexts::Enumerable.new(criteria)
20
20
  end
21
21
  Contexts::Mongo.new(criteria)