mongoid 2.0.0.beta.20 → 2.0.0.rc.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (134) hide show
  1. data/README.rdoc +8 -0
  2. data/Rakefile +51 -0
  3. data/lib/config/locales/nl.yml +39 -0
  4. data/lib/config/locales/ro.yml +1 -1
  5. data/lib/mongoid.rb +17 -17
  6. data/lib/mongoid/atomicity.rb +54 -22
  7. data/lib/mongoid/attributes.rb +145 -125
  8. data/lib/mongoid/callbacks.rb +7 -2
  9. data/lib/mongoid/collection.rb +49 -32
  10. data/lib/mongoid/collections.rb +0 -1
  11. data/lib/mongoid/components.rb +34 -29
  12. data/lib/mongoid/config.rb +207 -193
  13. data/lib/mongoid/config/database.rb +167 -0
  14. data/lib/mongoid/contexts.rb +2 -5
  15. data/lib/mongoid/contexts/enumerable.rb +30 -4
  16. data/lib/mongoid/contexts/ids.rb +2 -2
  17. data/lib/mongoid/contexts/mongo.rb +30 -5
  18. data/lib/mongoid/copyable.rb +44 -0
  19. data/lib/mongoid/criteria.rb +110 -56
  20. data/lib/mongoid/criterion/creational.rb +34 -0
  21. data/lib/mongoid/criterion/destructive.rb +37 -0
  22. data/lib/mongoid/criterion/exclusion.rb +3 -1
  23. data/lib/mongoid/criterion/inclusion.rb +59 -64
  24. data/lib/mongoid/criterion/inspection.rb +22 -0
  25. data/lib/mongoid/criterion/optional.rb +42 -54
  26. data/lib/mongoid/criterion/selector.rb +9 -0
  27. data/lib/mongoid/default_scope.rb +28 -0
  28. data/lib/mongoid/deprecation.rb +5 -5
  29. data/lib/mongoid/dirty.rb +4 -5
  30. data/lib/mongoid/document.rb +161 -114
  31. data/lib/mongoid/extensions.rb +7 -11
  32. data/lib/mongoid/extensions/array/parentization.rb +2 -2
  33. data/lib/mongoid/extensions/date/conversions.rb +1 -1
  34. data/lib/mongoid/extensions/hash/conversions.rb +0 -23
  35. data/lib/mongoid/extensions/nil/collectionization.rb +12 -0
  36. data/lib/mongoid/extensions/object/reflections.rb +17 -0
  37. data/lib/mongoid/extensions/object/yoda.rb +27 -0
  38. data/lib/mongoid/extensions/string/conversions.rb +23 -4
  39. data/lib/mongoid/extensions/time_conversions.rb +4 -4
  40. data/lib/mongoid/field.rb +30 -19
  41. data/lib/mongoid/fields.rb +15 -5
  42. data/lib/mongoid/finders.rb +19 -11
  43. data/lib/mongoid/hierarchy.rb +34 -28
  44. data/lib/mongoid/identity.rb +62 -20
  45. data/lib/mongoid/inspection.rb +58 -0
  46. data/lib/mongoid/matchers.rb +20 -0
  47. data/lib/mongoid/multi_database.rb +11 -0
  48. data/lib/mongoid/nested_attributes.rb +41 -0
  49. data/lib/mongoid/paranoia.rb +3 -4
  50. data/lib/mongoid/paths.rb +1 -1
  51. data/lib/mongoid/persistence.rb +89 -90
  52. data/lib/mongoid/persistence/command.rb +20 -4
  53. data/lib/mongoid/persistence/insert.rb +13 -11
  54. data/lib/mongoid/persistence/insert_embedded.rb +8 -6
  55. data/lib/mongoid/persistence/remove.rb +6 -4
  56. data/lib/mongoid/persistence/remove_all.rb +6 -4
  57. data/lib/mongoid/persistence/remove_embedded.rb +8 -6
  58. data/lib/mongoid/persistence/update.rb +12 -10
  59. data/lib/mongoid/railtie.rb +2 -2
  60. data/lib/mongoid/railties/database.rake +10 -9
  61. data/lib/mongoid/relations.rb +104 -0
  62. data/lib/mongoid/relations/accessors.rb +154 -0
  63. data/lib/mongoid/relations/auto_save.rb +34 -0
  64. data/lib/mongoid/relations/binding.rb +24 -0
  65. data/lib/mongoid/relations/bindings.rb +9 -0
  66. data/lib/mongoid/relations/bindings/embedded/in.rb +77 -0
  67. data/lib/mongoid/relations/bindings/embedded/many.rb +93 -0
  68. data/lib/mongoid/relations/bindings/embedded/one.rb +65 -0
  69. data/lib/mongoid/relations/bindings/referenced/in.rb +78 -0
  70. data/lib/mongoid/relations/bindings/referenced/many.rb +93 -0
  71. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +94 -0
  72. data/lib/mongoid/relations/bindings/referenced/one.rb +63 -0
  73. data/lib/mongoid/relations/builder.rb +41 -0
  74. data/lib/mongoid/relations/builders.rb +79 -0
  75. data/lib/mongoid/relations/builders/embedded/in.rb +25 -0
  76. data/lib/mongoid/relations/builders/embedded/many.rb +32 -0
  77. data/lib/mongoid/relations/builders/embedded/one.rb +26 -0
  78. data/lib/mongoid/relations/builders/nested_attributes/many.rb +116 -0
  79. data/lib/mongoid/relations/builders/nested_attributes/one.rb +135 -0
  80. data/lib/mongoid/relations/builders/referenced/in.rb +32 -0
  81. data/lib/mongoid/relations/builders/referenced/many.rb +26 -0
  82. data/lib/mongoid/relations/builders/referenced/many_to_many.rb +29 -0
  83. data/lib/mongoid/relations/builders/referenced/one.rb +30 -0
  84. data/lib/mongoid/relations/cascading.rb +55 -0
  85. data/lib/mongoid/relations/cascading/delete.rb +19 -0
  86. data/lib/mongoid/relations/cascading/destroy.rb +19 -0
  87. data/lib/mongoid/relations/cascading/nullify.rb +18 -0
  88. data/lib/mongoid/relations/cascading/strategy.rb +26 -0
  89. data/lib/mongoid/relations/cyclic.rb +97 -0
  90. data/lib/mongoid/relations/embedded/in.rb +172 -0
  91. data/lib/mongoid/relations/embedded/many.rb +450 -0
  92. data/lib/mongoid/relations/embedded/one.rb +169 -0
  93. data/lib/mongoid/relations/macros.rb +302 -0
  94. data/lib/mongoid/relations/many.rb +185 -0
  95. data/lib/mongoid/relations/metadata.rb +529 -0
  96. data/lib/mongoid/relations/nested_builder.rb +52 -0
  97. data/lib/mongoid/relations/one.rb +29 -0
  98. data/lib/mongoid/relations/polymorphic.rb +54 -0
  99. data/lib/mongoid/relations/proxy.rb +122 -0
  100. data/lib/mongoid/relations/referenced/in.rb +214 -0
  101. data/lib/mongoid/relations/referenced/many.rb +358 -0
  102. data/lib/mongoid/relations/referenced/many_to_many.rb +379 -0
  103. data/lib/mongoid/relations/referenced/one.rb +204 -0
  104. data/lib/mongoid/relations/reflections.rb +45 -0
  105. data/lib/mongoid/safe.rb +11 -1
  106. data/lib/mongoid/safety.rb +122 -97
  107. data/lib/mongoid/scope.rb +14 -9
  108. data/lib/mongoid/state.rb +37 -3
  109. data/lib/mongoid/timestamps.rb +11 -0
  110. data/lib/mongoid/validations.rb +42 -3
  111. data/lib/mongoid/validations/associated.rb +8 -5
  112. data/lib/mongoid/validations/uniqueness.rb +23 -2
  113. data/lib/mongoid/version.rb +1 -1
  114. data/lib/mongoid/versioning.rb +25 -16
  115. data/lib/rails/generators/mongoid/model/templates/model.rb +3 -1
  116. metadata +95 -80
  117. data/lib/mongoid/associations.rb +0 -364
  118. data/lib/mongoid/associations/embedded_in.rb +0 -74
  119. data/lib/mongoid/associations/embeds_many.rb +0 -299
  120. data/lib/mongoid/associations/embeds_one.rb +0 -111
  121. data/lib/mongoid/associations/foreign_key.rb +0 -35
  122. data/lib/mongoid/associations/meta_data.rb +0 -38
  123. data/lib/mongoid/associations/options.rb +0 -78
  124. data/lib/mongoid/associations/proxy.rb +0 -60
  125. data/lib/mongoid/associations/referenced_in.rb +0 -70
  126. data/lib/mongoid/associations/references_many.rb +0 -254
  127. data/lib/mongoid/associations/references_many_as_array.rb +0 -128
  128. data/lib/mongoid/associations/references_one.rb +0 -104
  129. data/lib/mongoid/extensions/array/accessors.rb +0 -17
  130. data/lib/mongoid/extensions/array/assimilation.rb +0 -26
  131. data/lib/mongoid/extensions/hash/accessors.rb +0 -42
  132. data/lib/mongoid/extensions/hash/assimilation.rb +0 -40
  133. data/lib/mongoid/extensions/nil/assimilation.rb +0 -17
  134. data/lib/mongoid/memoization.rb +0 -33
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Criterion #:nodoc:
4
+
5
+ # This module defines criteria behavior for creating documents in the
6
+ # database for specified conditions.
7
+ module Creational
8
+
9
+ # Create a document in the database given the selector and return it.
10
+ # Complex criteria, such as $in and $or operations will get ignored.
11
+ #
12
+ # @example Create the document.
13
+ # Person.where(:title => "Sir").create
14
+ #
15
+ # @example Create with selectors getting ignored.
16
+ # Person.where(:age.gt => 5).create
17
+ #
18
+ # @return [ Document ] A newly created document.
19
+ #
20
+ # @since 2.0.0.rc.1
21
+ def create
22
+ klass.create(
23
+ selector.inject({}) do |hash, (key, value)|
24
+ hash.tap do |attrs|
25
+ unless key.to_s =~ /\$/ || value.is_a?(Hash)
26
+ attrs[key] = value
27
+ end
28
+ end
29
+ end
30
+ )
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,37 @@
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
@@ -58,7 +58,9 @@ module Mongoid #:nodoc:
58
58
  #
