mongodb 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,34 +5,34 @@ module Mongo::CollectionExt
5
5
  #
6
6
  # CRUD
7
7
  #
8
- def save_with_ext doc, opts = {}
9
- save_without_ext doc, reverse_merge_defaults(opts, :safe)
8
+ def save_with_ext doc, options = {}
9
+ save_without_ext doc, reverse_merge_defaults(options, :safe)
10
10
  end
11
11
 
12
- def insert_with_ext args, opts = {}
13
- result = insert_without_ext args, reverse_merge_defaults(opts, :safe)
12
+ def insert_with_ext args, options = {}
13
+ result = insert_without_ext args, reverse_merge_defaults(options, :safe)
14
14
 
15
15
  # fix for mongodriver, it will return single result if we supply [doc] as args
16
16
  (args.is_a?(Array) and !result.is_a?(Array)) ? [result] : result
17
17
  end
18
18
 
19
- def update_with_ext selector, doc, opts = {}
19
+ def update_with_ext selector, doc, options = {}
20
20
  selector = convert_underscore_to_dollar_in_selector selector
21
21
  doc = convert_underscore_to_dollar_in_update doc
22
22
 
23
23
  # because :multi works only with $ operators, we need to check if it's applicable
24
- opts = if doc.keys.any?{|k| k =~ /^\$/}
25
- reverse_merge_defaults(opts, :safe, :multi)
24
+ options = if doc.keys.any?{|k| k =~ /^\$/}
25
+ reverse_merge_defaults(options, :safe, :multi)
26
26
  else
27
- reverse_merge_defaults(opts, :safe)
27
+ reverse_merge_defaults(options, :safe)
28
28
  end
29
29
 
30
- update_without_ext selector, doc, opts
30
+ update_without_ext selector, doc, options
31
31
  end
32
32
 
33
- def remove_with_ext selector = {}, opts = {}
33
+ def remove_with_ext selector = {}, options = {}
34
34
  selector = convert_underscore_to_dollar_in_selector selector
35
- remove_without_ext selector, reverse_merge_defaults(opts, :safe, :multi)
35
+ remove_without_ext selector, reverse_merge_defaults(options, :safe, :multi)
36
36
  end
37
37
 
38
38
  def destroy *args
@@ -46,33 +46,33 @@ module Mongo::CollectionExt
46
46
  #
47
47
  # Querying
48
48
  #
49
- def first selector = {}, opts = {}
49
+ def first selector = {}, options = {}
50
50
  selector = convert_underscore_to_dollar_in_selector selector if selector.is_a? Hash
51
51
 
52
- h = find_one selector, opts
52
+ h = find_one selector, options
53
53
  symbolize_doc h
54
54
  end
55
55
 
56
- def first! selector = {}, opts = {}
57
- first(selector, opts) || raise(Mongo::NotFound, "document with selector #{selector} not found!")
56
+ def first! selector = {}, options = {}
57
+ first(selector, options) || raise(Mongo::NotFound, "document with selector #{selector} not found!")
58
58
  end
59
59
 
60
- def all selector = {}, opts = {}, &block
60
+ def all selector = {}, options = {}, &block
61
61
  if block
62
- each selector, opts, &block
62
+ each selector, options, &block
63
63
  else
64
64
  list = []
65
- each(selector, opts){|doc| list << doc}
65
+ each(selector, options){|doc| list << doc}
66
66
  list
67
67
  end
68
68
  end
69
69
 
70
- def each selector = {}, opts = {}, &block
70
+ def each selector = {}, options = {}, &block
71
71
  selector = convert_underscore_to_dollar_in_selector selector
72
72
 
73
73
  cursor = nil
74
74
  begin
75
- cursor = find selector, reverse_merge_defaults(opts, :batch_size)
75
+ cursor = find selector, reverse_merge_defaults(options, :batch_size)
76
76
  cursor.each do |doc|
77
77
  doc = symbolize_doc doc
78
78
  block.call doc
@@ -84,8 +84,8 @@ module Mongo::CollectionExt
84
84
  nil
85
85
  end
86
86
 
87
- def count_with_ext selector = {}, opts = {}
88
- find(selector, opts).count()
87
+ def count_with_ext selector = {}, options = {}
88
+ find(selector, options).count()
89
89
  end
90
90
 
91
91
  protected
@@ -100,8 +100,8 @@ module Mongo::CollectionExt
100
100
  :_inc, :_set, :_unset, :_push, :_pushAll, :_addToSet, :_pop, :_pull, :_pullAll, :_rename, :_bit
101
101
  ].to_set
102
102
 
103
- def reverse_merge_defaults opts, *keys
104
- h = opts.clone
103
+ def reverse_merge_defaults options, *keys
104
+ h = options.clone
105
105
  keys.each do |k|
106
106
  h[k] = Mongo.defaults[k] if Mongo.defaults.include?(k) and !h.include?(k)
107
107
  end
@@ -1,23 +1,23 @@
1
1
  module Mongo::Object
2
- attr_accessor :_id
2
+ attr_accessor :_id, :_parent
3
3
 
4
- def valid? opts = {}
5
- opts = ::Mongo::Object.parse_options opts
4
+ def valid? options = {}
5
+ options = ::Mongo::Object.parse_options options
6
6
  begin
7
- return false if opts[:callbacks] and !::Mongo::Object.run_before_callbacks(self, :validate)
7
+ return false if options[:callbacks] and !::Mongo::Object.run_before_callbacks(self, :validate)
8
8
 
9
- child_opts = opts.merge internal: true
9
+ child_options = options.merge internal: true
10
10
  result = [
11
- child_objects.all?{|group| group.all?{|obj| obj.valid?(child_opts)}},
11
+ child_objects.all?{|group| group.all?{|obj| obj.valid?(child_options)}},
12
12
  ::Mongo::Object.run_validations(self),
13
13
  errors.empty?
14
14
  ].all?
15
15
 
16
- ::Mongo::Object.run_after_callbacks(self, :validate) if opts[:callbacks]
16
+ ::Mongo::Object.run_after_callbacks(self, :validate) if options[:callbacks]
17
17
 
18
18
  result
19
19
  ensure
20
- clear_child_objects unless opts[:internal]
20
+ clear_child_objects unless options[:internal]
21
21
  end
22
22
  end
23
23
 
@@ -25,34 +25,34 @@ module Mongo::Object
25
25
  @_errors ||= {}
26
26
  end
27
27
 
28
- def create_object collection, opts
29
- with_object_callbacks :create, opts do |opts|
28
+ def create_object collection, options
29
+ with_object_callbacks :create, options do |options|
30
30
  doc = ::Mongo::Object.to_mongo self
31
- collection.create(doc, opts)
31
+ collection.create(doc, options)
32
32
  self._id = doc[:_id] || doc['_id']
33
33
  end
34
34
  end
35
35
 
36
- def update_object collection, opts
37
- with_object_callbacks :update, opts do |opts|
36
+ def update_object collection, options
37
+ with_object_callbacks :update, options do |options|
38
38
  id = _id || "can't update object without _id (#{self})!"
39
39
  doc = ::Mongo::Object.to_mongo self
40
- collection.update({_id: id}, doc, opts)
40
+ collection.update({_id: id}, doc, options)
41
41
  end
42
42
  end
43
43
 
44
- def destroy_object collection, opts
45
- with_object_callbacks :destroy, opts do |opts|
44
+ def destroy_object collection, options
45
+ with_object_callbacks :destroy, options do |options|
46
46
  id = _id || "can't destroy object without _id (#{self})!"
47
- collection.destroy({_id: id}, opts)
47
+ collection.destroy({_id: id}, options)
48
48
  end
49
49
  end
50
50
 
51
- def save_object collection, opts
51
+ def save_object collection, options
52
52
  if _id
53
- update_object collection, opts
53
+ update_object collection, options
54
54
  else
55
- create_object collection, opts
55
+ create_object collection, options
56
56
  end
57
57
  end
58
58
 
@@ -61,21 +61,21 @@ module Mongo::Object
61
61
  SKIP_IV_REGEXP = /^@_/
62
62
 
63
63
  class << self
64
- def parse_options opts
65
- opts = opts.clone
66
- opts[:validate] = true unless opts.include?(:validate)
67
- opts[:callbacks] = Mongo.defaults[:callbacks] unless opts.include?(:callbacks)
68
- return opts
64
+ def parse_options options
65
+ options = options.clone
66
+ options[:validate] = true unless options.include?(:validate)
67
+ options[:callbacks] = Mongo.defaults[:callbacks] unless options.include?(:callbacks)
68
+ return options
69
69
  end
70
70
 
71
- def to_mongo_options opts
72
- opts = opts.clone
73
- opts.delete :validate
74
- opts.delete :callbacks
75
- opts
71
+ def to_mongo_options options
72
+ options = options.clone
73
+ options.delete :validate
74
+ options.delete :callbacks
75
+ options
76
76
  end
