mongoid 2.0.0.rc.3 → 2.0.0.rc.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
 
3
- # Copyright (c) 2009, 2010 Durran Jordan
3
+ # Copyright (c) 2009 - 2011 Durran Jordan and friends.
4
4
  #
5
5
  # Permission is hereby granted, free of charge, to any person obtaining
6
6
  # a copy of this software and associated documentation files (the
@@ -52,7 +52,6 @@ require "mongoid/contexts"
52
52
  require "mongoid/copyable"
53
53
  require "mongoid/criteria"
54
54
  require "mongoid/cursor"
55
- require "mongoid/deprecation"
56
55
  require "mongoid/default_scope"
57
56
  require "mongoid/dirty"
58
57
  require "mongoid/extras"
@@ -122,17 +121,6 @@ module Mongoid #:nodoc
122
121
  block_given? ? yield(config) : config
123
122
  end
124
123
  alias :config :configure
125
-
126
- # Easy convenience method for generating an alert from the
127
- # deprecation module.
128
- #
129
- # @example Alert a deprecation.
130
- # Mongoid.deprecate("Method no longer used")
131
- #
132
- # @param [ String ] message The message to print.
133
- def deprecate(message)
134
- Mongoid::Deprecation.alert(message)
135
- end
136
124
  end
137
125
 
138
126
  # Take all the public instance methods from the Config singleton and allow
@@ -218,7 +218,7 @@ module Mongoid #:nodoc:
218
218
  if value.is_a?(Hash)
219
219
  metadata.nested_builder(value, {}).build(self)
220
220
  else
221
- send("#{name}=", value)
221
+ send("#{name}=", value, :building => true)
222
222
  end
223
223
  end
224
224
  end
@@ -303,6 +303,26 @@ module Mongoid #:nodoc:
303
303
  options.dup
304
304
  end
305
305
 
306
+ # Very basic update that will perform a simple atomic $set of the
307
+ # attributes provided in the hash. Can be expanded to later for more
308
+ # robust functionality.
309
+ #
310
+ # @example Update all matching documents.
311
+ # context.update_all(:title => "Sir")
312
+ #
313
+ # @param [ Hash ] attributes The sets to perform.
314
+ #
315
+ # @since 2.0.0.rc.4
316
+ def update_all(attributes = {})
317
+ klass.collection.update(
318
+ selector,
319
+ { "$set" => attributes },
320
+ :multi => true,
321
+ :safe => Mongoid.persist_in_safe_mode
322
+ )
323
+ end
324
+ alias :update :update_all
325
+
306
326
  protected
307
327
 
308
328
  # Iterate over each +Document+ in the results and cache the collection.
@@ -1,7 +1,6 @@
1
1
  # encoding: utf-8
2
2
  require "mongoid/criterion/creational"
3
3
  require "mongoid/criterion/complex"
4
- require "mongoid/criterion/destructive"
5
4
  require "mongoid/criterion/exclusion"
6
5
  require "mongoid/criterion/inclusion"
7
6
  require "mongoid/criterion/inspection"
@@ -27,7 +26,6 @@ module Mongoid #:nodoc:
27
26
  class Criteria
28
27
  include Enumerable
29
28
  include Criterion::Creational
30
- include Criterion::Destructive
31
29
  include Criterion::Exclusion
32
30
  include Criterion::Inclusion
33
31
  include Criterion::Inspection
@@ -35,9 +33,32 @@ module Mongoid #:nodoc:
35
33
 
36
34
  attr_accessor :collection, :documents, :embedded, :ids, :klass, :options, :selector
37
35
 
38
- delegate :aggregate, :avg, :blank?, :count, :distinct, :empty?,
39
- :execute, :first, :group, :id_criteria, :last, :max,
40
- :min, :one, :page, :paginate, :per_page, :shift, :sum, :to => :context
36
+ delegate \
37
+ :aggregate,
38
+ :avg,
39
+ :blank?,
40
+ :count,
41
+ :delete,
42
+ :delete_all,
43
+ :destroy,
44
+ :destroy_all,
45
+ :distinct,
46
+ :empty?,
47
+ :execute,
48
+ :first,
49
+ :group,
50
+ :id_criteria,
51
+ :last,
52
+ :max,
53
+ :min,
54
+ :one,
55
+ :page,
56
+ :paginate,
57
+ :per_page,
58
+ :shift,
59
+ :sum,
60
+ :update,
61
+ :update_all, :to => :context
41
62
 