59
59
  # Returns: <tt>self</tt>
60
60
  def only(*args)
61
- @options[:fields] = args.flatten if args.any?; self
61
+ clone.tap do |crit|
62
+ crit.options[:fields] = args.flatten if args.any?
63
+ end
62
64
  end
63
65
  end
64
66
  end
@@ -2,23 +2,19 @@
2
2
  module Mongoid #:nodoc:
3
3
  module Criterion #:nodoc:
4
4
  module Inclusion
5
+
5
6
  # Adds a criterion to the +Criteria+ that specifies values that must all
6
7
  # be matched in order to return results. Similar to an "in" clause but the
7
8
  # underlying conditional logic is an "AND" and not an "OR". The MongoDB
8
9
  # conditional operator that will be used is "$all".
9
10
  #
10
- # Options:
11
- #
12
- # attributes: A +Hash+ where the key is the field name and the value is an
13
- # +Array+ of values that must all match.
14
- #
15
- # Example:
16
- #
17
- # <tt>criteria.all(:field => ["value1", "value2"])</tt>
11
+ # @example Adding the criterion.
12
+ # criteria.all(:field => ["value1", "value2"])
13
+ # criteria.all(:field1 => ["value1", "value2"], :field2 => ["value1"])
18
14
  #
19
- # <tt>criteria.all(:field1 => ["value1", "value2"], :field2 => ["value1"])</tt>
15
+ # @param [ Hash ] attributes Name/value pairs that all must match.
20
16
  #
