mongo_odm 0.2.6 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
data/CONTRIBUTORS CHANGED
@@ -1,4 +1,5 @@
1
1
  Steve Sloan
2
2
 
3
- * Config class, railtie to load config from YAML file
4
- * Timestamps module
3
+ * Config class, railtie to load config from YAML file
4
+ * Timestamps module
5
+ * BSON::DBRef ideas
data/Gemfile.lock CHANGED
@@ -1,20 +1,14 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- ZenTest (4.4.2)
5
4
  activemodel (3.0.4)
6
5
  activesupport (= 3.0.4)
7
6
  builder (~> 2.1.2)
8
7
  i18n (~> 0.4)
9
8
  activesupport (3.0.4)
10
- archive-tar-minitar (0.5.2)
11
- autotest (4.4.6)
12
- ZenTest (>= 4.4.1)
13
9
  bson (1.2.2)
14
10
  bson_ext (1.2.2)
15
11
  builder (2.1.2)
16
- columnize (0.3.2)
17
- database_cleaner (0.6.3)
18
12
  diff-lcs (1.1.2)
19
13
  git (1.2.5)
20
14
  i18n (0.5.0)
@@ -22,8 +16,6 @@ GEM
22
16
  bundler (~> 1.0.0)
23
17
  git (>= 1.2.5)
24
18
  rake
25
- linecache19 (0.5.11)
26
- ruby_core_source (>= 0.1.4)
27
19
  mongo (1.2.2)
28
20
  bson (>= 1.2.2)
29
21
  rake (0.8.7)
@@ -36,16 +28,6 @@ GEM
36
28
  rspec-expectations (2.4.0)
37
29
  diff-lcs (~> 1.1.2)
38
30
  rspec-mocks (2.4.0)
39
- ruby-debug-base19 (0.11.24)
40
- columnize (>= 0.3.1)
41
- linecache19 (>= 0.5.11)
42
- ruby_core_source (>= 0.1.4)
43
- ruby-debug19 (0.11.6)
44
- columnize (>= 0.3.1)
45
- linecache19 (>= 0.5.11)
46
- ruby-debug-base19 (>= 0.11.19)
47
- ruby_core_source (0.1.4)
48
- archive-tar-minitar (>= 0.5.2)
49
31
  tzinfo (0.3.24)
50
32
  watchr (0.7)
51
33
  yard (0.6.4)
@@ -56,14 +38,11 @@ PLATFORMS
56
38
  DEPENDENCIES
57
39
  activemodel (~> 3.0.4)
58
40
  activesupport (~> 3.0.4)
59
- autotest
60
41
  bson_ext (~> 1.2.2)
61
- database_cleaner
62
42
  jeweler (~> 1.5.2)
63
43
  mongo (~> 1.2.2)
64
44
  rcov (~> 0.9.9)
65
45
  rspec (= 2.4.0)
66
- ruby-debug19
67
46
  tzinfo (~> 0.3.24)
68
47
  watchr (~> 0.7)
69
48
  yard (~> 0.6.4)
data/README.rdoc CHANGED
@@ -105,6 +105,33 @@ Alternatively, you can pass a MongoODM::Collection instance to set_collection, t
105
105
  set_collection MongoODM::Collection.new(MongoODM.connection.db('another_database'), 'my_shapes')
106
106
  end
107
107
 
108
+ = References
109
+
110
+ You can use BSON::DBRef as the type of a field. This acts as a pointer to any other document in your database, at any collection. If you assign a MongoODM::Document instance to a BSON::DBRef field, it will be converted to a reference automatically. To instantiate any reference object, just call "dereference" on it. To convert any MongoODM::Document object to a reference, just call "to_dbref" on it.
111
+
112
+ You can even dereference a full array or hash that contains BSON::DBRef instances! It will dereference them at any level.
113
+
114
+ class Node
115
+ include MongoODM::Document
116
+ field :name
117
+ field :parent, BSON::DBRef
118
+ field :children, Array
119
+ end
120
+
121
+ root_node = Node.new(:name => 'root')
122
+ root_node.save
123
+ children1 = Node.new(:name => 'children1', :parent => root_node)
124
+ children1.save
125
+ root_node.children = [children1.to_dbref]
126
+ root_node.save
127
+
128
+ children1.parent # Returns BSON::DBRef(namespace:"nodes", id: "4d60e8c83f5f19cf08000001")
129
+ root_node.children # Returns [BSON::DBRef(namespace:"nodes", id: "4d60e8c83f5f19cf08000002")]
130
+
131
+ children1.parent.dereference # Returns #<Node _id: BSON::ObjectId('4d60e8c83f5f19cf08000001'), children: [BSON::DBRef(namespace:"nodes", id: "4d60e8c83f5f19cf08000002")], name: "root", parent: nil>
132
+
133
+ root_node.children.dereference # Returns [#<Node _id: BSON::ObjectId('4d60e8c83f5f19cf08000002'), children: nil, name: "children1", parent: BSON::DBRef(namespace:"nodes", id: "4d60e8c83f5f19cf08000001")>]
134
+
108
135
  = Associations
109
136
 
110
137
  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:
@@ -234,7 +261,65 @@ If you want to save a collection of objects, just define the field as an Array.
234
261
  # #<Draw objects: [#<Circle x: 1.0, y: 1.0, color: nil, radius: 10.0>, #<Line x: 0.0, y: 0.0, color: nil, dx: 10.0, dy: 5.0>, #<Circle x: 2.0, y: 2.0, color: nil, radius: 20.0>], _id: {"$oid"=>"4bf0775d715dd2725a000001"}>