77
77
 
78
- def each_instance_variable obj, &block
78
+ def each_object_instance_variable obj, &block
79
79
  obj.instance_variables.each do |iv_name|
80
80
  # skipping variables starting with _xx, usually they
81
81
  # have specific meaning and used for example for cache
@@ -102,7 +102,7 @@ module Mongo::Object
102
102
  doc = {}
103
103
 
104
104
  # copying instance variables
105
- each_instance_variable obj do |iv_name, v|
105
+ each_object_instance_variable obj do |iv_name, v|
106
106
  k = iv_name.to_s[1..-1]
107
107
  k = k.to_sym if symbolize
108
108
  doc[k] = to_mongo v
@@ -127,7 +127,7 @@ module Mongo::Object
127
127
  obj.each{|v| each_object v, &block}
128
128
  elsif obj.is_a? ::Mongo::Object
129
129
  block.call obj if include_first
130
- each_instance_variable obj do |iv_name, v|
130
+ each_object_instance_variable obj do |iv_name, v|
131
131
  each_object v, &block
132
132
  end
133
133
  end
@@ -136,7 +136,7 @@ module Mongo::Object
136
136
 
137
137
  def build doc
138
138
  return unless doc
139
- obj = _build doc
139
+ obj = _build doc, nil
140
140
  obj.send :update_original_children! if obj.is_a? ::Mongo::Object
141
141
  obj
142
142
  end
@@ -164,28 +164,33 @@ module Mongo::Object
164
164
  end
165
165
 
166
166
  protected
167
- def _build doc
167
+ def _build doc, parent = nil
168
168
  if doc.is_a? Hash
169
169
  if class_name = doc[:_class] || doc['_class']
170
170
  klass = constantize class_name
171
171
 
172
172
  if klass.respond_to? :from_mongo
173
- klass.from_mongo doc
173
+ obj = klass.from_mongo doc
174
174
  else
175
175
  obj = klass.new
176
+ parent ||= obj
176
177
  doc.each do |k, v|
177
178
  next if k.to_sym == :_class
178
179
 
179
- v = _build v
180
+ v = _build v, parent
180
181
  obj.instance_variable_set "@#{k}", v
181
182
  end
182
183
  obj
183
184
  end
185
+ obj._parent = parent if parent
186
+ obj
184
187
  else
185
- doc
188
+ hash = {}
189
+ doc.each{|k, v| hash[k] = _build v, parent}
190
+ hash
186
191
  end
187
192
  elsif doc.is_a? Array
188
- doc.collect{|v| _build v}
193
+ doc.collect{|v| _build v, parent}
189
194
  else
190
195
  doc
191
196
  end
@@ -240,21 +245,21 @@ module Mongo::Object
240
245
  @_child_objects
241
246
  end
242
247
 
243
- def with_object_callbacks method, opts, &block
244
- opts = ::Mongo::Object.parse_options opts
248
+ def with_object_callbacks method, options, &block
249
+ options = ::Mongo::Object.parse_options options
245
250
 
246
251
  # validation
247
- return false if opts[:validate] and !valid?(opts.merge(internal: true))
252
+ return false if options[:validate] and !valid?(options.merge(internal: true))
248
253
 
249
254
  # before callbacks
250
- return false if opts[:callbacks] and !run_all_callbacks(:before, method)
255
+ return false if options[:callbacks] and !run_all_callbacks(:before, method)
251
256
 
252
257
  # saving
253
- block.call ::Mongo::Object.to_mongo_options(opts)
258
+ block.call ::Mongo::Object.to_mongo_options(options)
254
259
  update_original_children!
255
260
 
256
261
  # after callbacks
257
- run_all_callbacks :after, method if opts[:callbacks]
262
+ run_all_callbacks :after, method if options[:callbacks]
258
263
 
259
264
  true
260
265
  ensure
@@ -2,37 +2,37 @@ module Mongo::ObjectHelper
2
2
  #
3
3
  # CRUD
4
4
  #
5
- def create_with_object doc, opts = {}
5
+ def create_with_object doc, options = {}
6
6
  if doc.is_a? ::Mongo::Object
7
- doc.create_object self, opts
7
+ doc.create_object self, options
8
8
  else
9
- create_without_object doc, opts
9
+ create_without_object doc, options
10
10
  end
11
11
  end
12
12
 
