mongomatic 0.1.2 → 0.1.3

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.
@@ -28,7 +28,7 @@ module Mongomatic
28
28
 
29
29
  def find_one(query={}, opts={})
30
30
  return nil unless doc = self.collection.find_one(query, opts)
31
- self.new(doc)
31
+ self.new(doc, false)
32
32
  end
33
33
 
34
34
  def all
@@ -48,11 +48,20 @@ module Mongomatic
48
48
  end
49
49
  end
50
50
 
51
- attr_accessor :removed
51
+ attr_accessor :removed, :is_new
52
52
 
53
- def initialize(doc={})
53
+ def initialize(doc={}, is_new=true)
54
54
  @doc = doc.stringify_keys
55
55
  self.removed = false
56
+ self.is_new = is_new
57
+ end
58
+
59
+ def is_new?
60
+ self.is_new == true
61
+ end
62
+
63
+ def new?
64
+ self.is_new == true
56
65
  end
57
66
 
58
67
  def []=(k,v)
@@ -74,10 +83,6 @@ module Mongomatic
74
83
  def ==(obj)
75
84
  obj.is_a?(self.class) && obj.doc["_id"] == @doc["_id"]
76
85
  end
77
-
78
- def new?
79
- @doc["_id"] == nil
80
- end
81
86
 
82
87
  def reload
83
88
  if obj = self.class.find({"_id" => @doc["_id"]}).next_document
@@ -90,7 +95,8 @@ module Mongomatic
90
95
  self.send(:before_insert) if self.respond_to?(:before_insert)
91
96
  self.send(:before_insert_or_update) if self.respond_to?(:before_insert_or_update)
92
97
  if ret = self.class.collection.insert(@doc,opts)
93
- @doc["_id"] = @doc.delete(:_id); ret
98
+ @doc["_id"] = @doc.delete(:_id) if @doc[:_id]
99
+ self.is_new = false
94
100
  end
95
101
  self.send(:after_insert) if self.respond_to?(:after_insert)
96
102
  self.send(:after_insert_or_update) if self.respond_to?(:after_insert_or_update)
@@ -20,7 +20,7 @@ module Mongomatic
20
20
 
21
21
  def next_document
22
22
  if doc = @mongo_cursor.next_document
23
- @obj_class.new(doc)
23
+ @obj_class.new(doc, false)
24
24
  end
25
25
  end
26
26
 
@@ -28,14 +28,10 @@ module Mongomatic
28
28
 
29
29
  def each
30
30
  @mongo_cursor.each do |doc|
31
- yield(@obj_class.new(doc))
31
+ yield(@obj_class.new(doc, false))
32
32
  end
33
33
  end
34
34
 
35
- # def to_a
36
- # @mongo_cursor.to_a.collect { |doc| @obj_class.new(doc) }
37
- # end
38
-
39
35
  def current_limit
40
36
  @mongo_cursor.limit
41
37
  end
@@ -183,4 +183,33 @@ class TestMongomatic < Test::Unit::TestCase
183
183
 
184
184
  assert_equal 1, Person.count
185
185
  end
186
+
187
+ should "have the is_new flag set appropriately" do
188
+ Person.collection.drop
189
+ p = Person.new
190
+ assert p.is_new?
191
+ assert !p.insert
192
+ assert p.is_new?
193
+ p["name"] = "Ben"
194
+ assert p.insert
195
+ assert !p.is_new?
196
+ p = Person.find_one(p["_id"])
197
+ assert !p.is_new?
198
+ end
199
+
200
+ should "be able to set a custom id" do
201
+ Person.collection.drop
202
+ p = Person.new(:name => "Ben")
203
+ assert p.is_new?
204
+ p["_id"] = "mycustomid"
205
+ assert p.insert
206
+ found = Person.find_one({"_id" => "mycustomid"})
207
+ assert_equal found, p
208
+ assert !p.is_new?
209
+ found["age"] = 26
210
+ assert found.update
211
+ found = Person.find_one({"_id" => "mycustomid"})
212
+ assert_equal found["_id"], "mycustomid"
213
+ assert_equal 26, found["age"]
214
+ end
186
215
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 2
9
- version: 0.1.2
8
+ - 3
9
+ version: 0.1.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ben Myles