235
262
 
236
263
 
237
- To reference the associated objects instead of embed them, for now you need to define your own methods. Example:
264
+ To reference the associated objects instead of embed them, you can use BSON::DBRef (to reference one), Array (to reference several), and others:
265
+
266
+ class Flag
267
+ include MongoODM::Document
268
+ field :colors_refs, Array, :default => []
269
+
270
+ def add_color(color)
271
+ colors_refs << color.to_dbref
272
+ end
273
+
274
+ def colors
275
+ colors_refs.dereference
276
+ end
277
+ end
278
+
279
+ class Color
280
+ include MongoODM::Document
281
+ field :name
282
+ end
283
+
284
+ color_red = Color.new(:name => "red")
285
+ color_red.save
286
+ color_green = Color.new(:name => "green")
287
+ color_green.save
288
+
289
+ flag = Flag.new
290
+ flag.add_color(color_red)
291
+ flag.add_color(color_green)
292
+ flag.save
293
+
294
+ # Saves:
295
+ # { "_id" : ObjectId("4be96c15715dd2c4be000003"),
296
+ # "_class" : "Flag",
297
+ # "colors_refs" : [
298
+ # { "$ns" : "colors",
299
+ # "$id" : {
300
+ # "$oid" : "4d60ea4e3f5f19cf10000001"
301
+ # }
302
+ # },
303
+ # { "$ns" : "colors",
304
+ # "$id" : {
305
+ # "$oid" : "4d60ea4e3f5f19cf10000002"
306
+ # }
307
+ # }
308
+ # ]
309
+ # }
310
+
311
+ flag.colors # Returns [#<Color _id: BSON::ObjectId('4d60ea4e3f5f19cf10000001'), name: "red">, #<Color _id: BSON::ObjectId('4d60ea4e3f5f19cf10000002'), name: "green">]
312
+
313
+ flag.colors
314
+
315
+ # Returns a criteria object that wraps a cursor
316
+
317
+ flag.colors.to_a
318
+
319
+ # Returns:
320
+ # [#<Color name: "red", _id: {"$oid"=>"4be96bfe715dd2c4be000001"}>, #<Color name: "green", _id: {"$oid"=>"4be96c08715dd2c4be000002"}>]
321
+
322
+ Or you can build your custon methods. Example:
238
323
 
239
324
  class Flag
240
325
  include MongoODM::Document
data/lib/mongo_odm.rb CHANGED
@@ -46,5 +46,9 @@ module MongoODM
46
46
  end
47
47
  value.class.type_cast(value.to_mongo)
48
48
  end
49
+
50
+ def self.dereference(value)
51
+ value.respond_to?(:dereference) ? value.dereference : value
52
+ end
49
53
 
50
54
  end
@@ -26,6 +26,27 @@ class BSON::ObjectId
26
26
  end
27
27
  end
28
28
 
29
+ # @private
30
+ class BSON::DBRef
31
+ def self.type_cast(value)
32
+ return value if value.is_a?(BSON::DBRef)
33
+ return value.to_dbref if value.respond_to?(:to_dbref)
34
+ nil
35
+ end
36
+
37
+ def to_mongo
38
+ self
39
+ end
40
+
41
+ def dereference
42
+ MongoODM.instanciate(MongoODM.database.dereference(self))
43
+ end
44
+
45
+ def inspect
46
+ "BSON::DBRef(namespace:\"#{namespace}\", id: \"#{object_id}\")"
47
+ end
48
+ end
49
+
29
50
  # @private
30
51
  class Array
31
52
  def self.type_cast(value)
@@ -36,6 +57,10 @@ class Array
36
57
  def to_mongo
37
58
  self.map {|elem| elem.to_mongo}
38
59
  end
60
+
61
+ def dereference
62
+ MongoODM.instanciate(self.map{|value| MongoODM.dereference(value)})
63
+ end
39
64
  end
40
65
 
41
66
  # @private
@@ -219,6 +244,10 @@ class Hash
219
244
  def to_mongo
220
245
  Hash[self.map{|k,v| [k.to_mongo, v.to_mongo]}]
221
246
  end
247
+
248
+ def dereference
249
+ Hash[self.map{|k,v| [MongoODM.instanciate(MongoODM.dereference(k)), MongoODM.instanciate(MongoODM.dereference(v))]}]
250
+ end
222
251
  end
223
252
 
224
253
  # @private
@@ -57,6 +57,10 @@ module MongoODM
57
57
  attrs
58
58
  end
59
59
  end
60
+
61
+ def to_dbref
62
+ BSON::DBRef.new(self.class.collection.name, _id)
63
+ end
60
64
  end
61
65
 
62
66
  module ClassMethods
@@ -2,6 +2,6 @@
2
2
  module MongoODM
3
3
  VERSION_MAJOR = "0"
4
4
  VERSION_MINOR = "2"
5
- VERSION_BUILD = "6"
5
+ VERSION_BUILD = "7"
6
6
  VERSION = "#{VERSION_MAJOR}.#{VERSION_MINOR}.#{VERSION_BUILD}"
7
7
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: mongo_odm
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.6
5
+ version: 0.2.7
6
6
  platform: ruby
7
7
  authors:
8
8
  - Carlos Paramio