mongoo 0.5.5 → 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.5
1
+ 0.5.6
@@ -44,6 +44,11 @@ module Mongoo
44
44
  end
45
45
  protected :init_from_hash
46
46
 
47
+ def reset_persisted_mongohash
48
+ @persisted = true
49
+ set_persisted_mongohash(mongohash)
50
+ end
51
+
47
52
  def set_mongohash(mongohash)
48
53
  @mongohash = mongohash
49
54
  end
@@ -27,7 +27,11 @@ module Mongoo
27
27
  def cast_value(v)
28
28
  if v.is_a?(Mongoo::Embedded::Base)
29
29
  return v.to_hash
30
- end; v
30
+ elsif v.is_a?(Array)
31
+ v.collect { |e| e.is_a?(Mongoo::Embedded::Base) ? e.to_hash : e }
32
+ else
33
+ v
34
+ end
31
35
  end
32
36
 
33
37
  def inc(k, v=1)
@@ -106,6 +110,7 @@ module Mongoo
106
110
  if !ret.is_a?(Hash) || (ret["err"] == nil && ret["n"] == 1)
107
111
  @queue.each do |op, op_queue|
108
112
  op_queue.each do |k, v|
113
+ duped_v = Marshal.load(Marshal.dump(v))
109
114
  case op
110
115
  when "$inc" then
111
116
  new_val = @doc.persisted_mongohash.dot_get(k).to_i + v
@@ -113,7 +118,7 @@ module Mongoo
113
118
  @doc.persisted_mongohash.dot_set( k, new_val )
114
119
  when "$set" then
115
120
  @doc.mongohash.dot_set( k, v )
116
- @doc.persisted_mongohash.dot_set( k, v )
121
+ @doc.persisted_mongohash.dot_set( k, duped_v )
117
122
  when "$unset" then
118
123
  @doc.mongohash.dot_delete( k )
119
124
  @doc.persisted_mongohash.dot_delete( k )
@@ -125,7 +130,7 @@ module Mongoo
125
130
  @doc.mongohash.dot_set(k, [])
126
131
  end
127
132
 
128
- @doc.persisted_mongohash.dot_get(k) << v
133
+ @doc.persisted_mongohash.dot_get(k) << duped_v
129
134
  @doc.mongohash.dot_get(k) << v
130
135
  when "$pushAll" then
131
136
  unless @doc.persisted_mongohash.dot_get(k)
@@ -135,7 +140,7 @@ module Mongoo
135
140
  @doc.mongohash.dot_set(k, [])
136
141
  end
137
142
 
138
- @doc.persisted_mongohash.dot_get(k).concat(v)
143
+ @doc.persisted_mongohash.dot_get(k).concat(duped_v)
139
144
  @doc.mongohash.dot_get(k).concat(v)
140
145
  when "$addToSet" then
141
146
  unless @doc.persisted_mongohash.dot_get(k)
@@ -146,7 +151,7 @@ module Mongoo
146
151
  end
147
152
 
148
153
  unless @doc.persisted_mongohash.dot_get(k).include?(v)
149
- @doc.persisted_mongohash.dot_get(k) << v
154
+ @doc.persisted_mongohash.dot_get(k) << duped_v
150
155
  end
151
156
  unless @doc.mongohash.dot_get(k).include?(v)
152
157
  @doc.mongohash.dot_get(k) << v
@@ -12,6 +12,33 @@ module Mongoo
12
12
 
13
13
  module Persistence
14
14
 
15
+ class RawUpdate
16
+ attr_accessor :criteria, :updates, :opts
17
+
18
+ def initialize(doc)
19
+ @doc = doc
20
+ @criteria ||= {}
21
+ @updates ||= {}
22
+ @opts ||= {}
23
+ end
24
+
25
+ def will_change(&block)
26
+ @will_change_block = block
27
+ end
28
+
29
+ def run
30
+ @criteria.stringify_keys!
31
+ @criteria["_id"] = @doc.id
32
+ ret = @doc.collection.update(self.criteria, self.updates, self.opts)
33
+ if !ret.is_a?(Hash) || (ret["updatedExisting"] && ret["n"] == 1)
34
+ if @will_change_block
35
+ @will_change_block.call(@doc, ret)
36
+ end
37
+ end
38
+ ret
39
+ end
40
+ end
41
+
15
42
  def self.included(base)
16
43
  base.extend(ClassMethods)
17
44
  end
@@ -210,6 +237,12 @@ module Mongoo
210
237
  insert(opts.merge(:safe => true))
211
238
  end
212
239
 
240
+ def raw_update(&block)
241
+ raw = RawUpdate.new(self)
242
+ block.call(raw)
243
+ raw.run
244
+ end
245
+
213
246
  def update(opts={})
214
247
  _run_update_callbacks do
215
248
  unless persisted?
@@ -240,8 +273,7 @@ module Mongoo
240
273
  else
241
274
  ret = self.collection.update(update_query_hash, update_hash, opts)
242
275
  if !ret.is_a?(Hash) || (ret["updatedExisting"] && ret["n"] == 1)
243
- set_persisted_mongohash(mongohash)
244
- @persisted = true
276
+ reset_persisted_mongohash
245
277
  true
246
278
  else
247
279
  if opts[:only_if_current]
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoo}
8
- s.version = "0.5.5"
8
+ s.version = "0.5.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ben Myles"]
12
- s.date = %q{2011-06-16}
12
+ s.date = %q{2011-06-20}
13
13
  s.description = %q{Simple object mapper for MongoDB}
14
14
  s.email = %q{ben.myles@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -240,4 +240,58 @@ class TestEmbedded < Test::Unit::TestCase
240
240
  assert_equal 1, c.addresses.size
241
241
  assert_equal [address], c.addresses.to_a
242
242
  end
243
+
244
+ should "be able to change an element in an embedded array doc" do
245
+ c = Customer.new(name: "Ben")
246
+ c.insert!
247
+
248
+ address = c.addresses.build(street: "123 Street", city: "Metropolis")
249
+ c.mod! do |m|
250
+ m.push 'addresses', address
251
+ end
252
+
253
+ assert_equal address, c.addresses.first
254
+ assert_equal address, c.addresses[0]
255
+ assert_equal 1, c.addresses.size
256
+
257
+ addr = c.addresses[0]
258
+ addr.street = "456 Street"
259
+
260
+ assert_equal [[:set, "addresses", [{"street"=>"456 Street", "city"=>"Metropolis"}]]], c.changelog
261
+ end
262
+
263
+ should "be able to do a custom update with modifiers" do
264
+ c = Customer.new(name: "Ben")
265
+ c.insert!
266
+
267
+ addresses = []
268
+ %w(123 456 789).each do |s|
269
+ addresses << c.addresses.build(street: "123 Street", city: "Metropolis")
270
+ end
271
+ c.mod! do |m|
272
+ m.push_all 'addresses', addresses
273
+ end
274
+
275
+ 2.times do
276
+ assert_equal addresses[0], c.addresses.first
277
+ assert_equal addresses[2], c.addresses.last
278
+ assert_equal 3, c.addresses.size
279
+ c = Customer.find_one(c.id)
280
+ end
281
+
282
+ resp = c.raw_update do |u|
283
+ u.updates = { "$push" => { "addresses" => { "street" => "1 E Main St", "city" => "NY" } } }
284
+ u.opts = { safe: true }
285
+ u.will_change do |doc, res|
286
+ doc.addresses.raw << { "street" => "1 E Main St", "city" => "NY" }
287
+ doc.persisted_mongohash["addresses"] = doc.addresses.raw
288
+ end
289
+ end
290
+
291
+ 2.times do
292
+ assert_equal 4, c.addresses.size
293
+ assert_equal "NY", c.addresses.last.city
294
+ c = Customer.find_one(c.id)
295
+ end
296
+ end
243
297
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.5
4
+ version: 0.5.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-06-16 00:00:00.000000000 -07:00
12
+ date: 2011-06-20 00:00:00.000000000 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: i18n
17
- requirement: &2151899320 !ruby/object:Gem::Requirement
17
+ requirement: &2151897000 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 0.4.1
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *2151899320
25
+ version_requirements: *2151897000
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: activesupport
28
- requirement: &2151897440 !ruby/object:Gem::Requirement
28
+ requirement: &2151895180 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: 3.0.3
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *2151897440
36
+ version_requirements: *2151895180
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: activemodel
39
- requirement: &2151896040 !ruby/object:Gem::Requirement
39
+ requirement: &2151893580 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: 3.0.3
45
45
  type: :runtime
46
46
  prerelease: false
47
- version_requirements: *2151896040
47
+ version_requirements: *2151893580
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: mongo
50
- requirement: &2151893780 !ruby/object:Gem::Requirement
50
+ requirement: &2151891620 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ~>
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: 1.3.1
56
56
  type: :runtime
57
57
  prerelease: false
58
- version_requirements: *2151893780
58
+ version_requirements: *2151891620
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: ruby-debug19
61
- requirement: &2151891820 !ruby/object:Gem::Requirement
61
+ requirement: &2151889100 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ! '>='
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: '0'
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *2151891820
69
+ version_requirements: *2151889100
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: shoulda
72
- requirement: &2151889020 !ruby/object:Gem::Requirement
72
+ requirement: &2151887680 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ! '>='
@@ -77,10 +77,10 @@ dependencies:
77
77
  version: '0'
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *2151889020
80
+ version_requirements: *2151887680
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: bundler
83
- requirement: &2151887240 !ruby/object:Gem::Requirement
83
+ requirement: &2151885300 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
86
  - - ~>
@@ -88,10 +88,10 @@ dependencies:
88
88
  version: 1.0.0
89
89
  type: :development
90
90
  prerelease: false
91
- version_requirements: *2151887240
91
+ version_requirements: *2151885300
92
92
  - !ruby/object:Gem::Dependency
93
93
  name: jeweler
94
- requirement: &2151885260 !ruby/object:Gem::Requirement
94
+ requirement: &2151882720 !ruby/object:Gem::Requirement
95
95
  none: false
96
96
  requirements:
97
97
  - - ~>
@@ -99,10 +99,10 @@ dependencies:
99
99
  version: 1.5.1
100
100
  type: :development
101
101
  prerelease: false
102
- version_requirements: *2151885260
102
+ version_requirements: *2151882720
103
103
  - !ruby/object:Gem::Dependency
104
104
  name: rcov
105
- requirement: &2151882160 !ruby/object:Gem::Requirement
105
+ requirement: &2151880160 !ruby/object:Gem::Requirement
106
106
  none: false
107
107
  requirements:
108
108
  - - ! '>='
@@ -110,10 +110,10 @@ dependencies:
110
110
  version: '0'
111
111
  type: :development
112
112
  prerelease: false
113
- version_requirements: *2151882160
113
+ version_requirements: *2151880160
114
114
  - !ruby/object:Gem::Dependency
115
115
  name: perftools.rb
116
- requirement: &2151880020 !ruby/object:Gem::Requirement
116
+ requirement: &2151877020 !ruby/object:Gem::Requirement
117
117
  none: false
118
118
  requirements:
119
119
  - - ! '>='
@@ -121,10 +121,10 @@ dependencies:
121
121
  version: '0'
122
122
  type: :development
123
123
  prerelease: false
124
- version_requirements: *2151880020
124
+ version_requirements: *2151877020
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: bson_ext
127
- requirement: &2151877880 !ruby/object:Gem::Requirement
127
+ requirement: &2151876040 !ruby/object:Gem::Requirement
128
128
  none: false
129
129
  requirements:
130
130
  - - ~>
@@ -132,7 +132,7 @@ dependencies:
132
132
  version: 1.3.1
133
133
  type: :development
134
134
  prerelease: false
135
- version_requirements: *2151877880
135
+ version_requirements: *2151876040
136
136
  description: Simple object mapper for MongoDB
137
137
  email: ben.myles@gmail.com
138
138
  executables: []
@@ -197,7 +197,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
197
197
  version: '0'
198
198
  segments:
199
199
  - 0
200
- hash: 2334518242117856164
200
+ hash: -2464241099549832500
201
201
  required_rubygems_version: !ruby/object:Gem::Requirement
202
202
  none: false
203
203
  requirements: