mongoid 6.1.0 → 6.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/mongoid/atomic.rb +3 -3
- data/lib/mongoid/attributes/dynamic.rb +3 -2
- data/lib/mongoid/attributes/nested.rb +1 -1
- data/lib/mongoid/attributes/processing.rb +0 -4
- data/lib/mongoid/attributes.rb +1 -2
- data/lib/mongoid/changeable.rb +1 -1
- data/lib/mongoid/clients/options.rb +1 -1
- data/lib/mongoid/composable.rb +3 -1
- data/lib/mongoid/contextual/atomic.rb +1 -1
- data/lib/mongoid/contextual/geo_near.rb +3 -2
- data/lib/mongoid/contextual/memory.rb +3 -3
- data/lib/mongoid/contextual/mongo.rb +5 -5
- data/lib/mongoid/contextual/none.rb +14 -2
- data/lib/mongoid/copyable.rb +1 -5
- data/lib/mongoid/criteria/findable.rb +3 -2
- data/lib/mongoid/criteria/includable.rb +2 -2
- data/lib/mongoid/criteria/queryable/aggregable.rb +1 -1
- data/lib/mongoid/criteria/queryable/extensions/array.rb +1 -1
- data/lib/mongoid/criteria/queryable/extensions/boolean.rb +1 -1
- data/lib/mongoid/criteria/queryable/extensions/hash.rb +4 -4
- data/lib/mongoid/criteria/queryable/extensions/object.rb +4 -4
- data/lib/mongoid/criteria/queryable/extensions/regexp.rb +34 -0
- data/lib/mongoid/criteria/queryable/options.rb +16 -0
- data/lib/mongoid/criteria/queryable/selector.rb +1 -1
- data/lib/mongoid/criteria.rb +2 -2
- data/lib/mongoid/document.rb +1 -1
- data/lib/mongoid/errors/delete_restriction.rb +2 -2
- data/lib/mongoid/errors/inverse_not_found.rb +1 -1
- data/lib/mongoid/extensions/hash.rb +3 -2
- data/lib/mongoid/extensions/object.rb +2 -2
- data/lib/mongoid/extensions/time.rb +1 -1
- data/lib/mongoid/factory.rb +0 -1
- data/lib/mongoid/fields/foreign_key.rb +2 -2
- data/lib/mongoid/findable.rb +1 -1
- data/lib/mongoid/indexable/specification.rb +1 -1
- data/lib/mongoid/indexable.rb +3 -2
- data/lib/mongoid/interceptable.rb +9 -11
- data/lib/mongoid/loggable.rb +1 -1
- data/lib/mongoid/matchable/all.rb +2 -2
- data/lib/mongoid/matchable/and.rb +3 -3
- data/lib/mongoid/matchable/default.rb +2 -2
- data/lib/mongoid/matchable/elem_match.rb +3 -3
- data/lib/mongoid/matchable/exists.rb +2 -2
- data/lib/mongoid/matchable/gt.rb +2 -2
- data/lib/mongoid/matchable/gte.rb +2 -2
- data/lib/mongoid/matchable/in.rb +2 -2
- data/lib/mongoid/matchable/lt.rb +2 -2
- data/lib/mongoid/matchable/lte.rb +2 -2
- data/lib/mongoid/matchable/ne.rb +2 -2
- data/lib/mongoid/matchable/nin.rb +2 -2
- data/lib/mongoid/matchable/or.rb +3 -3
- data/lib/mongoid/matchable/regexp.rb +27 -0
- data/lib/mongoid/matchable/size.rb +2 -2
- data/lib/mongoid/matchable.rb +16 -9
- data/lib/mongoid/persistable/creatable.rb +2 -4
- data/lib/mongoid/persistable/deletable.rb +1 -1
- data/lib/mongoid/persistable/settable.rb +1 -1
- data/lib/mongoid/persistable/upsertable.rb +1 -1
- data/lib/mongoid/persistable.rb +0 -3
- data/lib/mongoid/persistence_context.rb +2 -3
- data/lib/mongoid/relations/accessors.rb +1 -2
- data/lib/mongoid/relations/bindings/embedded/in.rb +0 -4
- data/lib/mongoid/relations/bindings/embedded/many.rb +1 -8
- data/lib/mongoid/relations/bindings/embedded/one.rb +0 -10
- data/lib/mongoid/relations/bindings/referenced/many.rb +0 -4
- data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +4 -4
- data/lib/mongoid/relations/builders/embedded/one.rb +1 -1
- data/lib/mongoid/relations/builders/nested_attributes/many.rb +1 -1
- data/lib/mongoid/relations/conversions.rb +1 -1
- data/lib/mongoid/relations/counter_cache.rb +10 -10
- data/lib/mongoid/relations/eager/base.rb +4 -4
- data/lib/mongoid/relations/eager.rb +6 -11
- data/lib/mongoid/relations/embedded/batchable.rb +3 -3
- data/lib/mongoid/relations/embedded/in.rb +1 -3
- data/lib/mongoid/relations/embedded/many.rb +12 -9
- data/lib/mongoid/relations/embedded/one.rb +1 -3
- data/lib/mongoid/relations/macros.rb +3 -2
- data/lib/mongoid/relations/metadata.rb +3 -3
- data/lib/mongoid/relations/nested_builder.rb +1 -1
- data/lib/mongoid/relations/proxy.rb +3 -2
- data/lib/mongoid/relations/referenced/in.rb +1 -4
- data/lib/mongoid/relations/referenced/many.rb +5 -7
- data/lib/mongoid/relations/referenced/many_to_many.rb +2 -2
- data/lib/mongoid/relations/referenced/one.rb +0 -2
- data/lib/mongoid/relations/reflections.rb +2 -2
- data/lib/mongoid/relations/synchronization.rb +11 -11
- data/lib/mongoid/scopable.rb +2 -2
- data/lib/mongoid/serializable.rb +4 -3
- data/lib/mongoid/tasks/database.rb +2 -1
- data/lib/mongoid/threaded.rb +4 -4
- data/lib/mongoid/validatable/macros.rb +4 -2
- data/lib/mongoid/validatable.rb +1 -1
- data/lib/mongoid/version.rb +1 -1
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +12 -2
- data/spec/app/models/book.rb +2 -1
- data/spec/app/models/page.rb +1 -1
- data/spec/mongoid/contextual/none_spec.rb +15 -0
- data/spec/mongoid/criteria/queryable/extensions/regexp_raw_spec.rb +89 -0
- data/spec/mongoid/criteria/queryable/selectable_spec.rb +15 -0
- data/spec/mongoid/criteria_spec.rb +6 -2
- data/spec/mongoid/interceptable_spec.rb +35 -8
- data/spec/mongoid/matchable/all_spec.rb +4 -4
- data/spec/mongoid/matchable/and_spec.rb +10 -10
- data/spec/mongoid/matchable/default_spec.rb +12 -12
- data/spec/mongoid/matchable/elem_match_spec.rb +11 -11
- data/spec/mongoid/matchable/exists_spec.rb +5 -5
- data/spec/mongoid/matchable/gt_spec.rb +8 -8
- data/spec/mongoid/matchable/gte_spec.rb +8 -8
- data/spec/mongoid/matchable/in_spec.rb +5 -5
- data/spec/mongoid/matchable/lt_spec.rb +8 -8
- data/spec/mongoid/matchable/lte_spec.rb +8 -8
- data/spec/mongoid/matchable/ne_spec.rb +5 -5
- data/spec/mongoid/matchable/nin_spec.rb +5 -5
- data/spec/mongoid/matchable/or_spec.rb +7 -7
- data/spec/mongoid/matchable/regexp_spec.rb +59 -0
- data/spec/mongoid/matchable/size_spec.rb +3 -3
- data/spec/mongoid/matchable_spec.rb +108 -58
- data/spec/mongoid/persistable/deletable_spec.rb +15 -0
- data/spec/mongoid/persistable/settable_spec.rb +16 -0
- data/spec/mongoid/persistence_context_spec.rb +22 -1
- data/spec/mongoid/relations/bindings/referenced/many_to_many_spec.rb +4 -4
- data/spec/mongoid/relations/embedded/many_spec.rb +24 -6
- data/spec/mongoid/relations/referenced/many_to_many_spec.rb +1 -1
- data/spec/mongoid/relations/synchronization_spec.rb +48 -2
- data/spec/mongoid/relations/touchable_spec.rb +40 -0
- data/spec/mongoid/validatable/uniqueness_spec.rb +2 -0
- data/spec/spec_helper.rb +2 -2
- data.tar.gz.sig +0 -0
- metadata +10 -5
- metadata.gz.sig +1 -1
data/lib/mongoid/indexable.rb
CHANGED
|
@@ -92,7 +92,7 @@ module Mongoid
|
|
|
92
92
|
# index({ name: 1 }, { background: true })
|
|
93
93
|
# end
|
|
94
94
|
#
|
|
95
|
-
# @param [ Symbol ]
|
|
95
|
+
# @param [ Symbol ] spec The index spec.
|
|
96
96
|
# @param [ Hash ] options The index options.
|
|
97
97
|
#
|
|
98
98
|
# @return [ Hash ] The index options.
|
|
@@ -110,7 +110,8 @@ module Mongoid
|
|
|
110
110
|
# @example Get the index specification.
|
|
111
111
|
# Model.index_specification(name: 1)
|
|
112
112
|
#
|
|
113
|
-
# @param [ Hash ]
|
|
113
|
+
# @param [ Hash ] index_hash The index key/direction pair.
|
|
114
|
+
# @param [ String ] index_name The index name.
|
|
114
115
|
#
|
|
115
116
|
# @return [ Specification ] The found specification.
|
|
116
117
|
#
|
|
@@ -46,7 +46,7 @@ module Mongoid
|
|
|
46
46
|
# @example Is the callback executable?
|
|
47
47
|
# document.callback_executable?(:save)
|
|
48
48
|
#
|
|
49
|
-
# @param [ Symbol ]
|
|
49
|
+
# @param [ Symbol ] kind The type of callback.
|
|
50
50
|
#
|
|
51
51
|
# @return [ true, false ] If the callback can be executed.
|
|
52
52
|
#
|
|
@@ -118,19 +118,13 @@ module Mongoid
|
|
|
118
118
|
# end
|
|
119
119
|
#
|
|
120
120
|
# @param [ Symbol ] kind The type of callback to execute.
|
|
121
|
-
# @param [ Array ]
|
|
121
|
+
# @param [ Array ] args Any options.
|
|
122
122
|
#
|
|
123
123
|
# @return [ Document ] The document
|
|
124
124
|
#
|
|
125
125
|
# @since 2.3.0
|
|
126
126
|
def run_callbacks(kind, *args, &block)
|
|
127
127
|
cascadable_children(kind).each do |child|
|
|
128
|
-
# This is returning false for some destroy tests on 4.1.0.beta1,
|
|
129
|
-
# causing them to fail since 4.1.0 expects a block to be passed if the
|
|
130
|
-
# callbacks for the type are empty. If no block is passed then the nil
|
|
131
|
-
# return value gets interpreted as false and halts the chain.
|
|
132
|
-
#
|
|
133
|
-
# @see https://github.com/rails/rails/blob/master/activesupport/lib/active_support/callbacks.rb#L79
|
|
134
128
|
if child.run_callbacks(child_callback_type(kind, child), *args) == false
|
|
135
129
|
return false
|
|
136
130
|
end
|
|
@@ -196,7 +190,7 @@ module Mongoid
|
|
|
196
190
|
#
|
|
197
191
|
# @since 2.3.0
|
|
198
192
|
def cascadable_child?(kind, child, metadata)
|
|
199
|
-
return false if kind == :initialize || kind == :find
|
|
193
|
+
return false if kind == :initialize || kind == :find || kind == :touch
|
|
200
194
|
return false if kind == :validate && metadata.validate?
|
|
201
195
|
child.callback_executable?(kind) ? child.in_callback_state?(kind) : false
|
|
202
196
|
end
|
|
@@ -260,8 +254,12 @@ module Mongoid
|
|
|
260
254
|
chain.append(callback) if callback.kind == place
|
|
261
255
|
end
|
|
262
256
|
self.class.send :define_method, name do
|
|
263
|
-
|
|
264
|
-
chain.compile
|
|
257
|
+
env = ActiveSupport::Callbacks::Filters::Environment.new(self, false, nil)
|
|
258
|
+
sequence = chain.compile
|
|
259
|
+
sequence.invoke_before(env)
|
|
260
|
+
env.value = !env.halted
|
|
261
|
+
sequence.invoke_after(env)
|
|
262
|
+
env.value
|
|
265
263
|
end
|
|
266
264
|
self.class.send :protected, name
|
|
267
265
|
end
|
data/lib/mongoid/loggable.rb
CHANGED
|
@@ -8,12 +8,12 @@ module Mongoid
|
|
|
8
8
|
# Return true if the attribute and first value in the hash are equal.
|
|
9
9
|
#
|
|
10
10
|
# @example Do the values match?
|
|
11
|
-
# matcher.
|
|
11
|
+
# matcher._matches?({ :key => 10 })
|
|
12
12
|
#
|
|
13
13
|
# @param [ Hash ] value The values to check.
|
|
14
14
|
#
|
|
15
15
|
# @return [ true, false ] If the values match.
|
|
16
|
-
def
|
|
16
|
+
def _matches?(value)
|
|
17
17
|
first = first(value)
|
|
18
18
|
return false if first.is_a?(Array) && first.empty?
|
|
19
19
|
|
|
@@ -8,19 +8,19 @@ module Mongoid
|
|
|
8
8
|
# Does the supplied query match the attribute?
|
|
9
9
|
#
|
|
10
10
|
# @example Does this match?
|
|
11
|
-
# matcher.
|
|
11
|
+
# matcher._matches?([ { field => value } ])
|
|
12
12
|
#
|
|
13
13
|
# @param [ Array ] conditions The or expression.
|
|
14
14
|
#
|
|
15
15
|
# @return [ true, false ] True if matches, false if not.
|
|
16
16
|
#
|
|
17
17
|
# @since 2.3.0
|
|
18
|
-
def
|
|
18
|
+
def _matches?(conditions)
|
|
19
19
|
conditions.each do |condition|
|
|
20
20
|
condition.keys.each do |k|
|
|
21
21
|
key = k
|
|
22
22
|
value = condition[k]
|
|
23
|
-
return false unless document.
|
|
23
|
+
return false unless document._matches?(key => value)
|
|
24
24
|
end
|
|
25
25
|
end
|
|
26
26
|
true
|
|
@@ -24,14 +24,14 @@ module Mongoid
|
|
|
24
24
|
# if the value is included.
|
|
25
25
|
#
|
|
26
26
|
# @example Does this value match?
|
|
27
|
-
# default.
|
|
27
|
+
# default._matches?("value")
|
|
28
28
|
#
|
|
29
29
|
# @param [ Object ] value The value to check if it matches.
|
|
30
30
|
#
|
|
31
31
|
# @return [ true, false ] True if matches, false if not.
|
|
32
32
|
#
|
|
33
33
|
# @since 1.0.0
|
|
34
|
-
def
|
|
34
|
+
def _matches?(value)
|
|
35
35
|
attribute.is_a?(Array) && !value.is_a?(Array) ? attribute.any? { |_attribute| value === _attribute } : value === attribute
|
|
36
36
|
end
|
|
37
37
|
|
|
@@ -7,19 +7,19 @@ module Mongoid
|
|
|
7
7
|
# Return true if a given predicate matches a sub document entirely
|
|
8
8
|
#
|
|
9
9
|
# @example Do the values match?
|
|
10
|
-
# matcher.
|
|
10
|
+
# matcher._matches?({"$elemMatch" => {"a" => 1, "b" => 2}})
|
|
11
11
|
#
|
|
12
12
|
# @param [ Hash ] value The values to check.
|
|
13
13
|
#
|
|
14
14
|
# @return [ true, false ] If the values match.
|
|
15
|
-
def
|
|
15
|
+
def _matches?(value)
|
|
16
16
|
if !@attribute.is_a?(Array) || !value.kind_of?(Hash) || !value["$elemMatch"].kind_of?(Hash)
|
|
17
17
|
return false
|
|
18
18
|
end
|
|
19
19
|
|
|
20
20
|
return @attribute.any? do |sub_document|
|
|
21
21
|
value["$elemMatch"].all? do |k, v|
|
|
22
|
-
Matchable.matcher(sub_document, k, v).
|
|
22
|
+
Matchable.matcher(sub_document, k, v)._matches?(v)
|
|
23
23
|
end
|
|
24
24
|
end
|
|
25
25
|
end
|
|
@@ -10,12 +10,12 @@ module Mongoid
|
|
|
10
10
|
# non-existence.
|
|
11
11
|
#
|
|
12
12
|
# @example Does anything exist?
|
|
13
|
-
# matcher.
|
|
13
|
+
# matcher._matches?({ :key => 10 })
|
|
14
14
|
#
|
|
15
15
|
# @param [ Hash ] value The values to check.
|
|
16
16
|
#
|
|
17
17
|
# @return [ true, false ] If a value exists.
|
|
18
|
-
def
|
|
18
|
+
def _matches?(value)
|
|
19
19
|
@attribute.nil? != value.values.first
|
|
20
20
|
end
|
|
21
21
|
end
|
data/lib/mongoid/matchable/gt.rb
CHANGED
|
@@ -8,12 +8,12 @@ module Mongoid
|
|
|
8
8
|
# Return true if the attribute is greater than the value.
|
|
9
9
|
#
|
|
10
10
|
# @example Do the values match?
|
|
11
|
-
# matcher.
|
|
11
|
+
# matcher._matches?({ :key => 10 })
|
|
12
12
|
#
|
|
13
13
|
# @param [ Hash ] value The values to check.
|
|
14
14
|
#
|
|
15
15
|
# @return [ true, false ] If a value exists.
|
|
16
|
-
def
|
|
16
|
+
def _matches?(value)
|
|
17
17
|
determine(value, :>)
|
|
18
18
|
rescue ArgumentError
|
|
19
19
|
false
|
|
@@ -8,12 +8,12 @@ module Mongoid
|
|
|
8
8
|
# Return true if the attribute is greater than or equal to the value.
|
|
9
9
|
#
|
|
10
10
|
# @example Do the values match?
|
|
11
|
-
# matcher.
|
|
11
|
+
# matcher._matches?({ :key => 10 })
|
|
12
12
|
#
|
|
13
13
|
# @param [ Hash ] value The values to check.
|
|
14
14
|
#
|
|
15
15
|
# @return [ true, false ] If a value exists.
|
|
16
|
-
def
|
|
16
|
+
def _matches?(value)
|
|
17
17
|
determine(value, :>=)
|
|
18
18
|
rescue ArgumentError
|
|
19
19
|
false
|
data/lib/mongoid/matchable/in.rb
CHANGED
|
@@ -8,12 +8,12 @@ module Mongoid
|
|
|
8
8
|
# Return true if the attribute is in the values.
|
|
9
9
|
#
|
|
10
10
|
# @example Do the values match?
|
|
11
|
-
# matcher.
|
|
11
|
+
# matcher._matches?({ :key => 10 })
|
|
12
12
|
#
|
|
13
13
|
# @param [ Hash ] value The values to check.
|
|
14
14
|
#
|
|
15
15
|
# @return [ true, false ] If a value exists.
|
|
16
|
-
def
|
|
16
|
+
def _matches?(value)
|
|
17
17
|
attribute_array = @attribute.nil? ? [nil] : Array.wrap(@attribute)
|
|
18
18
|
value.values.first.any? do |e|
|
|
19
19
|
attribute_array.any? { |_attribute| e === _attribute }
|
data/lib/mongoid/matchable/lt.rb
CHANGED
|
@@ -8,12 +8,12 @@ module Mongoid
|
|
|
8
8
|
# Return true if the attribute is less than the value.
|
|
9
9
|
#
|
|
10
10
|
# @example Do the values match?
|
|
11
|
-
# matcher.
|
|
11
|
+
# matcher._matches?({ :key => 10 })
|
|
12
12
|
#
|
|
13
13
|
# @param [ Hash ] value The values to check.
|
|
14
14
|
#
|
|
15
15
|
# @return [ true, false ] If a value exists.
|
|
16
|
-
def
|
|
16
|
+
def _matches?(value)
|
|
17
17
|
determine(value, :<)
|
|
18
18
|
rescue ArgumentError
|
|
19
19
|
false
|
|
@@ -8,12 +8,12 @@ module Mongoid
|
|
|
8
8
|
# Return true if the attribute is less than or equal to the value.
|
|
9
9
|
#
|
|
10
10
|
# @example Do the values match?
|
|
11
|
-
# matcher.
|
|
11
|
+
# matcher._matches?({ :key => 10 })
|
|
12
12
|
#
|
|
13
13
|
# @param [ Hash ] value The values to check.
|
|
14
14
|
#
|
|
15
15
|
# @return [ true, false ] If a value exists.
|
|
16
|
-
def
|
|
16
|
+
def _matches?(value)
|
|
17
17
|
determine(value, :<=)
|
|
18
18
|
rescue ArgumentError
|
|
19
19
|
false
|
data/lib/mongoid/matchable/ne.rb
CHANGED
|
@@ -8,12 +8,12 @@ module Mongoid
|
|
|
8
8
|
# Return true if the attribute and first value are not equal.
|
|
9
9
|
#
|
|
10
10
|
# @example Do the values match?
|
|
11
|
-
# matcher.
|
|
11
|
+
# matcher._matches?({ :key => 10 })
|
|
12
12
|
#
|
|
13
13
|
# @param [ Hash ] value The values to check.
|
|
14
14
|
#
|
|
15
15
|
# @return [ true, false ] If a value exists.
|
|
16
|
-
def
|
|
16
|
+
def _matches?(value)
|
|
17
17
|
!super(value.values.first)
|
|
18
18
|
end
|
|
19
19
|
end
|
|
@@ -8,12 +8,12 @@ module Mongoid
|
|
|
8
8
|
# Return true if the attribute is not in the value list.
|
|
9
9
|
#
|
|
10
10
|
# @example Do the values match?
|
|
11
|
-
# matcher.
|
|
11
|
+
# matcher._matches?({ :key => 10 })
|
|
12
12
|
#
|
|
13
13
|
# @param [ Hash ] value The values to check.
|
|
14
14
|
#
|
|
15
15
|
# @return [ true, false ] If a value exists.
|
|
16
|
-
def
|
|
16
|
+
def _matches?(value)
|
|
17
17
|
attribute_array = @attribute.nil? ? [nil] : Array.wrap(@attribute)
|
|
18
18
|
attribute_array.none? { |e| value.values.first.include?(e) }
|
|
19
19
|
end
|
data/lib/mongoid/matchable/or.rb
CHANGED
|
@@ -8,20 +8,20 @@ module Mongoid
|
|
|
8
8
|
# Does the supplied query match the attribute?
|
|
9
9
|
#
|
|
10
10
|
# @example Does this match?
|
|
11
|
-
# matcher.
|
|
11
|
+
# matcher._matches?("$or" => [ { field => value } ])
|
|
12
12
|
#
|
|
13
13
|
# @param [ Array ] conditions The or expression.
|
|
14
14
|
#
|
|
15
15
|
# @return [ true, false ] True if matches, false if not.
|
|
16
16
|
#
|
|
17
17
|
# @since 2.0.0.rc.7
|
|
18
|
-
def
|
|
18
|
+
def _matches?(conditions)
|
|
19
19
|
conditions.each do |condition|
|
|
20
20
|
res = true
|
|
21
21
|
condition.keys.each do |k|
|
|
22
22
|
key = k
|
|
23
23
|
value = condition[k]
|
|
24
|
-
res &&= document.
|
|
24
|
+
res &&= document._matches?(key => value)
|
|
25
25
|
break unless res
|
|
26
26
|
end
|
|
27
27
|
return res if res
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
module Mongoid
|
|
2
|
+
module Matchable
|
|
3
|
+
|
|
4
|
+
# Defines behavior for handling regular expressions in embedded documents.
|
|
5
|
+
class Regexp < Default
|
|
6
|
+
|
|
7
|
+
# Does the supplied query match the attribute?
|
|
8
|
+
#
|
|
9
|
+
# @example Does this match?
|
|
10
|
+
# matcher._matches?(/^Em/)
|
|
11
|
+
# matcher._matches?(BSON::Regex::Raw.new("^Em"))
|
|
12
|
+
#
|
|
13
|
+
# @param [ BSON::Regexp::Raw, Regexp ] regexp The regular expression object.
|
|
14
|
+
#
|
|
15
|
+
# @return [ true, false ] True if matches, false if not.
|
|
16
|
+
#
|
|
17
|
+
# @since 5.2.1
|
|
18
|
+
def _matches?(regexp)
|
|
19
|
+
if native_regexp = regexp.try(:compile)
|
|
20
|
+
super(native_regexp)
|
|
21
|
+
else
|
|
22
|
+
super
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -8,12 +8,12 @@ module Mongoid
|
|
|
8
8
|
# Return true if the attribute size is equal to the first value.
|
|
9
9
|
#
|
|
10
10
|
# @example Do the values match?
|
|
11
|
-
# matcher.
|
|
11
|
+
# matcher._matches?({ :key => 10 })
|
|
12
12
|
#
|
|
13
13
|
# @param [ Hash ] value The values to check.
|
|
14
14
|
#
|
|
15
15
|
# @return [ true, false ] If a value exists.
|
|
16
|
-
def
|
|
16
|
+
def _matches?(value)
|
|
17
17
|
@attribute.size == value.values.first
|
|
18
18
|
end
|
|
19
19
|
end
|
data/lib/mongoid/matchable.rb
CHANGED
|
@@ -13,6 +13,7 @@ require "mongoid/matchable/nin"
|
|
|
13
13
|
require "mongoid/matchable/or"
|
|
14
14
|
require "mongoid/matchable/size"
|
|
15
15
|
require "mongoid/matchable/elem_match"
|
|
16
|
+
require "mongoid/matchable/regexp"
|
|
16
17
|
|
|
17
18
|
module Mongoid
|
|
18
19
|
|
|
@@ -46,26 +47,26 @@ module Mongoid
|
|
|
46
47
|
# MongoDB selector. Used for matching on embedded associations.
|
|
47
48
|
#
|
|
48
49
|
# @example Does the document match?
|
|
49
|
-
# document.
|
|
50
|
+
# document._matches?(:title => { "$in" => [ "test" ] })
|
|
50
51
|
#
|
|
51
52
|
# @param [ Hash ] selector The MongoDB selector.
|
|
52
53
|
#
|
|
53
54
|
# @return [ true, false ] True if matches, false if not.
|
|
54
55
|
#
|
|
55
56
|
# @since 1.0.0
|
|
56
|
-
def
|
|
57
|
+
def _matches?(selector)
|
|
57
58
|
selector.each_pair do |key, value|
|
|
58
59
|
if value.is_a?(Hash)
|
|
59
60
|
value.each do |item|
|
|
60
61
|
if item[0].to_s == "$not".freeze
|
|
61
62
|
item = item[1]
|
|
62
|
-
return false if matcher(self, key, item).
|
|
63
|
+
return false if matcher(self, key, item)._matches?(item)
|
|
63
64
|
else
|
|
64
|
-
return false unless matcher(self, key, Hash[*item]).
|
|
65
|
+
return false unless matcher(self, key, Hash[*item])._matches?(Hash[*item])
|
|
65
66
|
end
|
|
66
67
|
end
|
|
67
68
|
else
|
|
68
|
-
return false unless matcher(self, key, value).
|
|
69
|
+
return false unless matcher(self, key, value)._matches?(value)
|
|
69
70
|
end
|
|
70
71
|
end
|
|
71
72
|
true
|
|
@@ -83,7 +84,7 @@ module Mongoid
|
|
|
83
84
|
#
|
|
84
85
|
# @param [ Document ] document The document to check.
|
|
85
86
|
# @param [ Symbol, String ] key The field name.
|
|
86
|
-
# @param [ Object, Hash ] The value or selector.
|
|
87
|
+
# @param [ Object, Hash ] value The value or selector.
|
|
87
88
|
#
|
|
88
89
|
# @return [ Matcher ] The matcher.
|
|
89
90
|
#
|
|
@@ -104,7 +105,7 @@ module Mongoid
|
|
|
104
105
|
#
|
|
105
106
|
# @param [ Document ] document The document to check.
|
|
106
107
|
# @param [ Symbol, String ] key The field name.
|
|
107
|
-
# @param [ Object, Hash ] The value or selector.
|
|
108
|
+
# @param [ Object, Hash ] value The value or selector.
|
|
108
109
|
#
|
|
109
110
|
# @return [ Matcher ] The matcher.
|
|
110
111
|
#
|
|
@@ -117,6 +118,8 @@ module Mongoid
|
|
|
117
118
|
else
|
|
118
119
|
Default.new(extract_attribute(document, key))
|
|
119
120
|
end
|
|
121
|
+
elsif value.regexp?
|
|
122
|
+
Regexp.new(extract_attribute(document, key))
|
|
120
123
|
else
|
|
121
124
|
case key.to_s
|
|
122
125
|
when "$or" then Or.new(value, document)
|
|
@@ -143,9 +146,13 @@ module Mongoid
|
|
|
143
146
|
# @since 2.2.1
|
|
144
147
|
def extract_attribute(document, key)
|
|
145
148
|
if (key_string = key.to_s) =~ /.+\..+/
|
|
146
|
-
key_string.split('.').inject(document.
|
|
149
|
+
key_string.split('.').inject(document.send(:as_attributes)) do |_attribs, _key|
|
|
147
150
|
if _attribs.is_a?(::Array)
|
|
148
|
-
_attribs.
|
|
151
|
+
if _key =~ /\A\d+\z/ && _attribs.none? {|doc| doc.is_a?(Hash)}
|
|
152
|
+
_attribs.try(:[], _key.to_i)
|
|
153
|
+
else
|
|
154
|
+
_attribs.map { |doc| doc.try(:[], _key) }
|
|
155
|
+
end
|
|
149
156
|
else
|
|
150
157
|
_attribs.try(:[], _key)
|
|
151
158
|
end
|
|
@@ -42,7 +42,7 @@ module Mongoid
|
|
|
42
42
|
#
|
|
43
43
|
# @since 2.1.0
|
|
44
44
|
def atomic_inserts
|
|
45
|
-
{ atomic_insert_modifier => { atomic_position =>
|
|
45
|
+
{ atomic_insert_modifier => { atomic_position => as_attributes }}
|
|
46
46
|
end
|
|
47
47
|
|
|
48
48
|
# Insert the embedded document.
|
|
@@ -76,7 +76,7 @@ module Mongoid
|
|
|
76
76
|
#
|
|
77
77
|
# @since 4.0.0
|
|
78
78
|
def insert_as_root
|
|
79
|
-
collection.insert_one(
|
|
79
|
+
collection.insert_one(as_attributes)
|
|
80
80
|
end
|
|
81
81
|
|
|
82
82
|
# Post process an insert, which sets the new record attribute to false
|
|
@@ -165,8 +165,6 @@ module Mongoid
|
|
|
165
165
|
#
|
|
166
166
|
# @param [ Hash, Array ] attributes The attributes to create with, or an
|
|
167
167
|
# Array of multiple attributes for multiple documents.
|
|
168
|
-
# @param [ Hash ] options A mass-assignment protection options. Supports
|
|
169
|
-
# :as and :without_protection
|
|
170
168
|
#
|
|
171
169
|
# @return [ Document, Array<Document> ] The newly created document(s).
|
|
172
170
|
#
|
|
@@ -28,7 +28,7 @@ module Mongoid
|
|
|
28
28
|
|
|
29
29
|
if fields[field] && fields[field].type == Hash && attributes.key?(field)
|
|
30
30
|
merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
|
|
31
|
-
value = attributes[field].merge(field_and_value_hash[field], &merger)
|
|
31
|
+
value = (attributes[field] || {}).merge(field_and_value_hash[field], &merger)
|
|
32
32
|
process_attribute(field.to_s, value)
|
|
33
33
|
else
|
|
34
34
|
process_attribute(field.to_s, field_and_value_hash[field])
|
data/lib/mongoid/persistable.rb
CHANGED
|
@@ -69,8 +69,6 @@ module Mongoid
|
|
|
69
69
|
# @example Raise the validation error.
|
|
70
70
|
# Person.fail_due_to_validation!(person)
|
|
71
71
|
#
|
|
72
|
-
# @param [ Document ] document The document to fail.
|
|
73
|
-
#
|
|
74
72
|
# @raise [ Errors::Validations ] The validation error.
|
|
75
73
|
#
|
|
76
74
|
# @since 4.0.0
|
|
@@ -83,7 +81,6 @@ module Mongoid
|
|
|
83
81
|
# @example Raise the callback error.
|
|
84
82
|
# Person.fail_due_to_callback!(person, :create!)
|
|
85
83
|
#
|
|
86
|
-
# @param [ Document ] document The document to fail.
|
|
87
84
|
# @param [ Symbol ] method The method being called.
|
|
88
85
|
#
|
|
89
86
|
# @raise [ Errors::Callback ] The callback error.
|
|
@@ -44,7 +44,7 @@ module Mongoid
|
|
|
44
44
|
# @example Create a new persistence context.
|
|
45
45
|
# PersistenceContext.new(model, collection: 'other')
|
|
46
46
|
#
|
|
47
|
-
# @param [ Object ]
|
|
47
|
+
# @param [ Object ] object The class or model instance for which a persistence context
|
|
48
48
|
# should be created.
|
|
49
49
|
# @param [ Hash ] opts The persistence context options.
|
|
50
50
|
#
|
|
@@ -67,8 +67,7 @@ module Mongoid
|
|
|
67
67
|
#
|
|
68
68
|
# @since 6.0.0
|
|
69
69
|
def collection(parent = nil)
|
|
70
|
-
|
|
71
|
-
client[name.to_sym]
|
|
70
|
+
parent ? parent.collection.with(client_options) : client[collection_name.to_sym]
|
|
72
71
|
end
|
|
73
72
|
|
|
74
73
|
# Get the collection name for this persistence context.
|
|
@@ -17,7 +17,6 @@ module Mongoid
|
|
|
17
17
|
# @param [ String, Symbol ] name The name of the relation.
|
|
18
18
|
# @param [ Hash, BSON::ObjectId ] object The id or attributes to use.
|
|
19
19
|
# @param [ Metadata ] metadata The relation's metadata.
|
|
20
|
-
# @param [ true, false ] building If we are in a build operation.
|
|
21
20
|
#
|
|
22
21
|
# @return [ Proxy ] The relation.
|
|
23
22
|
#
|
|
@@ -32,7 +31,7 @@ module Mongoid
|
|
|
32
31
|
# @example Create the relation.
|
|
33
32
|
# person.create_relation(document, metadata)
|
|
34
33
|
#
|
|
35
|
-
# @param [ Document, Array<Document ] object The relation target.
|
|
34
|
+
# @param [ Document, Array<Document> ] object The relation target.
|
|
36
35
|
# @param [ Metadata ] metadata The relation metadata.
|
|
37
36
|
#
|
|
38
37
|
# @return [ Proxy ] The relation.
|
|
@@ -17,8 +17,6 @@ module Mongoid
|
|
|
17
17
|
# name.person.bind(:continue => true)
|
|
18
18
|
# name.person = Person.new
|
|
19
19
|
#
|
|
20
|
-
# @param [ Hash ] options The binding options.
|
|
21
|
-
#
|
|
22
20
|
# @option options [ true, false ] :continue Continue binding the inverse.
|
|
23
21
|
# @option options [ true, false ] :binding Are we in build mode?
|
|
24
22
|
#
|
|
@@ -42,8 +40,6 @@ module Mongoid
|
|
|
42
40
|
# name.person.unbind(:continue => true)
|
|
43
41
|
# name.person = nil
|
|
44
42
|
#
|
|
45
|
-
# @param [ Hash ] options The options to pass through.
|
|
46
|
-
#
|
|
47
43
|
# @option options [ true, false ] :continue Do we continue unbinding?
|
|
48
44
|
#
|
|
49
45
|
# @since 2.0.0.rc.1
|
|
@@ -14,10 +14,6 @@ module Mongoid
|
|
|
14
14
|
# person.addresses.bind_one(address)
|
|
15
15
|
#
|
|
16
16
|
# @param [ Document ] doc The single document to bind.
|
|
17
|
-
# @param [ Hash ] options The binding options.
|
|
18
|
-
#
|
|
19
|
-
# @option options [ true, false ] :continue Continue binding the inverse.
|
|
20
|
-
# @option options [ true, false ] :binding Are we in build mode?
|
|
21
17
|
#
|
|
22
18
|
# @since 2.0.0.rc.1
|
|
23
19
|
def bind_one(doc)
|
|
@@ -32,10 +28,7 @@ module Mongoid
|
|
|
32
28
|
# @example Unbind the document.
|
|
33
29
|
# person.addresses.unbind_one(document)
|
|
34
30
|
#
|
|
35
|
-
# @param [
|
|
36
|
-
#
|
|
37
|
-
# @option options [ true, false ] :continue Continue binding the inverse.
|
|
38
|
-
# @option options [ true, false ] :binding Are we in build mode?
|
|
31
|
+
# @param [ Document ] doc The single document to unbind.
|
|
39
32
|
#
|
|
40
33
|
# @since 2.0.0.rc.1
|
|
41
34
|
def unbind_one(doc)
|
|
@@ -17,11 +17,6 @@ module Mongoid
|
|
|
17
17
|
# person.name.bind(:continue => true)
|
|
18
18
|
# person.name = Name.new
|
|
19
19
|
#
|
|
20
|
-
# @param [ Hash ] options The options to pass through.
|
|
21
|
-
#
|
|
22
|
-
# @option options [ true, false ] :continue Do we continue binding?
|
|
23
|
-
# @option options [ true, false ] :binding Are we in build mode?
|
|
24
|
-
#
|
|
25
20
|
# @since 2.0.0.rc.1
|
|
26
21
|
def bind_one
|
|
27
22
|
target.parentize(base)
|
|
@@ -37,11 +32,6 @@ module Mongoid
|
|
|
37
32
|
# person.name.unbind(:continue => true)
|
|
38
33
|
# person.name = nil
|
|
39
34
|
#
|
|
40
|
-
# @param [ Hash ] options The options to pass through.
|
|
41
|
-
#
|
|
42
|
-
# @option options [ true, false ] :continue Do we continue unbinding?
|
|
43
|
-
# @option options [ true, false ] :binding Are we in build mode?
|
|
44
|
-
#
|
|
45
35
|
# @since 2.0.0.rc.1
|
|
46
36
|
def unbind_one
|
|
47
37
|
binding do
|
|
@@ -13,8 +13,6 @@ module Mongoid
|
|
|
13
13
|
# @example Bind one document.
|
|
14
14
|
# person.posts.bind_one(post)
|
|
15
15
|
#
|
|
16
|
-
# @param [ Document ] doc The single document to bind.
|
|
17
|
-
#
|
|
18
16
|
# @since 2.0.0.rc.1
|
|
19
17
|
def bind_one(doc)
|
|
20
18
|
binding do
|
|
@@ -27,8 +25,6 @@ module Mongoid
|
|
|
27
25
|
# @example Unbind the document.
|
|
28
26
|
# person.posts.unbind_one(document)
|
|
29
27
|
#
|
|
30
|
-
# @param [ Document ] document The document to unbind.
|
|
31
|
-
#
|
|
32
28
|
# @since 2.0.0.rc.1
|
|
33
29
|
def unbind_one(doc)
|
|
34
30
|
binding do
|