21
- # Returns: <tt>self</tt>
17
+ # @return [ Criteria ] A new criteria with the added selector.
22
18
  def all(attributes = {})
23
19
  update_selector(attributes, "$all")
24
20
  end
@@ -30,15 +26,12 @@ module Mongoid #:nodoc:
30
26
  # similar to the Javascript object that is used when performing a find()
31
27
  # in the MongoDB console.
32
28
  #
33
- # Options:
29
+ # @example Adding the criterion.
30
+ # criteria.and(:field1 => "value1", :field2 => 15)
34
31
  #
35
- # selectior: A +Hash+ that must match the attributes of the +Document+.
32
+ # @param [ Hash ] selectior Name/value pairs that all must match.
36
33
  #
37
- # Example:
38
- #
39
- # <tt>criteria.and(:field1 => "value1", :field2 => 15)</tt>
40
- #
41
- # Returns: <tt>self</tt>
34
+ # @return [ Criteria ] A new criteria with the added selector.
42
35
  def and(selector = nil)
43
36
  where(selector)
44
37
  end
@@ -48,39 +41,48 @@ module Mongoid #:nodoc:
48
41
  # is similar to a SQL OR. This is named #any_of and aliased "or" for
49
42
  # readability.
50
43
  #
51
- # Options:
52
- #
53
- # selector: Multiple +Hash+ expressions that any can match.
44
+ # @example Adding the criterion.
45
+ # criteria.any_of({ :field1 => "value" }, { :field2 => "value2" })
54
46
  #
55
- # Example:
47
+ # @param [ Array<Hash> ] args A list of name/value pairs any can match.
56
48
  #
57
- # <tt>criteria.any_of({ :field1 => "value" }, { :field2 => "value2" })</tt>
58
- #
59
- # Returns: <tt>self</tt>
49
+ # @return [ Criteria ] A new criteria with the added selector.
60
50
  def any_of(*args)
61
- criterion = @selector["$or"] || []
62
- expanded = args.collect(&:expand_complex_criteria)
63
- @selector["$or"] = criterion.concat(expanded)
64
- self
51
+ clone.tap do |crit|
52
+ criterion = @selector["$or"] || []
53
+ expanded = args.collect(&:expand_complex_criteria)
54
+ crit.selector["$or"] = criterion.concat(expanded)
55
+ end
65
56
  end
66
57
  alias :or :any_of
67
58
 
68
- # Adds a criterion to the +Criteria+ that specifies values where any can
69
- # be matched in order to return results. This is similar to an SQL "IN"
70
- # clause. The MongoDB conditional operator that will be used is "$in".
59
+ # Using the existing criteria, find a document by a single id, multiple
60
+ # ids, or using a conditions hash.
71
61
  #
72
- # Options:
62
+ # @example Find a single document by id.
63
+ # Person.where(:title => "Sir").find(id)
73
64
  #
74
- # attributes: A +Hash+ where the key is the field name and the value is an
75
- # +Array+ of values that any can match.
65
+ # @example Find multiple documents by ids.
66
+ # Person.where(:title => "Sir").find([ id_one, id_two ])
76
67
  #
77
- # Example:
68
+ # @return [ Document, Array<Document> ] The matching document(s).
78
69
  #
79
- # <tt>criteria.in(:field => ["value1", "value2"])</tt>
70
+ # @since 2.0.0.rc.1
71
+ def find(*args)
72
+ id_criteria(*args)
73
+ end
74
+
75
+ # Adds a criterion to the +Criteria+ that specifies values where any can
76
+ # be matched in order to return results. This is similar to an SQL "IN"
77
+ # clause. The MongoDB conditional operator that will be used is "$in".
78
+ #
79
+ # @example Adding the criterion.
80
+ # criteria.in(:field => ["value1", "value2"])
81
+ # criteria.in(:field1 => ["value1", "value2"], :field2 => ["value1"])
80
82
  #
81
- # <tt>criteria.in(:field1 => ["value1", "value2"], :field2 => ["value1"])</tt>
83
+ # @param [ Hash ] attributes Name/value pairs any can match.
82
84
  #
83
- # Returns: <tt>self</tt>
85
+ # @return [ Criteria ] A new criteria with the added selector.
84
86
  def in(attributes = {})
85
87
  update_selector(attributes, "$in")
86
88
  end
@@ -89,16 +91,12 @@ module Mongoid #:nodoc:
89
91
  # Adds a criterion to the +Criteria+ that specifies values to do
90
92
  # geospacial searches by. The field must be indexed with the "2d" option.
91
93
  #
92
- # Options:
93
- #
94
- # attributes: A +Hash+ where the keys are the field names and the values are
95
- # +Arrays+ of [latitude, longitude] pairs.
94
+ # @example Adding the criterion.
95
+ # criteria.near(:field1 => [30, -44])
96
96
  #
97
- # Example:
97
+ # @param [ Hash ] attributes The fields with lat/long values.
98
98
  #
99
- # <tt>criteria.near(:field1 => [30, -44])</tt>
100
- #
101
- # Returns: <tt>self</tt>
99
+ # @return [ Criteria ] A new criteria with the added selector.
102
100
  def near(attributes = {})
103
101
  update_selector(attributes, "$near")
104
102
  end
@@ -109,30 +107,27 @@ module Mongoid #:nodoc:
109
107
  # similar to the Javascript object that is used when performing a find()
110
108
  # in the MongoDB console.
111
109
  #
112
- # Options:
113
- #
114
- # selector: A +Hash+ that must match the attributes of the +Document+.
115
- #
116
- # Example:
110
+ # @example Adding the criterion.
111
+ # criteria.where(:field1 => "value1", :field2 => 15)
117
112
  #
118
- # <tt>criteria.where(:field1 => "value1", :field2 => 15)</tt>
113
+ # @param [ Hash ] selector Name/value pairs where all must match.
119
114
  #
120
- # Returns: <tt>self</tt>
115
+ # @return [ Criteria ] A new criteria with the added selector.
121
116
  def where(selector = nil)
122
- selector = case selector
123
- when String then {"$where" => selector}
124
- else selector ? selector.expand_complex_criteria : {}
125
- end
117
+ clone.tap do |crit|
118
+ selector = case selector
119
+ when String then {"$where" => selector}
120
+ else selector ? selector.expand_complex_criteria : {}
121
+ end
126
122
 
127
- selector.each_pair do |key, value|
128
- if @selector.has_key?(key) && @selector[key].respond_to?(:merge!)
129
- @selector[key].merge!(value)
130
- else
131
- @selector[key] = value
123
+ selector.each_pair do |key, value|
124
+ if crit.selector.has_key?(key) && crit.selector[key].respond_to?(:merge!)
125
+ crit.selector[key].merge!(value)
126
+ else
127
+ crit.selector[key] = value
128
+ end
132
129
  end
133
130
  end
134
-
135
- self
136
131
  end
137
132
  end
138
133
  end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+ module Mongoid #:nodoc:
3
+ module Criterion #:nodoc:
4
+ module Inspection #:nodoc:
5
+
6
+ # Get a pretty string representation of the criteria, including the
7
+ # selector, options, matching count and documents for inspection.
8
+ #
9
+ # @example Inspect the criteria.
10
+ # criteria.inspect
11
+ #
12
+ # @return [ String ] The inspection string.
13
+ def inspect
14
+ "#<Mongoid::Criteria\n" <<
15
+ " selector: #{selector.inspect},\n" <<
16
+ " options: #{options.inspect},\n" <<
17
+ " count: #{count.inspect},\n" <<
18
+ " matching: #{entries.inspect}>\n"
19
+ end
20
+ end
21
+ end
22
+ end
@@ -3,16 +3,6 @@ module Mongoid #:nodoc:
3
3
  module Criterion #:nodoc:
4
4
  module Optional
5
5
 
6
- def using_default_sort?
7
- @use_default_sort = true if @use_default_sort.nil? # TODO: move initialization elsewhere
8
- return @use_default_sort
9
- end
10
-
11
- def remove_default_sort
12
- @options[:sort] = nil if using_default_sort?
13
- @use_default_sort = false
14
- end
15
-
16
6
  # Adds fields to be sorted in ascending order. Will add them in the order
17
7
  # they were passed into the method.
18
8
  #
@@ -20,13 +10,11 @@ module Mongoid #:nodoc:
20
10
  #
21
11
  # <tt>criteria.ascending(:title, :dob)</tt>
22
12
  def ascending(*fields)
23
- remove_default_sort
24
-
25
- @options[:sort] = [] unless @options[:sort] || fields.first.nil?
26
- fields.flatten.each { |field| @options[:sort] << [ field, :asc ] }
27
- self
13
+ clone.tap do |crit|
14
+ crit.options[:sort] = [] unless options[:sort] || fields.first.nil?
15
+ fields.flatten.each { |field| crit.options[:sort] << [ field, :asc ] }
16
+ end
28
17
  end
29
-
30
18
  alias :asc :ascending
31
19
 
32
20
  # Tells the criteria that the cursor that gets returned needs to be
@@ -38,7 +26,7 @@ module Mongoid #:nodoc:
38
26
  #
39
27
  # <tt>criteria.cache</tt>
40
28
  def cache
41
- @options.merge!(:cache => true); self
29
+ clone.tap { |crit| crit.options.merge!(:cache => true) }
42
30
  end
43
31
 
44
32
  # Will return true if the cache option has been set.
@@ -47,7 +35,7 @@ module Mongoid #:nodoc:
47
35
  #
48
36
  # <tt>criteria.cached?</tt>
49
37
  def cached?
50
- @options[:cache] == true
38
+ options[:cache] == true
51
39
  end
52
40
 
53
41
  # Adds fields to be sorted in descending order. Will add them in the order
@@ -57,13 +45,11 @@ module Mongoid #:nodoc:
57
45
  #
58
46
  # <tt>criteria.descending(:title, :dob)</tt>
59
47
  def descending(*fields)
60
- remove_default_sort
61
-
62
- @options[:sort] = [] unless @options[:sort] || fields.first.nil?
63
- fields.flatten.each { |field| @options[:sort] << [ field, :desc ] }
64
- self
48
+ clone.tap do |crit|
49
+ crit.options[:sort] = [] unless options[:sort] || fields.first.nil?
50
+ fields.flatten.each { |field| crit.options[:sort] << [ field, :desc ] }
51
+ end
65
52
  end
66
-
67
53
  alias :desc :descending
68
54
 
69
55
  # Flags the criteria to execute against a read-only slave in the pool
@@ -73,7 +59,7 @@ module Mongoid #:nodoc:
73
59
  #
74
60
  # <tt>criteria.enslave</tt>
75
61
  def enslave
76
- @options.merge!(:enslave => true); self
62
+ clone.tap { |crit| crit.options.merge!(:enslave => true) }
77
63
  end
78
64
 
79
65
  # Will return true if the criteria is enslaved.
@@ -82,7 +68,7 @@ module Mongoid #:nodoc:
82
68
  #
83
69
  # <tt>criteria.enslaved?</tt>
84
70
  def enslaved?
85
- @options[:enslave] == true
71
+ options[:enslave] == true
86
72
  end
87
73
 
88
74
  # Adds a criterion to the +Criteria+ that specifies additional options
@@ -98,7 +84,10 @@ module Mongoid #:nodoc:
98
84
  #
99
85
  # Returns: <tt>self</tt>
100
86
  def extras(extras)
101
- @options.merge!(extras); filter_options; self
87
+ clone.tap do |crit|
88
+ crit.options.merge!(extras)
89
+ crit.filter_options
90
+ end
102
91
  end
103
92
 
104
93
  # Adds a criterion to the +Criteria+ that specifies an id that must be matched.
@@ -116,14 +105,15 @@ module Mongoid #:nodoc:
116
105
  def id(*ids)
117
106
  ids.flatten!
118
107
  if ids.size > 1
119
- self.in(
120
- :_id => ::BSON::ObjectId.cast!(@klass, ids, @klass.primary_key.nil?)
108
+ any_in(
109
+ :_id => ::BSON::ObjectId.cast!(klass, ids, klass.primary_key.nil?)
121
110
  )
122
111
  else
123
- @selector[:_id] =
124
- ::BSON::ObjectId.cast!(@klass, ids.first, @klass.primary_key.nil?)
112
+ clone.tap do |crit|
113
+ crit.selector[:_id] =
114
+ ::BSON::ObjectId.cast!(klass, ids.first, klass.primary_key.nil?)
115
+ end
125
116
  end
126
- self
127
117
  end
128
118
 
129
119
  # Adds a criterion to the +Criteria+ that specifies the maximum number of
@@ -140,14 +130,14 @@ module Mongoid #:nodoc:
140
130
  #
141
131
  # Returns: <tt>self</tt>
142
132
  def limit(value = 20)
143
- @options[:limit] = value; self
133
+ clone.tap { |crit| crit.options[:limit] = value }
144
134
  end
145
135
 
146
136
  # Returns the offset option. If a per_page option is in the list then it
147
137
  # will replace it with a skip parameter and return the same value. Defaults
148
138
  # to 20 if nothing was provided.
149
139
  def offset(*args)
150
- args.size > 0 ? skip(args.first) : @options[:skip]
140
+ args.size > 0 ? skip(args.first) : options[:skip]
151
141
  end
152
142
 
153
143
  # Adds a criterion to the +Criteria+ that specifies the sort order of
@@ -163,26 +153,24 @@ module Mongoid #:nodoc:
163
153
  #
164
154
  # Returns: <tt>self</tt>
165
155
  def order_by(*args)
166
- remove_default_sort
167
- set_order_by(*args)
168
- end
169
-
170
- def set_order_by(*args)
171
- @options[:sort] = [] unless @options[:sort] || args.first.nil?
172
- arguments = args.first
173
- case arguments
174
- when Hash
175
- arguments.each do |field, direction|
176
- @options[:sort] << [ field, direction ]
156
+ clone.tap do |crit|
157
+ crit.options[:sort] = [] unless options[:sort] || args.first.nil?
158
+ arguments = args.first
159
+ case arguments
160
+ when Hash
161
+ arguments.each do |field, direction|
162
+ crit.options[:sort] << [ field, direction ]
163
+ end
164
+ when Array
165
+ crit.options[:sort].concat(arguments)
166
+ when Complex
167
+ args.flatten.each do |complex|
168
+ crit.options[:sort] << [ complex.key, complex.operator.to_sym ]
169
+ end
177
170
  end
178
- when Array
179
- @options[:sort].concat(arguments)
180
- when Complex
181
- args.flatten.each do |complex|
182
- @options[:sort] << [ complex.key, complex.operator.to_sym ]
183
- end
184
- end; self
171
+ end
185
172
  end
173
+ alias :order :order_by
186
174
 
187
175
  # Adds a criterion to the +Criteria+ that specifies how many results to skip
188
176
  # when returning Documents. This is mostly used in conjunction with
@@ -199,7 +187,7 @@ module Mongoid #:nodoc:
199
187
  #
200
188
  # Returns: <tt>self</tt>
201
189
  def skip(value = 0)
202
- @options[:skip] = value; self
190
+ clone.tap { |crit| crit.options[:skip] = value }
203
191
  end
204
192
 
205
193
  # Adds a criterion to the +Criteria+ that specifies a type or an Array of
@@ -217,7 +205,7 @@ module Mongoid #:nodoc:
217
205
  # Returns: <tt>self</tt>
218
206
  def type(types)
219
207
  types = [types] unless types.is_a?(Array)
220
- self.in(:_type => types)
208
+ any_in(:_type => types)
221
209
  end
222
210
 
223
211
  end