42
63
  # Concatinate the criteria with another enumerable. If the other is a
43
64
  # +Criteria+ then it needs to get the collection from it.
@@ -204,6 +204,20 @@ module Mongoid #:nodoc:
204
204
 
205
205
  module ClassMethods #:nodoc:
206
206
 
207
+ # Performs class equality checking.
208
+ #
209
+ # @example Compare the classes.
210
+ # document === other
211
+ #
212
+ # @param [ Document, Object ] other The other object to compare with.
213
+ #
214
+ # @return [ true, false ] True if the classes are equal, false if not.
215
+ #
216
+ # @since 2.0.0.rc.4
217
+ def ===(other)
218
+ self == (other.is_a?(Class) ? other : other.class)
219
+ end
220
+
207
221
  # Instantiate a new object, only when loaded from the database or when
208
222
  # the attributes have already been typecast.
209
223
  #
@@ -11,7 +11,7 @@ module Mongoid #:nodoc:
11
11
  ::Set.new(value)
12
12
  end
13
13
  def set(value)
14
- value
14
+ value.to_a
15
15
  end
16
16
  end
17
17
  end
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc:
3
3
  class Field
4
- attr_reader :copyable, :klass, :name, :options, :type
4
+ attr_reader :copyable, :klass, :label, :name, :options, :type
5
5
 
6
6
  # Get the default value for the field.
7
7
  #
@@ -28,6 +28,7 @@ module Mongoid #:nodoc:
28
28
  @type = options[:type] || Object
29
29
  @name, @default = name, options[:default]
30
30
  @copyable = (@default.is_a?(Array) || @default.is_a?(Hash))
31
+ @label = options[:label]
31
32
  @options = options
32
33
  check_default!
33
34
  end
@@ -95,7 +95,7 @@ module Mongoid #:nodoc:
95
95
  #
96
96
  # @return [ true, false ] True if validation passed, false if not.
97
97
  def update_attributes(attributes = {})
98
- write_attributes(attributes); update
98
+ write_attributes(attributes); save
99
99
  end
100
100
 
101
101
  # Update the document attributes in the database and raise an error if
@@ -106,10 +106,11 @@ module Mongoid #:nodoc:
106
106
  #
107
107
  # @param [ Hash ] attributes The attributes to update.
108
108
  #
109
+ # @raise [ Errors::Validations ] If validation failed.
110
+ #
109
111
  # @return [ true, false ] True if validation passed.
110
112
  def update_attributes!(attributes = {})
111
- write_attributes(attributes)
112
- update.tap do |result|
113
+ update_attributes(attributes).tap do |result|
113
114
  self.class.fail_validate!(self) unless result
114
115
  end
115
116
  end
@@ -10,7 +10,20 @@ module Rails #:nodoc:
10
10
  module Mongoid #:nodoc:
11
11
  class Railtie < Rails::Railtie #:nodoc:
12
12
 
13
- config.app_generators.orm :mongoid, :migration => false
13
+ # Determine which generator to use. app_generators was introduced after
14
+ # 3.0.0.
15
+ #
16
+ # @example Get the generators method.
17
+ # railtie.generators
18
+ #
19
+ # @return [ Symbol ] The method name to use.
20
+ #
21
+ # @since 2.0.0.rc.4
22
+ def self.generator
23
+ config.respond_to?(:app_generators) ? :app_generators : :generators
24
+ end
25
+
26
+ config.send(generator).orm :mongoid, :migration => false
14
27
 
15
28
  rake_tasks do
16
29
  load "mongoid/railties/database.rake"
@@ -18,8 +31,7 @@ module Rails #:nodoc:
18
31
 
