mongoid 2.0.0.rc.8 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -38,10 +38,10 @@ module Mongoid #:nodoc:
38
38
  changes = child._sets
39
39
  updates["$set"].update(changes)
40
40
  unless changes.empty?
41
- processed[child._conficting_modification_key] = true
41
+ processed[child._conflicting_modification_key] = true
42
42
  end
43
43
 
44
- if processed.has_key?(child._conficting_modification_key)
44
+ if processed.has_key?(child._conflicting_modification_key)
45
45
  target = :other
46
46
  else
47
47
  target = "$pushAll"
@@ -70,7 +70,7 @@ module Mongoid #:nodoc:
70
70
  # person._conflicting_modification_key
71
71
  #
72
72
  # @return [ String ] The conflicting key.
73
- def _conficting_modification_key
73
+ def _conflicting_modification_key
74
74
  _path.sub(/\..*/, '')
75
75
  end
76
76
 
@@ -39,8 +39,7 @@ module Mongoid #:nodoc:
39
39
  def read_attribute(name)
40
40
  access = name.to_s
41
41
  value = @attributes[access]
42
- typed_value = fields.has_key?(access) ? fields[access].get(value) : value
43
- accessed(access, typed_value)
42
+ accessed(access, value)
44
43
  end
45
44
  alias :[] :read_attribute
46
45
 
@@ -55,7 +54,7 @@ module Mongoid #:nodoc:
55
54
  # @since 1.0.0
56
55
  def remove_attribute(name)
57
56
  access = name.to_s
58
- modify(access, @attributes.delete(name.to_s), nil)
57
+ modify(access, @attributes.delete(access), nil)
59
58
  end
60
59
 
61
60
  # Override respond_to? so it responds properly for dynamic attributes.
@@ -214,6 +214,21 @@ module Mongoid #:nodoc:
214
214
  end
215
215
  end
216
216
 
217
+ # Returns true if criteria responds to the given method.
218
+ #
219
+ # Options:
220
+ #
221
+ # name: The name of the class method on the +Document+.
222
+ # include_private: The arguments passed to the method.
223
+ #
224
+ # Example:
225
+ #
226
+ # <tt>criteria.respond_to?(:batch_update)</tt>
227
+ def respond_to?(name, include_private = false)
228
+ # don't include klass private methods because method_missing won't call them
229
+ super || @klass.respond_to?(name) || entries.respond_to?(name, include_private)
230
+ end
231
+
217
232
  # Returns the selector and options as a +Hash+ that would be passed to a
218
233
  # scope for use with named scopes.
219
234
  def scoped
@@ -155,7 +155,7 @@ module Mongoid #:nodoc:
155
155
  selector = case selector
156
156
  when String then {"$where" => selector}
157
157
  else
158
- BSON::ObjectId.convert(klass, selector || {}).expand_complex_criteria
158
+ BSON::ObjectId.convert(klass, selector || {}, false).expand_complex_criteria
159
159
  end
160
160
 
161
161
  selector.each_pair do |key, value|
@@ -98,8 +98,8 @@ module Mongoid #:nodoc:
98
98
  #
99
99
  # Example:
100
100
  #
101
- # <tt>criteria.id("4ab2bc4b8ad548971900005c")</tt>
102
- # <tt>criteria.id(["4ab2bc4b8ad548971900005c", "4c454e7ebf4b98032d000001"])</tt>
101
+ # <tt>criteria.for_ids("4ab2bc4b8ad548971900005c")</tt>
102
+ # <tt>criteria.for_ids(["4ab2bc4b8ad548971900005c", "4c454e7ebf4b98032d000001"])</tt>
103
103
  #
104
104
  # Returns: <tt>self</tt>
105
105
  def for_ids(*ids)
@@ -28,8 +28,8 @@ module Mongoid #:nodoc:
28
28
  # @param [ Hash ] conditions The conditions to create with.
29
29
  #
30
30
  # @since 2.0.0.rc.1
31
- def default_scope(conditions = {}, &block)
32
- self.default_scoping = Scope.new(conditions, &block).conditions.scoped
31
+ def default_scope(conditions = {})
32
+ self.default_scoping = Scope.new(conditions).conditions.scoped
33
33
  end
34
34
  end
35
35
  end
@@ -192,8 +192,9 @@ module Mongoid #:nodoc:
192
192
  #
193
193
  # <tt>person.accessed("aliases", [ "007" ])</tt>
194
194
  def accessed(name, value)
195
+ return value unless value.is_a?(Enumerable)
195
196
  @accessed ||= {}
196
- @accessed[name] = value.dup if (value.is_a?(Array) || value.is_a?(Hash)) && !@accessed.has_key?(name)
197
+ @accessed[name] = value.dup unless @accessed.has_key?(name)
197
198
  value
