chhean-mongoid 2.0.1.beta1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT_LICENSE +20 -0
- data/README.rdoc +49 -0
- data/lib/mongoid.rb +139 -0
- data/lib/mongoid/associations.rb +327 -0
- data/lib/mongoid/associations/embedded_in.rb +72 -0
- data/lib/mongoid/associations/embeds_many.rb +262 -0
- data/lib/mongoid/associations/embeds_one.rb +95 -0
- data/lib/mongoid/associations/foreign_key.rb +35 -0
- data/lib/mongoid/associations/meta_data.rb +29 -0
- data/lib/mongoid/associations/options.rb +73 -0
- data/lib/mongoid/associations/proxy.rb +33 -0
- data/lib/mongoid/associations/referenced_in.rb +71 -0
- data/lib/mongoid/associations/references_many.rb +243 -0
- data/lib/mongoid/associations/references_many_as_array.rb +78 -0
- data/lib/mongoid/associations/references_one.rb +116 -0
- data/lib/mongoid/attributes.rb +226 -0
- data/lib/mongoid/callbacks.rb +17 -0
- data/lib/mongoid/collection.rb +120 -0
- data/lib/mongoid/collections.rb +41 -0
- data/lib/mongoid/collections/cyclic_iterator.rb +34 -0
- data/lib/mongoid/collections/master.rb +29 -0
- data/lib/mongoid/collections/operations.rb +41 -0
- data/lib/mongoid/collections/slaves.rb +45 -0
- data/lib/mongoid/components.rb +32 -0
- data/lib/mongoid/config.rb +237 -0
- data/lib/mongoid/contexts.rb +24 -0
- data/lib/mongoid/contexts/enumerable.rb +151 -0
- data/lib/mongoid/contexts/ids.rb +25 -0
- data/lib/mongoid/contexts/mongo.rb +285 -0
- data/lib/mongoid/contexts/paging.rb +50 -0
- data/lib/mongoid/criteria.rb +230 -0
- data/lib/mongoid/criterion/complex.rb +21 -0
- data/lib/mongoid/criterion/exclusion.rb +65 -0
- data/lib/mongoid/criterion/inclusion.rb +110 -0
- data/lib/mongoid/criterion/optional.rb +136 -0
- data/lib/mongoid/cursor.rb +82 -0
- data/lib/mongoid/deprecation.rb +22 -0
- data/lib/mongoid/dirty.rb +254 -0
- data/lib/mongoid/document.rb +264 -0
- data/lib/mongoid/errors.rb +124 -0
- data/lib/mongoid/extensions.rb +106 -0
- data/lib/mongoid/extensions/array/accessors.rb +17 -0
- data/lib/mongoid/extensions/array/aliasing.rb +4 -0
- data/lib/mongoid/extensions/array/assimilation.rb +26 -0
- data/lib/mongoid/extensions/array/conversions.rb +27 -0
- data/lib/mongoid/extensions/array/parentization.rb +13 -0
- data/lib/mongoid/extensions/big_decimal/conversions.rb +19 -0
- data/lib/mongoid/extensions/binary/conversions.rb +17 -0
- data/lib/mongoid/extensions/boolean/conversions.rb +22 -0
- data/lib/mongoid/extensions/date/conversions.rb +24 -0
- data/lib/mongoid/extensions/datetime/conversions.rb +12 -0
- data/lib/mongoid/extensions/float/conversions.rb +20 -0
- data/lib/mongoid/extensions/hash/accessors.rb +38 -0
- data/lib/mongoid/extensions/hash/assimilation.rb +39 -0
- data/lib/mongoid/extensions/hash/conversions.rb +45 -0
- data/lib/mongoid/extensions/hash/criteria_helpers.rb +20 -0
- data/lib/mongoid/extensions/hash/scoping.rb +12 -0
- data/lib/mongoid/extensions/integer/conversions.rb +20 -0
- data/lib/mongoid/extensions/nil/assimilation.rb +17 -0
- data/lib/mongoid/extensions/object/conversions.rb +27 -0
- data/lib/mongoid/extensions/objectid/conversions.rb +15 -0
- data/lib/mongoid/extensions/proc/scoping.rb +12 -0
- data/lib/mongoid/extensions/set/conversions.rb +20 -0
- data/lib/mongoid/extensions/string/conversions.rb +15 -0
- data/lib/mongoid/extensions/string/inflections.rb +97 -0
- data/lib/mongoid/extensions/symbol/inflections.rb +39 -0
- data/lib/mongoid/extensions/time_conversions.rb +35 -0
- data/lib/mongoid/extras.rb +61 -0
- data/lib/mongoid/factory.rb +20 -0
- data/lib/mongoid/field.rb +80 -0
- data/lib/mongoid/fields.rb +61 -0
- data/lib/mongoid/finders.rb +144 -0
- data/lib/mongoid/identity.rb +39 -0
- data/lib/mongoid/indexes.rb +27 -0
- data/lib/mongoid/javascript.rb +21 -0
- data/lib/mongoid/javascript/functions.yml +37 -0
- data/lib/mongoid/matchers.rb +35 -0
- data/lib/mongoid/matchers/all.rb +11 -0
- data/lib/mongoid/matchers/default.rb +26 -0
- data/lib/mongoid/matchers/exists.rb +13 -0
- data/lib/mongoid/matchers/gt.rb +11 -0
- data/lib/mongoid/matchers/gte.rb +11 -0
- data/lib/mongoid/matchers/in.rb +11 -0
- data/lib/mongoid/matchers/lt.rb +11 -0
- data/lib/mongoid/matchers/lte.rb +11 -0
- data/lib/mongoid/matchers/ne.rb +11 -0
- data/lib/mongoid/matchers/nin.rb +11 -0
- data/lib/mongoid/matchers/size.rb +11 -0
- data/lib/mongoid/memoization.rb +33 -0
- data/lib/mongoid/named_scope.rb +37 -0
- data/lib/mongoid/observable.rb +30 -0
- data/lib/mongoid/paths.rb +62 -0
- data/lib/mongoid/persistence.rb +218 -0
- data/lib/mongoid/persistence/command.rb +39 -0
- data/lib/mongoid/persistence/insert.rb +47 -0
- data/lib/mongoid/persistence/insert_embedded.rb +38 -0
- data/lib/mongoid/persistence/remove.rb +39 -0
- data/lib/mongoid/persistence/remove_all.rb +37 -0
- data/lib/mongoid/persistence/remove_embedded.rb +50 -0
- data/lib/mongoid/persistence/update.rb +63 -0
- data/lib/mongoid/railtie.rb +54 -0
- data/lib/mongoid/railties/database.rake +37 -0
- data/lib/mongoid/scope.rb +75 -0
- data/lib/mongoid/state.rb +32 -0
- data/lib/mongoid/timestamps.rb +27 -0
- data/lib/mongoid/validations.rb +51 -0
- data/lib/mongoid/validations/associated.rb +32 -0
- data/lib/mongoid/validations/locale/en.yml +4 -0
- data/lib/mongoid/validations/uniqueness.rb +50 -0
- data/lib/mongoid/version.rb +4 -0
- data/lib/mongoid/versioning.rb +27 -0
- data/lib/rails/generators/mongoid/config/config_generator.rb +41 -0
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +24 -0
- data/lib/rails/generators/mongoid/model/model_generator.rb +24 -0
- data/lib/rails/generators/mongoid/model/templates/model.rb +15 -0
- data/lib/rails/generators/mongoid_generator.rb +61 -0
- metadata +284 -0
@@ -0,0 +1,50 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Contexts #:nodoc:
|
4
|
+
module Paging
|
5
|
+
# Paginates the documents.
|
6
|
+
#
|
7
|
+
# Example:
|
8
|
+
#
|
9
|
+
# <tt>context.paginate(:page => 6, :per_page => 25)</tt>
|
10
|
+
#
|
11
|
+
# Returns:
|
12
|
+
#
|
13
|
+
# A collection of documents paginated.
|
14
|
+
# All previous <tt>limit</tt> and <tt>skip</tt> call will be ignored.
|
15
|
+
def paginate(pager_options={})
|
16
|
+
if pager_options[:per_page]
|
17
|
+
options[:limit] = pager_options[:per_page].to_i
|
18
|
+
if pager_options[:page]
|
19
|
+
options[:skip] = (pager_options[:page].to_i - 1) * pager_options[:per_page].to_i
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
@collection ||= execute(true)
|
24
|
+
WillPaginate::Collection.create(page, per_page, count) do |pager|
|
25
|
+
pager.replace(@collection.to_a)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Either returns the page option and removes it from the options, or
|
30
|
+
# returns a default value of 1.
|
31
|
+
#
|
32
|
+
# Returns:
|
33
|
+
#
|
34
|
+
# An +Integer+ page number.
|
35
|
+
def page
|
36
|
+
skips, limits = options[:skip], options[:limit]
|
37
|
+
(skips && limits) ? (skips + limits) / limits : 1
|
38
|
+
end
|
39
|
+
|
40
|
+
# Get the number of results per page or the default of 20.
|
41
|
+
#
|
42
|
+
# Returns:
|
43
|
+
#
|
44
|
+
# The +Integer+ number of documents in each page.
|
45
|
+
def per_page
|
46
|
+
(options[:limit] || 20).to_i
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,230 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "mongoid/criterion/complex"
|
3
|
+
require "mongoid/criterion/exclusion"
|
4
|
+
require "mongoid/criterion/inclusion"
|
5
|
+
require "mongoid/criterion/optional"
|
6
|
+
|
7
|
+
module Mongoid #:nodoc:
|
8
|
+
# The +Criteria+ class is the core object needed in Mongoid to retrieve
|
9
|
+
# objects from the database. It is a DSL that essentially sets up the
|
10
|
+
# selector and options arguments that get passed on to a <tt>Mongo::Collection</tt>
|
11
|
+
# in the Ruby driver. Each method on the +Criteria+ returns self to they
|
12
|
+
# can be chained in order to create a readable criterion to be executed
|
13
|
+
# against the database.
|
14
|
+
#
|
15
|
+
# Example setup:
|
16
|
+
#
|
17
|
+
# <tt>criteria = Criteria.new</tt>
|
18
|
+
#
|
19
|
+
# <tt>criteria.only(:field).where(:field => "value").skip(20).limit(20)</tt>
|
20
|
+
#
|
21
|
+
# <tt>criteria.execute</tt>
|
22
|
+
class Criteria
|
23
|
+
include Criterion::Exclusion
|
24
|
+
include Criterion::Inclusion
|
25
|
+
include Criterion::Optional
|
26
|
+
include Enumerable
|
27
|
+
|
28
|
+
attr_reader :collection, :ids, :klass, :options, :selector
|
29
|
+
attr_accessor :documents
|
30
|
+
|
31
|
+
delegate :aggregate, :avg, :blank?, :count, :distinct, :empty?,
|
32
|
+
:execute, :first, :group, :id_criteria, :last, :max,
|
33
|
+
:min, :one, :page, :paginate, :per_page, :sum, :to => :context
|
34
|
+
|
35
|
+
# Concatinate the criteria with another enumerable. If the other is a
|
36
|
+
# +Criteria+ then it needs to get the collection from it.
|
37
|
+
def +(other)
|
38
|
+
entries + comparable(other)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Returns the difference between the criteria and another enumerable. If
|
42
|
+
# the other is a +Criteria+ then it needs to get the collection from it.
|
43
|
+
def -(other)
|
44
|
+
entries - comparable(other)
|
45
|
+
end
|
46
|
+
|
47
|
+
# Returns true if the supplied +Enumerable+ or +Criteria+ is equal to the results
|
48
|
+
# of this +Criteria+ or the criteria itself.
|
49
|
+
#
|
50
|
+
# This will force a database load when called if an enumerable is passed.
|
51
|
+
#
|
52
|
+
# Options:
|
53
|
+
#
|
54
|
+
# other: The other +Enumerable+ or +Criteria+ to compare to.
|
55
|
+
def ==(other)
|
56
|
+
case other
|
57
|
+
when Criteria
|
58
|
+
self.selector == other.selector && self.options == other.options
|
59
|
+
when Enumerable
|
60
|
+
return (execute.entries == other)
|
61
|
+
else
|
62
|
+
return false
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# Return or create the context in which this criteria should be executed.
|
67
|
+
#
|
68
|
+
# This will return an Enumerable context if the class is embedded,
|
69
|
+
# otherwise it will return a Mongo context for root classes.
|
70
|
+
def context
|
71
|
+
@context ||= Contexts.context_for(self)
|
72
|
+
end
|
73
|
+
|
74
|
+
# Iterate over each +Document+ in the results. This can take an optional
|
75
|
+
# block to pass to each argument in the results.
|
76
|
+
#
|
77
|
+
# Example:
|
78
|
+
#
|
79
|
+
# <tt>criteria.each { |doc| p doc }</tt>
|
80
|
+
def each(&block)
|
81
|
+
context.iterate(&block)
|
82
|
+
self
|
83
|
+
end
|
84
|
+
|
85
|
+
# Merges the supplied argument hash into a single criteria
|
86
|
+
#
|
87
|
+
# Options:
|
88
|
+
#
|
89
|
+
# criteria_conditions: Hash of criteria keys, and parameter values
|
90
|
+
#
|
91
|
+
# Example:
|
92
|
+
#
|
93
|
+
# <tt>criteria.fuse(:where => { :field => "value"}, :limit => 20)</tt>
|
94
|
+
#
|
95
|
+
# Returns <tt>self</tt>
|
96
|
+
def fuse(criteria_conditions = {})
|
97
|
+
criteria_conditions.inject(self) do |criteria, (key, value)|
|
98
|
+
criteria.send(key, value)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# Create the new +Criteria+ object. This will initialize the selector
|
103
|
+
# and options hashes, as well as the type of criteria.
|
104
|
+
#
|
105
|
+
# Options:
|
106
|
+
#
|
107
|
+
# type: One of :all, :first:, or :last
|
108
|
+
# klass: The class to execute on.
|
109
|
+
def initialize(klass)
|
110
|
+
@selector, @options, @klass, @documents = {}, {}, klass, []
|
111
|
+
end
|
112
|
+
|
113
|
+
# Merges another object into this +Criteria+. The other object may be a
|
114
|
+
# +Criteria+ or a +Hash+. This is used to combine multiple scopes together,
|
115
|
+
# where a chained scope situation may be desired.
|
116
|
+
#
|
117
|
+
# Options:
|
118
|
+
#
|
119
|
+
# other: The +Criteria+ or +Hash+ to merge with.
|
120
|
+
#
|
121
|
+
# Example:
|
122
|
+
#
|
123
|
+
# <tt>criteria.merge({ :conditions => { :title => "Sir" } })</tt>
|
124
|
+
def merge(other)
|
125
|
+
@selector.update(other.selector)
|
126
|
+
@options.update(other.options)
|
127
|
+
@documents = other.documents
|
128
|
+
end
|
129
|
+
|
130
|
+
# Used for chaining +Criteria+ scopes together in the for of class methods
|
131
|
+
# on the +Document+ the criteria is for.
|
132
|
+
#
|
133
|
+
# Options:
|
134
|
+
#
|
135
|
+
# name: The name of the class method on the +Document+ to chain.
|
136
|
+
# args: The arguments passed to the method.
|
137
|
+
#
|
138
|
+
# Returns: <tt>Criteria</tt>
|
139
|
+
def method_missing(name, *args)
|
140
|
+
if @klass.respond_to?(name)
|
141
|
+
new_scope = @klass.send(name, *args)
|
142
|
+
new_scope.merge(self)
|
143
|
+
return new_scope
|
144
|
+
else
|
145
|
+
return entries.send(name, *args)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
alias :to_ary :to_a
|
150
|
+
|
151
|
+
# Returns the selector and options as a +Hash+ that would be passed to a
|
152
|
+
# scope for use with named scopes.
|
153
|
+
def scoped
|
154
|
+
{ :where => @selector }.merge(@options)
|
155
|
+
end
|
156
|
+
|
157
|
+
# Translate the supplied arguments into a +Criteria+ object.
|
158
|
+
#
|
159
|
+
# If the passed in args is a single +String+, then it will
|
160
|
+
# construct an id +Criteria+ from it.
|
161
|
+
#
|
162
|
+
# If the passed in args are a type and a hash, then it will construct
|
163
|
+
# the +Criteria+ with the proper selector, options, and type.
|
164
|
+
#
|
165
|
+
# Options:
|
166
|
+
#
|
167
|
+
# args: either a +String+ or a +Symbol+, +Hash combination.
|
168
|
+
#
|
169
|
+
# Example:
|
170
|
+
#
|
171
|
+
# <tt>Criteria.translate(Person, "4ab2bc4b8ad548971900005c")</tt>
|
172
|
+
# <tt>Criteria.translate(Person, :conditions => { :field => "value"}, :limit => 20)</tt>
|
173
|
+
def self.translate(*args)
|
174
|
+
klass = args[0]
|
175
|
+
params = args[1] || {}
|
176
|
+
unless params.is_a?(Hash)
|
177
|
+
return klass.criteria.id_criteria(params)
|
178
|
+
end
|
179
|
+
conditions = params.delete(:conditions) || {}
|
180
|
+
if conditions.include?(:id)
|
181
|
+
conditions[:_id] = conditions[:id]
|
182
|
+
conditions.delete(:id)
|
183
|
+
end
|
184
|
+
return klass.criteria.where(conditions).extras(params)
|
185
|
+
end
|
186
|
+
|
187
|
+
protected
|
188
|
+
|
189
|
+
# Filters the unused options out of the options +Hash+. Currently this
|
190
|
+
# takes into account the "page" and "per_page" options that would be passed
|
191
|
+
# in if using will_paginate.
|
192
|
+
#
|
193
|
+
# Example:
|
194
|
+
#
|
195
|
+
# Given a criteria with a selector of { :page => 1, :per_page => 40 }
|
196
|
+
#
|
197
|
+
# <tt>criteria.filter_options</tt> # selector: { :skip => 0, :limit => 40 }
|
198
|
+
def filter_options
|
199
|
+
page_num = @options.delete(:page)
|
200
|
+
per_page_num = @options.delete(:per_page)
|
201
|
+
if (page_num || per_page_num)
|
202
|
+
@options[:limit] = limits = (per_page_num || 20).to_i
|
203
|
+
@options[:skip] = (page_num || 1).to_i * limits - limits
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
# Return the entries of the other criteria or the object. Used for
|
208
|
+
# comparing criteria or an enumerable.
|
209
|
+
def comparable(other)
|
210
|
+
other.is_a?(Criteria) ? other.entries : other
|
211
|
+
end
|
212
|
+
|
213
|
+
# Update the selector setting the operator on the value for each key in the
|
214
|
+
# supplied attributes +Hash+.
|
215
|
+
#
|
216
|
+
# Example:
|
217
|
+
#
|
218
|
+
# <tt>criteria.update_selector({ :field => "value" }, "$in")</tt>
|
219
|
+
def update_selector(attributes, operator)
|
220
|
+
attributes.each do |key, value|
|
221
|
+
unless @selector[key]
|
222
|
+
@selector[key] = { operator => value }
|
223
|
+
else
|
224
|
+
new_value = @selector[key].values.first + value
|
225
|
+
@selector[key] = { operator => new_value }
|
226
|
+
end
|
227
|
+
end; self
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Criterion #:nodoc:
|
4
|
+
# Complex criterion are used when performing operations on symbols to get
|
5
|
+
# get a shorthand syntax for where clauses.
|
6
|
+
#
|
7
|
+
# Example:
|
8
|
+
#
|
9
|
+
# <tt>{ :field => { "$lt" => "value" } }</tt>
|
10
|
+
# becomes:
|
11
|
+
# <tt> { :field.lt => "value }</tt>
|
12
|
+
class Complex
|
13
|
+
attr_accessor :key, :operator
|
14
|
+
|
15
|
+
# Create the new complex criterion.
|
16
|
+
def initialize(opts = {})
|
17
|
+
@key, @operator = opts[:key], opts[:operator]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Criterion #:nodoc:
|
4
|
+
module Exclusion
|
5
|
+
# Adds a criterion to the +Criteria+ that specifies values that are not allowed
|
6
|
+
# to match any document in the database. The MongoDB conditional operator that
|
7
|
+
# will be used is "$ne".
|
8
|
+
#
|
9
|
+
# Options:
|
10
|
+
#
|
11
|
+
# attributes: A +Hash+ where the key is the field name and the value is a
|
12
|
+
# value that must not be equal to the corresponding field value in the database.
|
13
|
+
#
|
14
|
+
# Example:
|
15
|
+
#
|
16
|
+
# <tt>criteria.excludes(:field => "value1")</tt>
|
17
|
+
#
|
18
|
+
# <tt>criteria.excludes(:field1 => "value1", :field2 => "value1")</tt>
|
19
|
+
#
|
20
|
+
# Returns: <tt>self</tt>
|
21
|
+
def excludes(attributes = {})
|
22
|
+
mongo_id = attributes.delete(:id)
|
23
|
+
attributes = attributes.merge(:_id => mongo_id) if mongo_id
|
24
|
+
update_selector(attributes, "$ne")
|
25
|
+
end
|
26
|
+
|
27
|
+
# Adds a criterion to the +Criteria+ that specifies values where none
|
28
|
+
# should match in order to return results. This is similar to an SQL "NOT IN"
|
29
|
+
# clause. The MongoDB conditional operator that will be used is "$nin".
|
30
|
+
#
|
31
|
+
# Options:
|
32
|
+
#
|
33
|
+
# attributes: A +Hash+ where the key is the field name and the value is an
|
34
|
+
# +Array+ of values that none can match.
|
35
|
+
#
|
36
|
+
# Example:
|
37
|
+
#
|
38
|
+
# <tt>criteria.not_in(:field => ["value1", "value2"])</tt>
|
39
|
+
#
|
40
|
+
# <tt>criteria.not_in(:field1 => ["value1", "value2"], :field2 => ["value1"])</tt>
|
41
|
+
#
|
42
|
+
# Returns: <tt>self</tt>
|
43
|
+
def not_in(attributes)
|
44
|
+
update_selector(attributes, "$nin")
|
45
|
+
end
|
46
|
+
|
47
|
+
# Adds a criterion to the +Criteria+ that specifies the fields that will
|
48
|
+
# get returned from the Document. Used mainly for list views that do not
|
49
|
+
# require all fields to be present. This is similar to SQL "SELECT" values.
|
50
|
+
#
|
51
|
+
# Options:
|
52
|
+
#
|
53
|
+
# args: A list of field names to retrict the returned fields to.
|
54
|
+
#
|
55
|
+
# Example:
|
56
|
+
#
|
57
|
+
# <tt>criteria.only(:field1, :field2, :field3)</tt>
|
58
|
+
#
|
59
|
+
# Returns: <tt>self</tt>
|
60
|
+
def only(*args)
|
61
|
+
@options[:fields] = args.flatten if args.any?; self
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Mongoid #:nodoc:
|
3
|
+
module Criterion #:nodoc:
|
4
|
+
module Inclusion
|
5
|
+
# Adds a criterion to the +Criteria+ that specifies values that must all
|
6
|
+
# be matched in order to return results. Similar to an "in" clause but the
|
7
|
+
# underlying conditional logic is an "AND" and not an "OR". The MongoDB
|
8
|
+
# conditional operator that will be used is "$all".
|
9
|
+
#
|
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>
|
18
|
+
#
|
19
|
+
# <tt>criteria.all(:field1 => ["value1", "value2"], :field2 => ["value1"])</tt>
|
20
|
+
#
|
21
|
+
# Returns: <tt>self</tt>
|
22
|
+
def all(attributes = {})
|
23
|
+
update_selector(attributes, "$all")
|
24
|
+
end
|
25
|
+
alias :all_in :all
|
26
|
+
|
27
|
+
# Adds a criterion to the +Criteria+ that specifies values that must
|
28
|
+
# be matched in order to return results. This is similar to a SQL "WHERE"
|
29
|
+
# clause. This is the actual selector that will be provided to MongoDB,
|
30
|
+
# similar to the Javascript object that is used when performing a find()
|
31
|
+
# in the MongoDB console.
|
32
|
+
#
|
33
|
+
# Options:
|
34
|
+
#
|
35
|
+
# selectior: A +Hash+ that must match the attributes of the +Document+.
|
36
|
+
#
|
37
|
+
# Example:
|
38
|
+
#
|
39
|
+
# <tt>criteria.and(:field1 => "value1", :field2 => 15)</tt>
|
40
|
+
#
|
41
|
+
# Returns: <tt>self</tt>
|
42
|
+
def and(selector = nil)
|
43
|
+
where(selector)
|
44
|
+
end
|
45
|
+
|
46
|
+
# Adds a criterion to the +Criteria+ that specifies values where any can
|
47
|
+
# be matched in order to return results. This is similar to an SQL "IN"
|
48
|
+
# clause. The MongoDB conditional operator that will be used is "$in".
|
49
|
+
#
|
50
|
+
# Options:
|
51
|
+
#
|
52
|
+
# attributes: A +Hash+ where the key is the field name and the value is an
|
53
|
+
# +Array+ of values that any can match.
|
54
|
+
#
|
55
|
+
# Example:
|
56
|
+
#
|
57
|
+
# <tt>criteria.in(:field => ["value1", "value2"])</tt>
|
58
|
+
#
|
59
|
+
# <tt>criteria.in(:field1 => ["value1", "value2"], :field2 => ["value1"])</tt>
|
60
|
+
#
|
61
|
+
# Returns: <tt>self</tt>
|
62
|
+
def in(attributes = {})
|
63
|
+
update_selector(attributes, "$in")
|
64
|
+
end
|
65
|
+
alias :any_in :in
|
66
|
+
|
67
|
+
# Adds a criterion to the +Criteria+ that specifies values to do
|
68
|
+
# geospacial searches by. The field must be indexed with the "2d" option.
|
69
|
+
#
|
70
|
+
# Options:
|
71
|
+
#
|
72
|
+
# attributes: A +Hash+ where the keys are the field names and the values are
|
73
|
+
# +Arrays+ of [latitude, longitude] pairs.
|
74
|
+
#
|
75
|
+
# Example:
|
76
|
+
#
|
77
|
+
# <tt>criteria.near(:field1 => [30, -44])</tt>
|
78
|
+
#
|
79
|
+
# Returns: <tt>self</tt>
|
80
|
+
def near(attributes = {})
|
81
|
+
update_selector(attributes, "$near")
|
82
|
+
end
|
83
|
+
|
84
|
+
# Adds a criterion to the +Criteria+ that specifies values that must
|
85
|
+
# be matched in order to return results. This is similar to a SQL "WHERE"
|
86
|
+
# clause. This is the actual selector that will be provided to MongoDB,
|
87
|
+
# similar to the Javascript object that is used when performing a find()
|
88
|
+
# in the MongoDB console.
|
89
|
+
#
|
90
|
+
# Options:
|
91
|
+
#
|
92
|
+
# selectior: A +Hash+ that must match the attributes of the +Document+.
|
93
|
+
#
|
94
|
+
# Example:
|
95
|
+
#
|
96
|
+
# <tt>criteria.where(:field1 => "value1", :field2 => 15)</tt>
|
97
|
+
#
|
98
|
+
# Returns: <tt>self</tt>
|
99
|
+
def where(selector = nil)
|
100
|
+
case selector
|
101
|
+
when String
|
102
|
+
@selector.update("$where" => selector)
|
103
|
+
else
|
104
|
+
@selector.update(selector ? selector.expand_complex_criteria : {})
|
105
|
+
end
|
106
|
+
self
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|