chhean-mongoid 2.0.1.beta1

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 (117) hide show
  1. data/MIT_LICENSE +20 -0
  2. data/README.rdoc +49 -0
  3. data/lib/mongoid.rb +139 -0
  4. data/lib/mongoid/associations.rb +327 -0
  5. data/lib/mongoid/associations/embedded_in.rb +72 -0
  6. data/lib/mongoid/associations/embeds_many.rb +262 -0
  7. data/lib/mongoid/associations/embeds_one.rb +95 -0
  8. data/lib/mongoid/associations/foreign_key.rb +35 -0
  9. data/lib/mongoid/associations/meta_data.rb +29 -0
  10. data/lib/mongoid/associations/options.rb +73 -0
  11. data/lib/mongoid/associations/proxy.rb +33 -0
  12. data/lib/mongoid/associations/referenced_in.rb +71 -0
  13. data/lib/mongoid/associations/references_many.rb +243 -0
  14. data/lib/mongoid/associations/references_many_as_array.rb +78 -0
  15. data/lib/mongoid/associations/references_one.rb +116 -0
  16. data/lib/mongoid/attributes.rb +226 -0
  17. data/lib/mongoid/callbacks.rb +17 -0
  18. data/lib/mongoid/collection.rb +120 -0
  19. data/lib/mongoid/collections.rb +41 -0
  20. data/lib/mongoid/collections/cyclic_iterator.rb +34 -0
  21. data/lib/mongoid/collections/master.rb +29 -0
  22. data/lib/mongoid/collections/operations.rb +41 -0
  23. data/lib/mongoid/collections/slaves.rb +45 -0
  24. data/lib/mongoid/components.rb +32 -0
  25. data/lib/mongoid/config.rb +237 -0
  26. data/lib/mongoid/contexts.rb +24 -0
  27. data/lib/mongoid/contexts/enumerable.rb +151 -0
  28. data/lib/mongoid/contexts/ids.rb +25 -0
  29. data/lib/mongoid/contexts/mongo.rb +285 -0
  30. data/lib/mongoid/contexts/paging.rb +50 -0
  31. data/lib/mongoid/criteria.rb +230 -0
  32. data/lib/mongoid/criterion/complex.rb +21 -0
  33. data/lib/mongoid/criterion/exclusion.rb +65 -0
  34. data/lib/mongoid/criterion/inclusion.rb +110 -0
  35. data/lib/mongoid/criterion/optional.rb +136 -0
  36. data/lib/mongoid/cursor.rb +82 -0
  37. data/lib/mongoid/deprecation.rb +22 -0
  38. data/lib/mongoid/dirty.rb +254 -0
  39. data/lib/mongoid/document.rb +264 -0
  40. data/lib/mongoid/errors.rb +124 -0
  41. data/lib/mongoid/extensions.rb +106 -0
  42. data/lib/mongoid/extensions/array/accessors.rb +17 -0
  43. data/lib/mongoid/extensions/array/aliasing.rb +4 -0
  44. data/lib/mongoid/extensions/array/assimilation.rb +26 -0
  45. data/lib/mongoid/extensions/array/conversions.rb +27 -0
  46. data/lib/mongoid/extensions/array/parentization.rb +13 -0
  47. data/lib/mongoid/extensions/big_decimal/conversions.rb +19 -0
  48. data/lib/mongoid/extensions/binary/conversions.rb +17 -0
  49. data/lib/mongoid/extensions/boolean/conversions.rb +22 -0
  50. data/lib/mongoid/extensions/date/conversions.rb +24 -0
  51. data/lib/mongoid/extensions/datetime/conversions.rb +12 -0
  52. data/lib/mongoid/extensions/float/conversions.rb +20 -0
  53. data/lib/mongoid/extensions/hash/accessors.rb +38 -0
  54. data/lib/mongoid/extensions/hash/assimilation.rb +39 -0
  55. data/lib/mongoid/extensions/hash/conversions.rb +45 -0
  56. data/lib/mongoid/extensions/hash/criteria_helpers.rb +20 -0
  57. data/lib/mongoid/extensions/hash/scoping.rb +12 -0
  58. data/lib/mongoid/extensions/integer/conversions.rb +20 -0
  59. data/lib/mongoid/extensions/nil/assimilation.rb +17 -0
  60. data/lib/mongoid/extensions/object/conversions.rb +27 -0
  61. data/lib/mongoid/extensions/objectid/conversions.rb +15 -0
  62. data/lib/mongoid/extensions/proc/scoping.rb +12 -0
  63. data/lib/mongoid/extensions/set/conversions.rb +20 -0
  64. data/lib/mongoid/extensions/string/conversions.rb +15 -0
  65. data/lib/mongoid/extensions/string/inflections.rb +97 -0
  66. data/lib/mongoid/extensions/symbol/inflections.rb +39 -0
  67. data/lib/mongoid/extensions/time_conversions.rb +35 -0
  68. data/lib/mongoid/extras.rb +61 -0
  69. data/lib/mongoid/factory.rb +20 -0
  70. data/lib/mongoid/field.rb +80 -0
  71. data/lib/mongoid/fields.rb +61 -0
  72. data/lib/mongoid/finders.rb +144 -0
  73. data/lib/mongoid/identity.rb +39 -0
  74. data/lib/mongoid/indexes.rb +27 -0
  75. data/lib/mongoid/javascript.rb +21 -0
  76. data/lib/mongoid/javascript/functions.yml +37 -0
  77. data/lib/mongoid/matchers.rb +35 -0
  78. data/lib/mongoid/matchers/all.rb +11 -0
  79. data/lib/mongoid/matchers/default.rb +26 -0
  80. data/lib/mongoid/matchers/exists.rb +13 -0
  81. data/lib/mongoid/matchers/gt.rb +11 -0
  82. data/lib/mongoid/matchers/gte.rb +11 -0
  83. data/lib/mongoid/matchers/in.rb +11 -0
  84. data/lib/mongoid/matchers/lt.rb +11 -0
  85. data/lib/mongoid/matchers/lte.rb +11 -0
  86. data/lib/mongoid/matchers/ne.rb +11 -0
  87. data/lib/mongoid/matchers/nin.rb +11 -0
  88. data/lib/mongoid/matchers/size.rb +11 -0
  89. data/lib/mongoid/memoization.rb +33 -0
  90. data/lib/mongoid/named_scope.rb +37 -0
  91. data/lib/mongoid/observable.rb +30 -0
  92. data/lib/mongoid/paths.rb +62 -0
  93. data/lib/mongoid/persistence.rb +218 -0
  94. data/lib/mongoid/persistence/command.rb +39 -0
  95. data/lib/mongoid/persistence/insert.rb +47 -0
  96. data/lib/mongoid/persistence/insert_embedded.rb +38 -0
  97. data/lib/mongoid/persistence/remove.rb +39 -0
  98. data/lib/mongoid/persistence/remove_all.rb +37 -0
  99. data/lib/mongoid/persistence/remove_embedded.rb +50 -0
  100. data/lib/mongoid/persistence/update.rb +63 -0
  101. data/lib/mongoid/railtie.rb +54 -0
  102. data/lib/mongoid/railties/database.rake +37 -0
  103. data/lib/mongoid/scope.rb +75 -0
  104. data/lib/mongoid/state.rb +32 -0
  105. data/lib/mongoid/timestamps.rb +27 -0
  106. data/lib/mongoid/validations.rb +51 -0
  107. data/lib/mongoid/validations/associated.rb +32 -0
  108. data/lib/mongoid/validations/locale/en.yml +4 -0
  109. data/lib/mongoid/validations/uniqueness.rb +50 -0
  110. data/lib/mongoid/version.rb +4 -0
  111. data/lib/mongoid/versioning.rb +27 -0
  112. data/lib/rails/generators/mongoid/config/config_generator.rb +41 -0
  113. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +24 -0
  114. data/lib/rails/generators/mongoid/model/model_generator.rb +24 -0
  115. data/lib/rails/generators/mongoid/model/templates/model.rb +15 -0
  116. data/lib/rails/generators/mongoid_generator.rb +61 -0
  117. metadata +284 -0