19
32
  # Exposes Mongoid's configuration to the Rails application configuration.
20
33
  #
21
- # Example:
22
- #
34
+ # @example Set up configuration in the Rails app.
23
35
  # module MyApplication
24
36
  # class Application < Rails::Application
25
37
  # config.mongoid.logger = Logger.new($stdout, :warn)
@@ -31,7 +43,7 @@ module Rails #:nodoc:
31
43
  # Initialize Mongoid. This will look for a mongoid.yml in the config
32
44
  # directory and configure mongoid appropriately.
33
45
  #
34
- # Example mongoid.yml:
46
+ # @example mongoid.yml
35
47
  #
36
48
  # defaults: &defaults
37
49
  # host: localhost
@@ -93,6 +105,8 @@ module Rails #:nodoc:
93
105
  end
94
106
  end
95
107
 
108
+ # When workers are forked in passenger and unicorn, we need to reconnect
109
+ # to the database to all the workers do not share the same connection.
96
110
  initializer "reconnect to master if application is preloaded" do
97
111
  config.after_initialize do
98
112
 
@@ -33,6 +33,7 @@ module Mongoid # :nodoc:
33
33
  #
34
34
  # @since 2.0.0.rc.1
35
35
  def query?
36
+ return true unless object.respond_to?(:to_a)
36
37
  obj = object.to_a.first
37
38
  !obj.respond_to?(:attributes) && !obj.nil?
38
39
  end
@@ -42,6 +42,7 @@ module Mongoid # :nodoc:
42
42
  # @return [ In ] The proxy.
43
43
  def initialize(base, target, metadata)
44
44
  init(base, target, metadata) do
45
+ characterize_one(target)
45
46
  base.parentize(target)
46
47
  end
47
48
  end
@@ -174,6 +174,7 @@ module Mongoid # :nodoc:
174
174
  def initialize(base, target, metadata)
175
175
  init(base, target, metadata) do
176
176
  target.each_with_index do |doc, index|
177
+ characterize_one(doc)
177
178
  doc.parentize(base)
178
179
  doc._index = index
179
180
  end
@@ -275,7 +276,7 @@ module Mongoid # :nodoc:
275
276
  # @since 2.0.0.rc.1
276
277
  def append(document, options = {})
277
278
  target << document
278
- metadatafy(document)
279
+ characterize_one(document)
279
280
  bind_one(document, options)
280
281
  document._index = target.size - 1
281
282
  end
@@ -39,6 +39,7 @@ module Mongoid # :nodoc:
39
39
  # @param [ Metadata ] metadata The relation's metadata
40
40
  def initialize(base, target, metadata)
41
41
  init(base, target, metadata) do
42
+ characterize_one(target)
42
43
  target.parentize(base)
43
44
  end
44
45
  end
@@ -50,7 +50,7 @@ module Mongoid # :nodoc:
50
50
  # @param [ Hash ] options The relation options.
51
51
  # @param [ Proc ] block Optional block for defining extensions.
52
52
  def embedded_in(name, options = {}, &block)
53
- metadatafy(name, Embedded::In, options, &block).tap do |meta|
53
+ characterize(name, Embedded::In, options, &block).tap do |meta|
54
54
  self.embedded = true
55
55
  relate(name, meta)
56
56
  end
@@ -76,7 +76,7 @@ module Mongoid # :nodoc:
76
76
  # @param [ Hash ] options The relation options.
77
77
  # @param [ Proc ] block Optional block for defining extensions.
78
78
  def embeds_many(name, options = {}, &block)
79
- metadatafy(name, Embedded::Many, options, &block).tap do |meta|
79
+ characterize(name, Embedded::Many, options, &block).tap do |meta|
80
80
  relate(name, meta)
81
81
  validate_relation(meta)
82
82
  end
@@ -102,7 +102,7 @@ module Mongoid # :nodoc:
102
102
  # @param [ Hash ] options The relation options.
103
103
  # @param [ Proc ] block Optional block for defining extensions.
104
104
  def embeds_one(name, options = {}, &block)
105
- metadatafy(name, Embedded::One, options, &block).tap do |meta|
105
+ characterize(name, Embedded::One, options, &block).tap do |meta|
106
106
  relate(name, meta)
107
107
  builder(name).creator(name)
108
108
  validate_relation(meta)
@@ -128,7 +128,7 @@ module Mongoid # :nodoc:
128
128
  # @param [ Hash ] options The relation options.
129
129
  # @param [ Proc ] block Optional block for defining extensions.
130
130
  def referenced_in(name, options = {}, &block)
131
- metadatafy(name, Referenced::In, options, &block).tap do |meta|
131
+ characterize(name, Referenced::In, options, &block).tap do |meta|
132
132
  relate(name, meta)
133
133
  reference(meta)
134
134
  end
@@ -156,7 +156,7 @@ module Mongoid # :nodoc:
156
156
  # @param [ Proc ] block Optional block for defining extensions.
157
157
  def references_many(name, options = {}, &block)
158
158
  check_options(options)
159
- metadatafy(name, Referenced::Many, options, &block).tap do |meta|
159
+ characterize(name, Referenced::Many, options, &block).tap do |meta|
160
160
  relate(name, meta)
161
161
  reference(meta)
162
162
  autosave(meta)
@@ -187,7 +187,7 @@ module Mongoid # :nodoc:
187
187
  #
188
188
  # @since 2.0.0.rc.1
189
189
  def references_and_referenced_in_many(name, options = {}, &block)
190
- metadatafy(name, Referenced::ManyToMany, options, &block).tap do |meta|
190
+ characterize(name, Referenced::ManyToMany, options, &block).tap do |meta|
191
191
  relate(name, meta)
192
192
  reference(meta)
193
193
  end
@@ -213,7 +213,7 @@ module Mongoid # :nodoc:
213
213
  # @param [ Hash ] options The relation options.
214
214
  # @param [ Proc ] block Optional block for defining extensions.
215
215
  def references_one(name, options = {}, &block)
216
- metadatafy(name, Referenced::One, options, &block).tap do |meta|
216
+ characterize(name, Referenced::One, options, &block).tap do |meta|
217
217
  relate(name, meta)
218
218
  reference(meta)
219
219
  builder(name).creator(name).autosave(meta)
@@ -248,7 +248,7 @@ module Mongoid # :nodoc:
248
248
  # Create the metadata for the relation.
249
249
  #
250
250
  # @example Create the metadata.
251
- # Person.metadatafy(:posts, Referenced::Many, {})
251
+ # Person.characterize(:posts, Referenced::Many, {})
252
252
  #
253
253
  # @param [ Symbol ] name The name of the relation.
254
254
  # @param [ Object ] relation The type of relation.
@@ -256,7 +256,7 @@ module Mongoid # :nodoc:
256
256
  # @param [ Proc ] block Optional block for defining extensions.
257
257
  #
258
258
  # @return [ Metadata ] The metadata for the relation.