198
199
  end
199
200
 
@@ -21,7 +21,7 @@ module Mongoid #:nodoc:
21
21
  #
22
22
  # @return [ Integer ] -1, 0, 1.
23
23
  def <=>(other)
24
- id.to_s <=> other.id.to_s
24
+ raw_attributes["_id"].to_s <=> other.raw_attributes["_id"].to_s
25
25
  end
26
26
 
27
27
  # Performs equality checking on the document ids. For more robust
@@ -34,8 +34,8 @@ module Mongoid #:nodoc:
34
34
  #
35
35
  # @return [ true, false ] True if the ids are equal, false if not.
36
36
  def ==(other)
37
- return false unless other.is_a?(Document)
38
- id == other.id || equal?(other)
37
+ self.class == other.class &&
38
+ raw_attributes["_id"] == other.raw_attributes["_id"]
39
39
  end
40
40
 
41
41
  # Performs class equality checking.
@@ -62,6 +62,31 @@ module Mongoid #:nodoc:
62
62
  self == (other)
63
63
  end
64
64
 
65
+ # Freezes the internal attributes of the document.
66
+ #
67
+ # @example Freeze the document
68
+ # document.freeze
69
+ #
70
+ # @return [ Document ] The document.
71
+ #
72
+ # @since 2.0.0
73
+ def freeze
74
+ raw_attributes.freeze
75
+ self
76
+ end
77
+
78
+ # Checks if the document is frozen
79
+ #
80
+ # @example Check if frozen
81
+ # document.frozen?
82
+ #
83
+ # @return [ true, false ] True if frozen, else false.
84
+ #
85
+ # @since 2.0.0
86
+ def frozen?
87
+ raw_attributes.frozen?
88
+ end
89
+
65
90
  # Delegates to id in order to allow two records of the same type and id to
66
91
  # work with something like:
67
92
  #
@@ -73,7 +98,7 @@ module Mongoid #:nodoc:
73
98
  #
74
99
  # @return [ Integer ] The hash of the document's id.
75
100
  def hash
76
- id.hash
101
+ raw_attributes["_id"].hash
77
102
  end
78
103
 
79
104
  # Return the attributes hash with indifferent access. Used mostly for
@@ -85,7 +110,7 @@ module Mongoid #:nodoc:
85
110
  #
86
111
  # @return [ HashWithIndifferentAccess ] The attributes.
87
112
  def attributes
88
- @attributes.with_indifferent_access
113
+ raw_attributes.with_indifferent_access
89
114
  end
90
115
 
91
116
  # Generate an id for this +Document+.
@@ -195,7 +220,7 @@ module Mongoid #:nodoc:
195
220
  #
196
221
  # @return [ Hash ] A hash of all attributes in the hierarchy.
197
222
  def as_document
198
- attributes = @attributes
223
+ attributes = raw_attributes
199
224
  attributes.tap do |attrs|
200
225
  relations.select { |name, meta| meta.embedded? }.each do |name, meta|
201
226
  relation = send(name, false, :continue => false)
@@ -258,27 +283,5 @@ module Mongoid #:nodoc:
258
283
  :mongoid
259
284
  end
260
285
  end
261
-
262
- # Freezes the internal attributes of the document.
263
- #
264
- # @example Freeze the document
265
- # document.freeze
266
- #
267
- # @return [ Document ] The document.
268
- def freeze
269
- raw_attributes.freeze
270
- self
271
- end
272
-
273
- # Checks if the document is frozen
274
- #
275
- # @example Check if frozen
276
- # document.frozen?
277
- #
278
- # @return [ true, false ] True if frozen, else false.
279
- def frozen?
280
- raw_attributes.frozen?
281
- end
282
-
283
286
  end
284
287
  end
@@ -62,21 +62,22 @@ module Mongoid #:nodoc:
62
62
  # @return [ BSON::ObjectId, Array, Hash ] The converted object ids.
63
63
  #
64
64
  # @since 2.0.0.rc.7
65
- def convert(klass, args)
65
+ def convert(klass, args, reject_blank = true)
66
66
  return args if args.is_a?(BSON::ObjectId) || !klass.using_object_ids?
67
67
  case args
68
68
  when ::String
69
69
  args.blank? ? nil : BSON::ObjectId.from_string(args)
70
70
  when ::Array
71
- args.reject(&:blank?).map do |arg|
72
- convert(klass, arg)
71
+ args = args.reject(&:blank?) if reject_blank
72
+ args.map do |arg|
73
+ convert(klass, arg, reject_blank)
73
74
  end
74
75
  when ::Hash
75
76
  args.tap do |hash|
76
77
  hash.each_pair do |key, value|
77
78
  next unless key.to_s =~ /id/