@@ -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,32 @@
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 ActiveModel::Conversion
9
+ include ActiveModel::Naming
10
+ include ActiveModel::Serialization
11
+ include ActiveModel::Serializers::JSON
12
+ include Mongoid::Associations
13
+ include Mongoid::Attributes
14
+ include Mongoid::Callbacks
15
+ include Mongoid::Collections
16
+ include Mongoid::Dirty
17
+ include Mongoid::Extras
18
+ include Mongoid::Fields
19
+ include Mongoid::Indexes
20
+ include Mongoid::Matchers
21
+ include Mongoid::Memoization
22
+ include Mongoid::Observable
23
+ include Mongoid::Paths
24
+ include Mongoid::Persistence
25
+ include Mongoid::State
26
+ include Mongoid::Validations
27
+ extend ActiveModel::Translation
28
+ extend Mongoid::Finders
29
+ extend Mongoid::NamedScope
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,237 @@
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
+ :skip_version_check
16
+
17
+ # Defaults the configuration options to true.
18
+ def initialize
19
+ reset
20
+ end
21
+
22
+ # Sets whether the times returned from the database are in UTC or local time.
23
+ # If you omit this setting, then times will be returned in
24
+ # the local time zone.
25
+ #
26
+ # Example:
27
+ #
28
+ # <tt>Config.use_utc = true</tt>
29
+ #
30
+ # Returns:
31
+ #
32
+ # A boolean
33
+ def use_utc=(value)
34
+ @use_utc = value || false
35
+ end
36
+
37
+ # Returns whether times are return from the database in UTC. If
38
+ # this setting is false, then times will be returned in the local time zone.
39
+ #
40
+ # Example:
41
+ #
42
+ # <tt>Config.use_utc</tt>
43
+ #
44
+ # Returns:
45
+ #
46
+ # A boolean
47
+ attr_reader :use_utc
48
+ alias_method :use_utc?, :use_utc
49
+
50
+ # Sets the Mongo::DB master database to be used. If the object trying to be
51
+ # set is not a valid +Mongo::DB+, then an error will be raised.
52
+ #
53
+ # Example:
54
+ #
55
+ # <tt>Config.master = Mongo::Connection.db("test")</tt>
56
+ #
57
+ # Returns:
58
+ #
59
+ # The Master DB instance.
60
+ def master=(db)
61
+ check_database!(db)
62
+ @master = db
63
+ end
64
+
65
+ # Returns the master database, or if none has been set it will raise an
66
+ # error.
67
+ #
68
+ # Example:
69
+ #
70
+ # <tt>Config.master</tt>
71
+ #
72
+ # Returns:
73
+ #
74
+ # The master +Mongo::DB+
75
+ def master
76
+ @master || (raise Errors::InvalidDatabase.new(nil))
77
+ end
78
+
79
+ alias :database :master
80
+ alias :database= :master=
81
+
82
+ # Sets the Mongo::DB slave databases to be used. If the objects trying to me
83
+ # set are not valid +Mongo::DBs+, then an error will be raise.
84
+ #
85
+ # Example:
86
+ #
87
+ # <tt>Config.slaves = [ Mongo::Connection.db("test") ]</tt>
88
+ #
89
+ # Returns:
90
+ #
91
+ # The slaves DB instances.
92
+ def slaves=(dbs)
93
+ return unless dbs
94
+ dbs.each do |db|
95
+ check_database!(db)
96
+ end
97
+ @slaves = dbs
98
+ end
99
+
100
+ # Returns the slave databases, or if none has been set nil
101
+ #
102
+ # Example:
103
+ #
104
+ # <tt>Config.slaves</tt>
105
+ #
106
+ # Returns:
107
+ #
108
+ # The slave +Mongo::DBs+
109
+ def slaves
110
+ @slaves
111
+ end
112
+
113
+ # Return field names that could cause destructive things to happen if
114
+ # defined in a Mongoid::Document
115
+ #
116
+ # Example:
117
+ #
118
+ # <tt>Config.destructive_fields</tt>
119
+ #
120
+ # Returns:
121
+ #
122
+ # An array of bad field names.
123
+ def destructive_fields
124
+ @destructive_fields ||= lambda {
125
+ klass = Class.new do
126
+ include Mongoid::Document
127
+ end
128
+ klass.instance_methods(true).collect { |method| method.to_s }
129
+ }.call
130
+ end
131
+
132
+ # Configure mongoid from a hash that was usually parsed out of yml.
133
+ #
134
+ # Example:
135
+ #
136
+ # <tt>Mongoid::Config.instance.from_hash({})</tt>
137
+ def from_hash(settings)
138
+ _master(settings)
139
+ _slaves(settings)
140
+ settings.except("database").each_pair do |name, value|
141
+ send("#{name}=", value) if respond_to?(name)
142
+ end
143
+ end
144
+
145
+ # Reset the configuration options to the defaults.
146
+ #
147
+ # Example:
148
+ #
149
+ # <tt>config.reset</tt>
150
+ def reset
151
+ @allow_dynamic_fields = true
152
+ @parameterize_keys = true
153
+ @persist_in_safe_mode = true
154
+ @raise_not_found_error = true
155
+ @reconnect_time = 3
156
+ @use_object_ids = false
157
+ @skip_version_check = false
158
+ @time_zone = nil
159
+ end
160
+
161
+ protected
162
+
163
+ # Check if the database is valid and the correct version.
164
+ #
165
+ # Example:
166
+ #
167
+ # <tt>config.check_database!</tt>
168
+ def check_database!(database)
169
+ raise Errors::InvalidDatabase.new(database) unless database.kind_of?(Mongo::DB)
170
+ unless Mongoid.skip_version_check
171
+ version = database.connection.server_version
172
+ raise Errors::UnsupportedVersion.new(version) if version < Mongoid::MONGODB_VERSION
173
+ end
174
+ end
175
+
176
+ # Get a Rails logger or stdout logger.
177
+ #
178
+ # Example:
179
+ #
180
+ # <tt>config.logger</tt>
181
+ def logger
182
+ defined?(Rails) ? Rails.logger : Logger.new($stdout)
183
+ end
184
+
185
+ # Get a master database from settings.
186
+ #
187
+ # Example:
188
+ #
189
+ # <tt>config._master({}, "test")</tt>
190
+ def _master(settings)
191
+ mongo_uri = settings["uri"].present? ? URI.parse(settings["uri"]) : OpenStruct.new
192
+
193
+ name = settings["database"] || mongo_uri.path.to_s.sub("/", "")
194
+ host = settings["host"] || mongo_uri.host || "localhost"
195
+ port = settings["port"] || mongo_uri.port || 27017
196
+ pool_size = settings["pool_size"] || 1
197
+ username = settings["username"] || mongo_uri.user
198
+ password = settings["password"] || mongo_uri.password
199
+
200
+ connection = Mongo::Connection.new(host, port, :logger => logger, :pool_size => pool_size)
201
+ if username || password
202
+ connection.add_auth(name, username, password)
203
+ connection.apply_saved_authentication
204
+ end
205
+ self.master = connection.db(name)
206
+ end
207
+
208
+ # Get a bunch-o-slaves from settings and names.
209
+ #
210
+ # Example:
211
+ #
212
+ # <tt>config._slaves({}, "test")</tt>
213
+ def _slaves(settings)
214
+ mongo_uri = settings["uri"].present? ? URI.parse(settings["uri"]) : OpenStruct.new
215
+ name = settings["database"] || mongo_uri.path.to_s.sub("/", "")
216
+ self.slaves = []
217
+ slaves = settings["slaves"]
218
+ slaves.to_a.each do |slave|
219
+ slave_uri = slave["uri"].present? ? URI.parse(slave["uri"]) : OpenStruct.new
220
+ slave_username = slave["username"] || slave_uri.user
221
+ slave_password = slave["password"] || slave_uri.password
222
+
223
+ slave_connection = Mongo::Connection.new(
224
+ slave["host"] || slave_uri.host || "localhost",
225
+ slave["port"] || slave_uri.port,
226
+ :slave_ok => true
227
+ )
228
+
229
+ if slave_username || slave_password
230
+ slave_connection.add_auth(name, slave_username, slave_password)
231
+ slave_connection.apply_saved_authentication
232
+ end
233
+ self.slaves << slave_connection.db(name)
234
+ end
235
+ end
236
+ end
237
+ end