259
- def metadatafy(name, relation, options, &block)
259
+ def characterize(name, relation, options, &block)
260
260
  Metadata.new(
261
261
  options.merge(
262
262
  :relation => relation,
@@ -32,7 +32,6 @@ module Mongoid # :nodoc:
32
32
  # @since 2.0.0.rc.1
33
33
  def init(base, target, metadata, &block)
34
34
  @base, @building, @target, @metadata = base, false, target, metadata
35
- metadatafy(target)
36
35
  yield block if block_given?
37
36
  extend Module.new(&metadata.extension) if metadata.extension?
38
37
  end
@@ -93,20 +92,29 @@ module Mongoid # :nodoc:
93
92
  !target.is_a?(Mongoid::Criteria)
94
93
  end
95
94
 
96
- # Takes the supplied document and sets the metadata on it. Used when
95
+ # Takes the supplied documents and sets the metadata on them. Used when
97
96
  # creating new documents and adding them to the relation.
98
97
  #
99
98
  # @example Set the metadata.
100
- # proxy.metadatafy(address)
99
+ # proxy.characterize(addresses)
101
100
  #
102
- # @param [ Document, Array<Document> ] object The object to set the
103
- # metadata on.
101
+ # @param [ Array<Document> ] documents The documents to set metadata on.
104
102
  #
105
- # @since 2.0.0.rc.1
106
- def metadatafy(object)
107
- object.to_a.each do |obj|
108
- obj.metadata = metadata unless obj.metadata
109
- end
103
+ # @since 2.0.0.rc.4
104
+ def characterize(documents)
105
+ documents.each { |doc| characterize_one(doc) }
106
+ end
107
+
108
+ # Takes the supplied document and sets the metadata on it.
109
+ #
110
+ # @example Set the metadata.
111
+ # proxt.characterize_one(name)
112
+ #
113
+ # @param [ Document ] document The document to set on.
114
+ #
115
+ # @since 2.0.0.rc.4
116
+ def characterize_one(document)
117
+ document.metadata = metadata unless document.metadata
110
118
  end
111
119
 
112
120
  # Default behavior of method missing should be to delegate all calls
@@ -40,7 +40,9 @@ module Mongoid # :nodoc:
40
40
  # relation.
41
41
  # @param [ Metadata ] metadata The relation's metadata.
42
42
  def initialize(base, target, metadata)
43
- init(base, target, metadata)
43
+ init(base, target, metadata) do
44
+ characterize_one(target)
45
+ end
44
46
  end
45
47
 
46
48
  # Substitutes the supplied target documents for the existing document
@@ -25,7 +25,7 @@ module Mongoid #:nodoc:
25
25
  #
26
26
  # @since 2.0.0.rc.1
27
27
  def bind(options = {})
28
- binding.bind(options)
28
+ loaded and binding.bind(options)
29
29
  target.map(&:save) if base.persisted? && !options[:building]
30
30
  end
31
31
 
@@ -202,7 +202,7 @@ module Mongoid #:nodoc:
202
202
  # @since 2.0.0.rc.1
203
203
  def append(document, options = {})
204
204
  loaded and target.push(document)
205
- metadatafy(document)
205
+ characterize_one(document)
206
206
  binding.bind_one(document, options)
207
207
  end
208
208
 
@@ -242,7 +242,7 @@ module Mongoid #:nodoc:
242
242
  # @since 2.0.0.rc.1
243
243
  def loaded
244
244
  tap do |relation|
245
- relation.target = target.entries if target.is_a?(Mongoid::Criteria)
245
+ relation.target = target.entries unless loaded?
246
246
  end
247
247
  end
248
248
 
@@ -25,7 +25,7 @@ module Mongoid # :nodoc:
25
25
  #
26
26
  # @since 2.0.0.rc.1
27
27
  def bind(options = {})
28
- binding.bind(options)
28
+ loaded and binding.bind(options)
29
29
  target.map(&:save) if base.persisted? && !options[:building]
30
30
  end
31
31
 
@@ -173,8 +173,16 @@ module Mongoid # :nodoc:
173
173
  # @return [ Many ] The relation.
174
174
  #
175
175
  # @since 2.0.0.rc.1
176
- def substitute(target, options = {})
177
- tap { target ? (@target = target.to_a; bind(options)) : (@target = unbind(options)) }
176
+ def substitute(new_target, options = {})
177
+ tap do |relation|
178
+ if new_target
179
+ binding.unbind(options)
180
+ relation.target = new_target.to_a
181
+ bind(options)
182
+ else
183
+ relation.target = unbind(options)
184
+ end
185
+ end
178
186
  end
179
187
 
180
188
  # Unbinds the base object to the inverse of the relation. This occurs
@@ -211,7 +219,7 @@ module Mongoid # :nodoc:
211
219
  # @since 2.0.0.rc.1
212
220
  def append(document, options = {})
213
221
  loaded and target.push(document)
214
- metadatafy(document)
222
+ characterize_one(document)
215
223
  binding.bind_one(document, options)
216
224
  end
217
225
 
@@ -39,7 +39,9 @@ module Mongoid # :nodoc:
39
39
  # @param [ Document ] target The target (child) of the relation.
40
40
  # @param [ Metadata ] metadata The relation's metadata.
41
41
  def initialize(base, target, metadata)
42
- init(base, target, metadata)
42
+ init(base, target, metadata) do
43
+ characterize_one(target)
44
+ end
43
45
  end
44
46
 
45
47
  # Removes the association between the base document and the target
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc
3
- VERSION = "2.0.0.rc.3"
3
+ VERSION = "2.0.0.rc.4"
4
4
  end
metadata CHANGED
@@ -7,8 +7,8 @@ version: !ruby/object:Gem::Version
7
7
  - 0
8
8
  - 0
9
9
  - rc
10
- - 3
11
- version: 2.0.0.rc.3
10
+ - 4
11
+ version: 2.0.0.rc.4
12
12
  platform: ruby
13
13
  authors:
14
14
  - Durran Jordan
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-01-10 00:00:00 +01:00
19
+ date: 2011-01-13 00:00:00 +01:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -183,7 +183,6 @@ files:
183
183
  - lib/mongoid/criteria.rb
184
184
  - lib/mongoid/criterion/complex.rb
185
185
  - lib/mongoid/criterion/creational.rb
186
- - lib/mongoid/criterion/destructive.rb
187
186
  - lib/mongoid/criterion/exclusion.rb
188
187
  - lib/mongoid/criterion/inclusion.rb
189
188
  - lib/mongoid/criterion/inspection.rb
@@ -191,7 +190,6 @@ files:
191
190
  - lib/mongoid/criterion/selector.rb
192
191
  - lib/mongoid/cursor.rb
193
192
  - lib/mongoid/default_scope.rb
194
- - lib/mongoid/deprecation.rb
195
193
  - lib/mongoid/dirty.rb
196
194
  - lib/mongoid/document.rb
197
195
  - lib/mongoid/errors/document_not_found.rb
@@ -356,7 +354,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
356
354
  requirements:
357
355
  - - ">="
358
356
  - !ruby/object:Gem::Version
359
- hash: -1678984237143289701
357
+ hash: 588977190582466333
360
358
  segments:
361
359
  - 0
362
360
  version: "0"
@@ -1,37 +0,0 @@
1
- # encoding: utf-8
2
- module Mongoid #:nodoc:
3
- module Criterion #:nodoc:
4
-
5
- # This module defines criteria behavior for deleting or destroying
6
- # documents.
7
- module Destructive
8
-
9
- # Delete all documents in the database that match the criteria.
10
- #
11
- # @example Delete all matching documents.
12
- # Person.where(:title => "Sir").and(:age.gt => 5).delete_all
13
- #
14
- # @return [ Integer ] The number of documents deleted.
15
- #
16
- # @since 2.0.0.rc.1
17
- def delete_all
18
- context.delete_all
19
- end
20
- alias :delete :delete_all
21
-
22
- # Destroy all documents in the database that match the criteria. Will run
23
- # the destruction callbacks on each document as well.
24
- #
25
- # @example Destroy all matching documents.
26
- # Person.where(:title => "Sir").and(:age.gt => 5).destroy_all
27
- #
28
- # @return [ Integer ] The number of documents destroyed.
29
- #
30
- # @since 2.0.0.rc.1
31
- def destroy_all
32
- context.destroy_all
33
- end
34
- alias :destroy :destroy_all
35
- end
36
- end
37
- end
@@ -1,21 +0,0 @@
1
- # encoding: utf-8
2
- module Mongoid #:nodoc:
3
- module Deprecation #:nodoc
4
- extend self
5
-
6
- # Alert of a deprecation. This will delegate to the logger and call warn on
7
- # it.
8
- #
9
- # Example:
10
- #
11
- # <tt>deprecation.alert("Method no longer used")</tt>
12
- def alert(message)
13
- logger.warn("Deprecation: #{message}")
14
- end
15
-
16
- protected
17
- def logger
18
- @logger ||= Mongoid::Logger.new
19
- end
20
- end
21
- end