78
79
  begin
79
- hash[key] = convert(klass, value)
80
+ hash[key] = convert(klass, value, reject_blank)
80
81
  rescue BSON::InvalidObjectId; end
81
82
  end
82
83
  end
@@ -79,12 +79,12 @@ module Mongoid #:nodoc:
79
79
  module ClassMethods #:nodoc:
80
80
 
81
81
  # Override the default +Criteria+ accessor to only get existing
82
- # documents.
82
+ # documents. Passes all arguments up to +NamedScope.criteria+
83
83
  #
84
84
  # Returns:
85
85
  #
86
86
  # A +Criteria+ for deleted_at not existing.
87
- def criteria(embedded = false)
87
+ def criteria(*args)
88
88
  super.where(:deleted_at.exists => false)
89
89
  end
90
90
 
@@ -20,8 +20,10 @@ module Mongoid #:nodoc:
20
20
  values = document.send(field)
21
21
  values.push(value) unless values.include?(value)
22
22
  values.tap do
23
- document.collection.update(document._selector, operation("$addToSet"), options)
24
- document.changes.delete(field.to_s) if document.persisted?
23
+ if document.persisted?
24
+ document.collection.update(document._selector, operation("$addToSet"), options)
25
+ document.changes.delete(field.to_s)
26
+ end
25
27
  end
26
28
  end
27
29
  end
@@ -12,6 +12,7 @@ module Mongoid #:nodoc:
12
12
  :klass,
13
13
  :options,
14
14
  :selector,
15
+ :suppress,
15
16
  :validate
16
17
 
17
18
  # Initialize the persistence +Command+.
@@ -28,6 +29,7 @@ module Mongoid #:nodoc:
28
29
  def initialize(document_or_class, options = {}, selector = {})
29
30
  init(document_or_class)
30
31
  validate = options[:validate]
32
+ @suppress = options[:suppress]
31
33
  @validate = (validate.nil? ? true : validate)
32
34
  @selector = selector
33
35
  @options = { :safe => safe_mode?(options) }
@@ -54,6 +56,16 @@ module Mongoid #:nodoc:
54
56
  @collection = @klass.collection
55
57
  end
56
58
  end
59
+
60
+ # Should we suppress parent notifications?
61
+ #
62
+ # @example Suppress notifications?
63
+ # command.suppress?
64
+ #
65
+ # @return [ true, false ] Should the parent notifcations be suppressed.
66
+ def suppress?
67
+ !!@suppress
68
+ end
57
69
  end
58
70
  end
59
71
  end
@@ -33,7 +33,7 @@ module Mongoid #:nodoc:
33
33
  if document.embedded?
34
34
  Persistence::RemoveEmbedded.new(
35
35
  document,
36
- options.merge(:validate => validate)
36
+ options.merge(:validate => validate, :suppress => suppress)
37
37
  ).persist
38
38
  else
39
39
  collection.remove({ :_id => document.id }, options)
@@ -27,7 +27,7 @@ module Mongoid #:nodoc:
27
27
  # +true+ or +false+, depending on if the removal passed.
28
28
  def persist
29
29
  parent = document._parent
30
- parent.remove_child(document)
30
+ parent.remove_child(document) unless suppress?
31
31
  unless parent.new_record?
32
32
  update = { document._remover => removal_selector }
33
33
  collection.update(parent._selector, update, options.merge(:multi => false))
@@ -41,8 +41,6 @@ module Mongoid # :nodoc:
41
41
  #
42
42
  # @since 2.0.0.rc.1
43
43
  def bind_one(doc, options = {})
44
- keys = base.do_or_do_not(metadata.foreign_key)
45
- keys.push(doc.id) unless keys.include?(doc.id)
46
44
  if options[:continue]
47
45
  inverse = metadata.inverse(target)
48
46
  if inverse
@@ -403,7 +403,7 @@ module Mongoid # :nodoc:
403
403
  criteria.size.tap do
404
404
  criteria.each do |doc|
405
405
  target.delete(doc)
406
- doc.send(method)
406
+ doc.send(method, :suppress => true)
407
407
  end
408
408
  reindex
409
409
  end
@@ -25,15 +25,30 @@ module Mongoid # :nodoc:
25
25
  args.flatten.each do |doc|
26
26
  return doc unless doc
27
27
  append(doc, options)
28
- if base.persisted? && !options[:binding]
29
- doc.save
30
- base.add_to_set(metadata.foreign_key, doc.id)
31
- end
28
+ base.add_to_set(metadata.foreign_key, doc.id)
29
+ doc.save if base.persisted? && !options[:binding]
32
30
  end
33
31
  end
34
32
  alias :concat :<<
35
33
  alias :push :<<
36
34
 
35
+ # Builds a new document in the relation and appends it to the target.
36
+ # Takes an optional type if you want to specify a subclass.
37
+ #
38
+ # @example Build a new document on the relation.
39
+ # person.people.build(:name => "Bozo")
40
+ #
41
+ # @param [ Hash ] attributes The attributes to build the document with.
42
+ # @param [ Class ] type Optional class to build the document with.
43
+ #
44
+ # @return [ Document ] The new document.
45
+ def build(attributes = {}, type = nil, &block)
46
+ super.tap do |doc|
47
+ base.send(metadata.foreign_key).push(doc.id)
48
+ end
49
+ end
50
+ alias :new :build
51
+
37
52
  # Creates a new document on the references many relation. This will
38
53
  # save the document if the parent has been persisted.
39
54
  #
@@ -44,12 +59,10 @@ module Mongoid # :nodoc:
44
59
  # @param [ Class ] type The optional type of document to create.
45
60
  #
46
61
  # @return [ Document ] The newly created document.
47
- def create(attributes = nil, type = nil)
48
- build(attributes, type).tap do |doc|
49
- if base.persisted?
50
- doc.save
51
- base.add_to_set(metadata.foreign_key, doc.id)
52
- end
62
+ def create(attributes = nil, type = nil, &block)
63
+ build(attributes, type, &block).tap do |doc|
64
+ base.add_to_set(metadata.foreign_key, doc.id)
65
+ doc.save if base.persisted?
53
66
  end
54
67
  end
55
68
 
@@ -66,12 +79,10 @@ module Mongoid # :nodoc:
66
79
  # @raise [ Errors::Validations ] If validation failed.
67
80
  #
68
81
  # @return [ Document ] The newly created document.
69
- def create!(attributes = nil, type = nil)
70
- build(attributes, type).tap do |doc|
71
- if base.persisted?
72
- doc.save!
73
- base.add_to_set(metadata.foreign_key, doc.id)
74
- end
82
+ def create!(attributes = nil, type = nil, &block)
83
+ build(attributes, type, &block).tap do |doc|
84
+ base.add_to_set(metadata.foreign_key, doc.id)
85
+ doc.save! if base.persisted?
75
86
  end
76
87
  end
77
88
 
@@ -121,6 +132,23 @@ module Mongoid # :nodoc:
121
132
  remove_all(conditions, :destroy_all)
122
133
  end
123
134
 
135
+ # Instantiate a new references_many relation. Will set the foreign key
136
+ # and the base on the inverse object.
137
+ #
138
+ # @example Create the new relation.
139
+ # Referenced::ManyToMany.new(base, target, metadata)
140
+ #
141
+ # @param [ Document ] base The document this relation hangs off of.
142
+ # @param [ Array<Document> ] target The target of the relation.
143
+ # @param [ Metadata ] metadata The relation's metadata.
144
+ def initialize(base, target, metadata)
145
+ init(base, target, metadata) do
146
+ unless base.frozen?
147
+ base.send(metadata.foreign_key_setter, target.map(&:id))
148
+ end
149
+ end
150
+ end
151
+
124
152
  # Removes all associations between the base document and the target
125
153
  # documents by deleting the foreign keys and the references, orphaning
126
154
  # the target documents in the process.
@@ -160,6 +188,7 @@ module Mongoid # :nodoc:
160
188
  if new_target
161
189
  binding.unbind(options)
162
190
  relation.target = new_target.to_a
191
+ base.send(metadata.foreign_key_setter, new_target.map(&:id))
163
192
  bind(options)
164
193
  else
165
194
  relation.target = unbind(options)
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc
3
- VERSION = "2.0.0.rc.8"
3
+ VERSION = "2.0.0"
4
4
  end
metadata CHANGED
@@ -1,14 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: true
4
+ prerelease: false
5
5
  segments:
6
6
  - 2
7
7
  - 0
8
8
  - 0
9
- - rc
10
- - 8
11
- version: 2.0.0.rc.8
9
+ version: 2.0.0
12
10
  platform: ruby
13
11
  authors:
14
12
  - Durran Jordan
@@ -16,7 +14,7 @@ autorequire:
16
14
  bindir: bin
17
15
  cert_chain: []
18
16
 
19
- date: 2011-03-23 00:00:00 +01:00
17
+ date: 2011-03-30 00:00:00 +02:00
20
18
  default_executable:
21
19
  dependencies:
22
20
  - !ruby/object:Gem::Dependency
@@ -381,7 +379,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
381
379
  requirements:
382
380
  - - ">="
383
381
  - !ruby/object:Gem::Version
384
- hash: 2466233120358762618
382
+ hash: 2548695650621036943
385
383
  segments:
386
384
  - 0
387
385
  version: "0"