13
13
  def update_with_object *args
14
14
  if args.first.is_a? ::Mongo::Object
15
- doc, opts = args
16
- opts ||= {}
17
- doc.update_object self, opts
15
+ doc, options = args
16
+ options ||= {}
17
+ doc.update_object self, options
18
18
  else
19
19
  update_without_object *args
20
20
  end
21
21
  end
22
22
 
23
- def save_with_object doc, opts = {}
23
+ def save_with_object doc, options = {}
24
24
  if doc.is_a? ::Mongo::Object
25
- doc.save_object self, opts
25
+ doc.save_object self, options
26
26
  else
27
- save_without_object doc, opts
27
+ save_without_object doc, options
28
28
  end
29
29
  end
30
30
 
31
31
  def destroy_with_object *args
32
32
  if args.first.is_a? ::Mongo::Object
33
- doc, opts = args
34
- opts ||= {}
35
- doc.destroy_object self, opts
33
+ doc, options = args
34
+ options ||= {}
35
+ doc.destroy_object self, options
36
36
  else
37
37
  destroy_without_object *args
38
38
  end
@@ -58,21 +58,21 @@ module Mongo::ObjectHelper
58
58
  #
59
59
  # Querying
60
60
  #
61
- def first selector = {}, opts = {}, &block
62
- opts = opts.clone
63
- if opts.delete(:object) == false
64
- super selector, opts, &block
61
+ def first selector = {}, options = {}, &block
62
+ options = options.clone
63
+ if options.delete(:object) == false
64
+ super selector, options, &block
65
65
  else
66
- ::Mongo::Object.build super(selector, opts, &block)
66
+ ::Mongo::Object.build super(selector, options, &block)
67
67
  end
68
68
  end
69
69
 
70
- def each selector = {}, opts = {}, &block
71
- opts = opts.clone
72
- if opts.delete(:object) == false
73
- super selector, opts, &block
70
+ def each selector = {}, options = {}, &block
71
+ options = options.clone
72
+ if options.delete(:object) == false
73
+ super selector, options, &block
74
74
  else
75
- super selector, opts do |doc|
75
+ super selector, options do |doc|
76
76
  block.call ::Mongo::Object.build(doc)
77
77
  end
78
78
  end
@@ -1,4 +1,6 @@
1
+ require 'mongo/object'
1
2
  require 'mongo/driver/spec'
2
3
 
3
4
  # RSpec adds some instance variables and we need to skip it.
5
+ Mongo::Object.send :remove_const, :SKIP_IV_REGEXP
4
6
  Mongo::Object.send :const_set, :SKIP_IV_REGEXP, /^@_|^@mock_proxy/
@@ -40,7 +40,11 @@ shared_examples_for 'embedded object CRUD' do
40
40
 
41
41
  # update
42
42
  @player.missions.first.stats[:units] = 9
43
- @player.missions << @mission_class.new('Desperate Alliance', {buildings: 11, units: 40})
43
+ mission = @mission_class.new.tap do |m|
44
+ m.name = 'Desperate Alliance'
45
+ m.stats = {buildings: 11, units: 40}
46
+ end
47
+ @player.missions << mission
44
48
  db.players.save @player
45
49
  db.players.count.should == 1
46
50
  db.players.first.should == @player
@@ -50,4 +54,10 @@ shared_examples_for 'embedded object CRUD' do
50
54
  db.players.destroy @player
51
55
  db.players.count.should == 0
52
56
  end
57
+
58
+ it "embedded object should have :_parent reference to the main object" do
59
+ db.players.save @player
60
+ player = db.players.first
61
+ player.missions.first._parent.should == player
62
+ end
53
63
  end
@@ -7,12 +7,12 @@ require 'driver/spec_helper'
7
7
 
8
8
  # To simplify callback expectations
9
9
  module RSpec::CallbackHelper
10
- def run_before_callbacks method_name, opts = {}
10
+ def run_before_callbacks method_name, options = {}
11
11
  callback_method_name = :"before_#{method_name}"
12
12
  respond_to?(callback_method_name) ? send(callback_method_name) : true
13
13
  end
14
14
 
15
- def run_after_callbacks method_name, opts = {}
15
+ def run_after_callbacks method_name, options = {}
16
16
  callback_method_name = :"after_#{method_name}"
17
17
  respond_to?(callback_method_name) ? send(callback_method_name) : true
18
18
  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.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-30 00:00:00.000000000Z
12
+ date: 2011-09-02 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: