mongo_odm 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -51,8 +51,9 @@ A piece of code is better than a hundred of words:
51
51
  # "color" : null,
52
52
  # "radius" : 1 }
53
53
 
54
- all_shapes = Shape.find.to_a
55
-
54
+ all_shapes = Shape.find # Returns a criteria object. It will execute the query and instance the objects once you iterate over it
55
+
56
+ all_shapes.to_a
56
57
  # Returns all the shapes; notice they are of different classes:
57
58
  # [ #<Shape x: 0.0, y: 5.0, color: nil, name: "Point", _id: {"$oid"=>"4be97178715dd2c4be000006"}>,
58
59
  # #<Circle x: 1.0, y: 1.0, color: nil, radius: 1.0, _id: {"$oid"=>"4be97293715dd2c4be000008"}> ]
@@ -65,6 +66,28 @@ to have the attribute "_class" set to the name of the class you want to use as t
65
66
  # Returns:
66
67
  # #<Circle x: 12.0, y: 5.0, color: nil, radius: 1.0>
67
68
 
69
+ And because any query method returns a MongoODM::Criteria object, you can concatenate them to nest several conditions (like if they were ActiveRecord scopes):
70
+
71
+ Shape.find(:radius => 1).find({}, {:sort => [:color, :asc]}) # Returns a criteria object. Once you iterate over it, it will run a query with both the :radius selector and :sort order.
72
+
73
+ You can also define your own class methods that returns criteria objects, and concatenate them to obtain a single criteria with all the conditions merged in the calls order:
74
+
75
+ class Shape
76
+ def self.with_radius(n)
77
+ find(:radius => n)
78
+ end
79
+
80
+ def self.ordered_by_color
81
+ find({}, {:sort => [:color, :asc]})
82
+ end
83
+ end
84
+
85
+ Shape.with_radius(1).ordered_by_color # Returns the same criteria than the previous example
86
+
87
+ Take a look at the Mongo Ruby driver documentation for the 'find' method to see the available options:
88
+
89
+ http://api.mongodb.org/ruby/1.0.8/Mongo/Collection.html#find-instance_method
90
+
68
91
  = Associations
69
92
 
70
93
  To embed just one copy of another class, just define the field type of that class. The class just need to respond to the "type_cast" class method and the "to_mongo" instance method. Example:
@@ -235,21 +258,68 @@ To reference the associated objects instead of embed them, for now you need to d
235
258
 
236
259
  For now, the available callbacks are: after_initialize, before_save, after_save
237
260
 
261
+ Example:
262
+
263
+ class User
264
+ include MongoODM::Document
265
+
266
+ field :encrypted_password
267
+ attr_accessor :password
268
+
269
+ before_save :encrypt_password
270
+
271
+ def encrypt_password
272
+ return if self.password.blank?
273
+ self.encrypted_password = encrypt(password)
274
+ end
275
+ protected :encrypt_password
276
+ end
277
+
238
278
  = Validations
239
279
 
240
280
  All the validation methods defined in ActiveModel::Validations are included
241
281
 
282
+ Example:
283
+
284
+ class User
285
+ field :email
286
+
287
+ validates_presence_of :email
288
+ validates_uniqueness_of :email, :case_sensitive => false
289
+ validates_format_of :email, :with => /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
290
+ end
291
+
242
292
  = Dirty
243
293
 
244
294
  All the dirty object methods defined in ActiveModel::Dirty are included
245
295
 
296
+ Example:
297
+
298
+ class User
299
+ field :email
300
+ end
301
+
302
+ user = User.new
303
+ user.email = "hello@h1labs.com"
304
+ user.email_changed? # Returns true
305
+ user.email_change # Returns [nil, "hello@h1labs.com"]
306
+ user.changes # Returns {"email" => [nil, "hello@h1labs.com"]}
307
+
246
308
  = TODO
247
309
 
310
+ * Support join of different concatenated find calls to a single criteria object
248
311
  * Add helpers to define attributes as referenced objects
312
+ * Allow to specify different database connections with each document definition
249
313
  * Increase rspec coverage
250
314
  * Document, document, document!
251
315
  * Create useful modules to make common operations easier (versioning, localization, etc)
252
316
 
317
+ = More
318
+
319
+ For now, take a look at the Mongo Ruby driver syntax:
320
+
321
+ http://api.mongodb.org/ruby/1.0.8/index.html
322
+
253
323
  = Copyright
254
324
 
255
325
  Copyright © 2010 Carlos Paramio. See LICENSE for details.
@@ -2,18 +2,33 @@
2
2
  module MongoODM
3
3
 
4
4
  class Criteria
5
- def initialize(collection, selector = {}, opts = {})
6
- @collection, @selector, @opts = collection, selector, opts
5
+ def initialize(klass, selector = {}, opts = {})
6
+ @klass, @selector, @opts = klass, selector, opts
7
7
  end
8
8
 
9
9
  def find(selector = {}, opts = {})
10
- @selector.merge!(selector)
11
- @opts.merge!(opts)
12
- self
10
+ _merge_criteria(selector, opts)
13
11
  end
14
12
 
15
13
  def method_missing(method_name, *args)
16
- @collection.find(@selector, @opts).send(method_name, *args)
14
+ if @klass.methods.include?(method_name)
15
+ result = @klass.send(method_name, *args)
16
+ if result.is_a?(Criteria)
17
+ selector = result.instance_variable_get("@selector")
18
+ opts = result.instance_variable_get("@opts")
19
+ _merge_criteria(selector, opts)
20
+ else
21
+ result
22
+ end
23
+ else
24
+ @klass.collection.find(@selector, @opts).send(method_name, *args)
25
+ end
26
+ end
27
+
28
+ def _merge_criteria(selector, opts)
29
+ @selector.merge!(selector)
30
+ @opts.merge!(opts)
31
+ self
17
32
  end
18
33
  end
19
34
 
@@ -76,7 +76,7 @@ module MongoODM
76
76
  end
77
77
 
78
78
  def find(*args)
79
- MongoODM::Criteria.new(collection, *args)
79
+ MongoODM::Criteria.new(self, *args)
80
80
  end
81
81
 
82
82
  def destroy_all(*args)
@@ -2,6 +2,6 @@
2
2
  module MongoODM
3
3
  VERSION_MAJOR = "0"
4
4
  VERSION_MINOR = "2"
5
- VERSION_BUILD = "2"
5
+ VERSION_BUILD = "3"
6
6
  VERSION = "#{VERSION_MAJOR}.#{VERSION_MINOR}.#{VERSION_BUILD}"
7
7
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 2
9
- version: 0.2.2
8
+ - 3
9
+ version: 0.2.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Carlos Paramio
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-08-29 00:00:00 +02:00
17
+ date: 2010-08-30 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency