mongodb 0.0.11 → 0.0.12

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.
@@ -1,4 +1,4 @@
1
- require 'mongo/gems'
1
+ require 'mongodb/gems'
2
2
 
3
3
  require 'mongo'
4
4
 
@@ -1,7 +1,7 @@
1
1
  require 'set'
2
2
  require 'date'
3
3
 
4
- module Mongo::CollectionExt
4
+ module Mongo::CollectionExt
5
5
  #
6
6
  # CRUD
7
7
  #
@@ -1,6 +1,6 @@
1
1
  module Mongo::ConnectionExt
2
2
  attr_writer :logger
3
-
3
+
4
4
  protected
5
5
  def method_missing db_name
6
6
  self.db db_name.to_s
@@ -2,14 +2,14 @@ module Mongo::DBExt
2
2
  def drop
3
3
  connection.drop_database name
4
4
  end
5
-
5
+
6
6
  def clear
7
7
  collection_names.each do |name|
8
8
  next if name =~ /^system\./
9
9
  mongo.db.collection(name).drop
10
10
  end
11
11
  end
12
-
12
+
13
13
  protected
14
14
  def method_missing collection_name
15
15
  self.collection collection_name
@@ -1,6 +1,7 @@
1
1
  require 'mongo/driver'
2
2
 
3
3
  %w(
4
+ support
4
5
  object
5
6
  object_helper
6
7
  ).each{|f| require "mongo/object/#{f}"}
@@ -2,15 +2,19 @@ module Mongo::Object
2
2
  attr_accessor :_id, :_parent
3
3
 
4
4
  def valid? options = {}
5
- options = ::Mongo::Object.parse_options options
6
5
  errors.clear
6
+ run_validations options
7
+ end
8
+
9
+ def run_validations options = {}
10
+ options = ::Mongo::Object.parse_options options
7
11
  begin
8
12
  return false if options[:callbacks] and !::Mongo::Object.run_before_callbacks(self, :validate)
9
13
 
10
14
  child_options = options.merge internal: true
11
15
  result = [
16
+ (respond_to?(:run_model_validations) ? run_model_validations : true),
12
17
  child_objects.all?{|group| group.all?{|obj| obj.valid?(child_options)}},
13
- ::Mongo::Object.run_validations(self),
14
18
  errors.empty?
15
19
  ].all?
16
20
 
@@ -18,12 +22,12 @@ module Mongo::Object
18
22
 
19
23
  result
20
24
  ensure
21
- clear_child_objects unless options[:internal]
25
+ clear_child_objects_cache unless options[:internal]
22
26
  end
23
27
  end
24
28
 
25
29
  def errors
26
- @_errors ||= {}
30
+ @_errors ||= Hash.new([])
27
31
  end
28
32
 
29
33
  def create_object collection, options
@@ -69,7 +73,7 @@ module Mongo::Object
69
73
  return options
70
74
  end
71
75
 
72
- def to_mongo_options options
76
+ def parse_mongo_options options
73
77
  options = options.clone
74
78
  options.delete :validate
75
79
  options.delete :callbacks
@@ -83,7 +87,7 @@ module Mongo::Object
83
87
  end
84
88
 
85
89
  def instance_variables obj
86
- # skipping variables starting with _xx, usually they
90
+ # skipping variables starting with @_, usually they
87
91
  # have specific meaning and used for example for cache
88
92
  obj.instance_variables.select{|n| n !~ SKIP_IV_REGEXP}
89
93
  end
@@ -138,8 +142,10 @@ module Mongo::Object
138
142
 
139
143
  def each_object obj, &block
140
144
  if obj.is_a? Hash
145
+ block.call obj if obj.is_a? ::Mongo::Object
141
146
  obj.each{|k, v| each_object v, &block}
142
147
  elsif obj.is_a? Array
148
+ block.call obj if obj.is_a? ::Mongo::Object
143
149
  obj.each{|v| each_object v, &block}
144
150
  elsif obj.is_a? ::Mongo::Object
145
151
  block.call obj
@@ -167,7 +173,7 @@ module Mongo::Object
167
173
  end
168
174
 
169
175
  def run_after_callbacks obj, method
170
- if obj.respond_to?(:run_before_callbacks)
176
+ if obj.respond_to?(:run_after_callbacks)
171
177
  obj.run_after_callbacks(method, method: method)
172
178
  obj.run_after_callbacks(:save, method: :save) if method == :update or method == :create
173
179
  else
@@ -175,10 +181,6 @@ module Mongo::Object
175
181
  end
176
182
  end
177
183
 
178
- def run_validations obj
179
- obj.respond_to?(:run_validations) ? obj.run_validations : true
180
- end
181
-
182
184
  protected
183
185
  def _build doc, parent = nil
184
186
  if doc.is_a? Hash
@@ -199,6 +201,7 @@ module Mongo::Object
199
201
  obj
200
202
  end
201
203
  obj._parent = parent if parent
204
+ run_after_callbacks obj, :build
202
205
  obj
203
206
  else
204
207
  hash = {}
@@ -223,7 +226,6 @@ module Mongo::Object
223
226
  end
224
227
 
225
228
  protected
226
- attr_writer :original_children
227
229
  def original_children; @_original_children ||= [] end
228
230
 
229
231
  def update_original_children!
@@ -235,17 +237,17 @@ module Mongo::Object
235
237
  end
236
238
  end
237
239
 
238
- def clear_child_objects
239
- if instance_variable_get(:@_child_objects)
240
+ def clear_child_objects_cache
241
+ if instance_variable_get(:@_child_objects_cache)
240
242
  child_objects.each do |group|
241
- group.each{|obj| obj.clear_child_objects}
243
+ group.each{|obj| obj.clear_child_objects_cache}
242
244
  end
243
- remove_instance_variable :@_child_objects
245
+ remove_instance_variable :@_child_objects_cache
244
246
  end
245
247
  end
246
248
 
247
249
  def child_objects
248
- unless @_child_objects
250
+ unless @_child_objects_cache
249
251
  created_children, updated_children, destroyed_children = [], [], []
250
252
 
251
253
  original_children_ids = Set.new; original_children.each{|obj| original_children_ids << obj.object_id}
@@ -258,9 +260,9 @@ module Mongo::Object
258
260
  end
259
261
  destroyed_children = original_children.select{|obj| !children_ids.include?(obj.object_id)}
260
262
 
261
- @_child_objects = [created_children, updated_children, destroyed_children]
263
+ @_child_objects_cache = [created_children, updated_children, destroyed_children]
262
264
  end
263
- @_child_objects
265
+ @_child_objects_cache
264
266
  end
265
267
 
266
268
  def with_object_callbacks method, options, &block
@@ -273,7 +275,7 @@ module Mongo::Object
273
275
  return false if options[:callbacks] and !run_all_callbacks(:before, method)
274
276
 
275
277
  # saving
276
- block.call ::Mongo::Object.to_mongo_options(options)
278
+ block.call ::Mongo::Object.parse_mongo_options(options)
277
279
  update_original_children!
278
280
 
279
281
  # after callbacks
@@ -281,10 +283,9 @@ module Mongo::Object
281
283
 
282
284
  true
283
285
  ensure
284
- clear_child_objects
286
+ clear_child_objects_cache
285
287
  end
286
288
 
287
- # TODO1 move to static method
288
289
  def run_all_callbacks type, method
289
290
  result = if type == :before
290
291
  ::Mongo::Object.run_before_callbacks self, method
File without changes
File without changes
@@ -3,95 +3,113 @@ require 'object/spec_helper'
3
3
  describe 'Object callbacks' do
4
4
  with_mongo
5
5
 
6
- before :all do
7
- class Player
8
- include Mongo::Object, RSpec::CallbackHelper
9
- attr_accessor :missions
6
+ [Object, Array, Hash].each do |embedded_object_superclass|
7
+ embedded_object_class = nil
8
+ before :all do
9
+ class MainObject
10
+ include Mongo::Object, RSpec::CallbackHelper
11
+
12
+ attr_accessor :children
13
+ end
10
14
 
11
- class Mission
15
+ embedded_object_class = Class.new embedded_object_superclass do
12
16
  include Mongo::Object, RSpec::CallbackHelper
13
17
  end
14
18
  end
15
- end
16
- after(:all){remove_constants :Player}
19
+ after(:all){remove_constants :MainObject}
17
20
 
18
- before do
19
- @mission = Player::Mission.new
20
- @player = Player.new
21
- @player.missions = [@mission]
22
- end
23
-
24
- it 'create' do
25
- %w(before_validate after_validate before_save before_create after_create after_save).each do |name|
26
- @player.should_receive(name).once.ordered.and_return(true)
27
- @mission.should_receive(name).once.ordered.and_return(true)
21
+ before do
22
+ @child = embedded_object_class.new
23
+ @object = MainObject.new
24
+ @object.children = [@child]
28
25
  end
29
26
 
30
- db.players.save(@player).should be_true
31
- end
32
-
33
- it 'update' do
34
- db.players.save(@player).should be_true
27
+ it 'create' do
28
+ %w(before_validate after_validate before_save before_create after_create after_save).each do |name|
29
+ @object.should_receive(name).once.ordered.and_return(true)
30
+ @child.should_receive(name).once.ordered.and_return(true)
31
+ end
35
32
 
36
- %w(before_validate after_validate before_save before_update after_update after_save).each do |name|
37
- @player.should_receive(name).once.ordered.and_return(true)
38
- @mission.should_receive(name).once.ordered.and_return(true)
33
+ db.objects.save(@object).should be_true
39
34
  end
40
- db.players.save(@player).should be_true
41
- end
42
35
 
43
- it 'destroy' do
44
- db.players.save(@player).should be_true
36
+ it 'update' do
37
+ db.objects.save(@object).should be_true
45
38
 
46
- %w(before_validate after_validate before_destroy after_destroy).each do |name|
47
- @player.should_receive(name).once.ordered.and_return(true)
48
- @mission.should_receive(name).once.ordered.and_return(true)
39
+ %w(before_validate after_validate before_save before_update after_update after_save).each do |name|
40
+ @object.should_receive(name).once.ordered.and_return(true)
41
+ @child.should_receive(name).once.ordered.and_return(true)
42
+ end
43
+ db.objects.save(@object).should be_true
49
44
  end
50
- db.players.destroy(@player).should be_true
51
- end
52
45
 
53
- it 'should be able skip callbacks' do
54
- @player.should_not_receive(:run_callbacks)
55
- @mission.should_not_receive(:run_callbacks)
46
+ it 'destroy' do
47
+ db.objects.save(@object).should be_true
56
48
 
57
- db.players.save(@player, callbacks: false).should be_true
58
- db.players.count.should == 1
59
- db.players.save(@player, callbacks: false).should be_true
60
- db.players.count.should == 1
61
- db.players.destroy(@player, callbacks: false).should be_true
62
- db.players.count.should == 0
63
- end
49
+ %w(before_validate after_validate before_destroy after_destroy).each do |name|
50
+ @object.should_receive(name).once.ordered.and_return(true)
51
+ @child.should_receive(name).once.ordered.and_return(true)
52
+ end
53
+ db.objects.destroy(@object).should be_true
54
+ end
64
55
 
65
- it 'should be able interrupt CRUD' do
66
- @mission.stub! :run_before_callbacks do |method_name|
67
- false if method_name == :create
56
+ it 'should be able skip callbacks' do
57
+ @object.should_not_receive(:run_callbacks)
58
+ @child.should_not_receive(:run_callbacks)
59
+
60
+ db.objects.save(@object, callbacks: false).should be_true
61
+ db.objects.count.should == 1
62
+ db.objects.save(@object, callbacks: false).should be_true
63
+ db.objects.count.should == 1
64
+ db.objects.destroy(@object, callbacks: false).should be_true
65
+ db.objects.count.should == 0
68
66
  end
69
- db.players.save(@player).should be_false
70
- db.players.count.should == 0
71
- end
72
67
 
73
- describe "embedded" do
74
- it 'should fire :destroy on detached objects' do
75
- db.players.save(@player).should be_true
76
- @player.missions.clear
77
- @mission.should_receive(:before_destroy).once.and_return(true)
78
- db.players.destroy(@player).should be_true
68
+ it 'should be able interrupt CRUD' do
69
+ @child.stub! :run_before_callbacks do |method_name|
70
+ false if method_name == :create
71
+ end
72
+ db.objects.save(@object).should be_false
73
+ db.objects.count.should == 0
79
74
  end
80
75
 
81
- it 'should fire :destroy on deleted objects in update' do
82
- db.players.save(@player).should be_true
83
- @player.missions.clear
84
- @mission.should_receive(:before_destroy).once.and_return(true)
85
- db.players.save(@player).should be_true
76
+ describe "embedded" do
77
+ it 'should fire :destroy on detached objects' do
78
+ db.objects.save(@object).should be_true
79
+ @object.children.clear
80
+ @child.should_receive(:before_destroy).once.and_return(true)
81
+ db.objects.destroy(@object).should be_true
82
+ end
83
+
84
+ it 'should fire :destroy on deleted objects in update' do
85
+ db.objects.save(@object).should be_true
86
+ @object.children.clear
87
+ @child.should_receive(:before_destroy).once.and_return(true)
88
+ db.objects.save(@object).should be_true
89
+ end
90
+
91
+ it 'should fire :create on new objects in update' do
92
+ db.objects.save(@object).should be_true
93
+ child2 = embedded_object_class.new
94
+ @object.children << child2
95
+ child2.should_receive(:before_create).once.and_return(true)
96
+ child2.should_not_receive(:before_update)
97
+ db.objects.save(@object).should be_true
98
+ end
86
99
  end
87
100
 
88
- it 'should fire :create on new objects in update' do
89
- db.players.save(@player).should be_true
90
- mission2 = Player::Mission.new
91
- @player.missions << mission2
92
- mission2.should_receive(:before_create).once.and_return(true)
93
- mission2.should_not_receive(:before_update)
94
- db.players.save(@player).should be_true
101
+ it "should fire :after_build callback after building the object" do
102
+ class SpecialMainObject < MainObject
103
+ def run_after_callbacks method, options
104
+ self.class.build_callback if method == :build
105
+ end
106
+ end
107
+
108
+ player = SpecialMainObject.new
109
+ db.objects.save! player
110
+
111
+ SpecialMainObject.should_receive :build_callback
112
+ db.objects.first
95
113
  end
96
114
  end
97
115
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongodb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.12
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,19 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-15 00:00:00.000000000Z
13
- dependencies: []
12
+ date: 2011-09-19 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongo
16
+ requirement: &2782360 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2782360
14
25
  description:
15
26
  email:
16
27
  executables: []
@@ -25,7 +36,6 @@ files:
25
36
  - lib/mongo/driver/dynamic_finders.rb
26
37
  - lib/mongo/driver/spec.rb
27
38
  - lib/mongo/driver.rb
28
- - lib/mongo/gems.rb
29
39
  - lib/mongo/migration/definition.rb
30
40
  - lib/mongo/migration/dsl.rb
31
41
  - lib/mongo/migration/migration.rb
@@ -35,7 +45,9 @@ files:
35
45
  - lib/mongo/object/object_helper.rb
36
46
  - lib/mongo/object/spec/crud_shared.rb
37
47
  - lib/mongo/object/spec.rb
48
+ - lib/mongo/object/support.rb
38
49
  - lib/mongo/object.rb
50
+ - lib/mongodb/gems.rb
39
51
  - spec/driver/collection_spec.rb
40
52
  - spec/driver/connection_spec.rb
41
53
  - spec/driver/crud_spec.rb