mongoid 7.2.6 → 7.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/README.md +1 -1
- data/Rakefile +16 -0
- data/lib/config/locales/en.yml +2 -15
- data/lib/mongoid/association/accessors.rb +1 -1
- data/lib/mongoid/association/constrainable.rb +1 -1
- data/lib/mongoid/association/depending.rb +4 -4
- data/lib/mongoid/association/embedded/batchable.rb +1 -1
- data/lib/mongoid/association/embedded/embedded_in.rb +1 -1
- data/lib/mongoid/association/embedded/embeds_many/proxy.rb +11 -4
- data/lib/mongoid/association/nested/many.rb +1 -1
- data/lib/mongoid/association/nested/one.rb +4 -2
- data/lib/mongoid/association/proxy.rb +7 -2
- data/lib/mongoid/association/referenced/auto_save.rb +2 -2
- data/lib/mongoid/association/referenced/has_many/enumerable.rb +493 -495
- data/lib/mongoid/association/referenced/has_many/proxy.rb +3 -3
- data/lib/mongoid/association/referenced/has_one/nested_builder.rb +2 -2
- data/lib/mongoid/association/relatable.rb +0 -2
- data/lib/mongoid/attributes/projector.rb +120 -0
- data/lib/mongoid/attributes.rb +24 -13
- data/lib/mongoid/cacheable.rb +2 -2
- data/lib/mongoid/clients/factory.rb +22 -8
- data/lib/mongoid/clients.rb +1 -1
- data/lib/mongoid/config/environment.rb +1 -9
- data/lib/mongoid/config.rb +19 -2
- data/lib/mongoid/contextual/aggregable/mongo.rb +10 -8
- data/lib/mongoid/contextual/atomic.rb +2 -7
- data/lib/mongoid/contextual/none.rb +0 -3
- data/lib/mongoid/copyable.rb +1 -1
- data/lib/mongoid/criteria/findable.rb +1 -1
- data/lib/mongoid/criteria/queryable/expandable.rb +0 -24
- data/lib/mongoid/criteria/queryable/extensions/boolean.rb +1 -1
- data/lib/mongoid/criteria/queryable/extensions.rb +0 -4
- data/lib/mongoid/criteria/queryable/mergeable.rb +46 -20
- data/lib/mongoid/criteria/queryable/selectable.rb +10 -10
- data/lib/mongoid/criteria/queryable/storable.rb +4 -4
- data/lib/mongoid/criteria.rb +5 -6
- data/lib/mongoid/document.rb +3 -18
- data/lib/mongoid/errors/delete_restriction.rb +8 -9
- data/lib/mongoid/errors/mongoid_error.rb +1 -1
- data/lib/mongoid/errors.rb +0 -2
- data/lib/mongoid/evolvable.rb +1 -1
- data/lib/mongoid/extensions/boolean.rb +1 -2
- data/lib/mongoid/extensions/false_class.rb +1 -1
- data/lib/mongoid/extensions/hash.rb +2 -2
- data/lib/mongoid/extensions/true_class.rb +1 -1
- data/lib/mongoid/fields.rb +43 -5
- data/lib/mongoid/inspectable.rb +1 -1
- data/lib/mongoid/interceptable.rb +1 -1
- data/lib/mongoid/matcher/bits.rb +41 -0
- data/lib/mongoid/matcher/bits_all_clear.rb +20 -0
- data/lib/mongoid/matcher/bits_all_set.rb +20 -0
- data/lib/mongoid/matcher/bits_any_clear.rb +20 -0
- data/lib/mongoid/matcher/bits_any_set.rb +20 -0
- data/lib/mongoid/matcher/expression.rb +4 -0
- data/lib/mongoid/matcher/field_operator.rb +6 -0
- data/lib/mongoid/matcher/mod.rb +17 -0
- data/lib/mongoid/matcher/type.rb +99 -0
- data/lib/mongoid/matcher.rb +7 -0
- data/lib/mongoid/persistable/deletable.rb +1 -2
- data/lib/mongoid/persistable/destroyable.rb +8 -2
- data/lib/mongoid/persistable/updatable.rb +27 -2
- data/lib/mongoid/persistence_context.rb +1 -3
- data/lib/mongoid/query_cache.rb +36 -40
- data/lib/mongoid/selectable.rb +5 -7
- data/lib/mongoid/shardable.rb +21 -5
- data/lib/mongoid/tasks/database.rb +1 -1
- data/lib/mongoid/touchable.rb +23 -4
- data/lib/mongoid/validatable/associated.rb +1 -1
- data/lib/mongoid/validatable/presence.rb +3 -3
- data/lib/mongoid/validatable/uniqueness.rb +1 -1
- data/lib/mongoid/version.rb +1 -1
- data/lib/mongoid.rb +0 -1
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +1 -1
- data/spec/integration/app_spec.rb +0 -3
- data/spec/integration/associations/embeds_many_spec.rb +44 -0
- data/spec/integration/associations/has_one_spec.rb +48 -0
- data/spec/integration/criteria/date_field_spec.rb +1 -1
- data/spec/integration/document_spec.rb +9 -0
- data/spec/integration/matcher_operator_data/bits_all_clear.yml +159 -0
- data/spec/integration/matcher_operator_data/bits_all_set.yml +159 -0
- data/spec/integration/matcher_operator_data/bits_any_clear.yml +159 -0
- data/spec/integration/matcher_operator_data/bits_any_set.yml +159 -0
- data/spec/integration/matcher_operator_data/comment.yml +22 -0
- data/spec/integration/matcher_operator_data/in.yml +16 -0
- data/spec/integration/matcher_operator_data/mod.yml +55 -0
- data/spec/integration/matcher_operator_data/type.yml +70 -0
- data/spec/integration/matcher_operator_data/type_array.yml +16 -0
- data/spec/integration/matcher_operator_data/type_binary.yml +18 -0
- data/spec/integration/matcher_operator_data/type_boolean.yml +39 -0
- data/spec/integration/matcher_operator_data/type_code.yml +26 -0
- data/spec/integration/matcher_operator_data/type_code_with_scope.yml +26 -0
- data/spec/integration/matcher_operator_data/type_date.yml +39 -0
- data/spec/integration/matcher_operator_data/type_db_pointer.yml +19 -0
- data/spec/integration/matcher_operator_data/type_decimal.yml +40 -0
- data/spec/integration/matcher_operator_data/type_double.yml +15 -0
- data/spec/integration/matcher_operator_data/type_int32.yml +33 -0
- data/spec/integration/matcher_operator_data/type_int64.yml +33 -0
- data/spec/integration/matcher_operator_data/type_max_key.yml +17 -0
- data/spec/integration/matcher_operator_data/type_min_key.yml +17 -0
- data/spec/integration/matcher_operator_data/type_null.yml +23 -0
- data/spec/integration/matcher_operator_data/type_object.yml +23 -0
- data/spec/integration/matcher_operator_data/type_object_id.yml +25 -0
- data/spec/integration/matcher_operator_data/type_regex.yml +44 -0
- data/spec/integration/matcher_operator_data/type_string.yml +15 -0
- data/spec/integration/matcher_operator_data/type_symbol.yml +32 -0
- data/spec/integration/matcher_operator_data/type_timestamp.yml +25 -0
- data/spec/integration/matcher_operator_data/type_undefined.yml +17 -0
- data/spec/integration/stringified_symbol_field_spec.rb +2 -2
- data/spec/lite_spec_helper.rb +2 -0
- data/spec/mongoid/association/depending_spec.rb +391 -352
- data/spec/mongoid/association/embedded/embeds_many/proxy_spec.rb +4 -17
- data/spec/mongoid/association/nested/one_spec.rb +18 -14
- data/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +25 -25
- data/spec/mongoid/association/referenced/belongs_to_query_spec.rb +0 -20
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/binding_spec.rb +1 -1
- data/spec/mongoid/association/referenced/has_many/binding_spec.rb +1 -1
- data/spec/mongoid/association/referenced/has_many/enumerable_spec.rb +1 -1
- data/spec/mongoid/association/referenced/has_many_models.rb +0 -17
- data/spec/mongoid/association/referenced/has_one_models.rb +8 -0
- data/spec/mongoid/atomic/paths_spec.rb +64 -12
- data/spec/mongoid/attributes/projector_data/embedded.yml +105 -0
- data/spec/mongoid/attributes/projector_data/fields.yml +93 -0
- data/spec/mongoid/attributes/projector_spec.rb +41 -0
- data/spec/mongoid/attributes_spec.rb +98 -6
- data/spec/mongoid/clients/factory_spec.rb +51 -9
- data/spec/mongoid/clients/options_spec.rb +3 -11
- data/spec/mongoid/config/environment_spec.rb +8 -86
- data/spec/mongoid/config_spec.rb +32 -0
- data/spec/mongoid/contextual/atomic_spec.rb +25 -64
- data/spec/mongoid/contextual/geo_near_spec.rb +1 -1
- data/spec/mongoid/contextual/mongo_spec.rb +2 -2
- data/spec/mongoid/criteria/modifiable_spec.rb +1 -1
- data/spec/mongoid/criteria/queryable/expandable_spec.rb +0 -73
- data/spec/mongoid/criteria/queryable/extensions/boolean_spec.rb +1 -1
- data/spec/mongoid/criteria/queryable/mergeable_spec.rb +105 -7
- data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +229 -24
- data/spec/mongoid/criteria/queryable/selectable_shared_examples.rb +39 -0
- data/spec/mongoid/criteria/queryable/selectable_spec.rb +1 -565
- data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +590 -0
- data/spec/mongoid/criteria_projection_spec.rb +411 -0
- data/spec/mongoid/criteria_spec.rb +0 -279
- data/spec/mongoid/document_query_spec.rb +0 -51
- data/spec/mongoid/document_spec.rb +14 -34
- data/spec/mongoid/errors/delete_restriction_spec.rb +1 -1
- data/spec/mongoid/errors/mongoid_error_spec.rb +8 -20
- data/spec/mongoid/extensions/false_class_spec.rb +1 -1
- data/spec/mongoid/extensions/string_spec.rb +5 -5
- data/spec/mongoid/extensions/true_class_spec.rb +1 -1
- data/spec/mongoid/fields/localized_spec.rb +4 -4
- data/spec/mongoid/fields_spec.rb +4 -4
- data/spec/mongoid/inspectable_spec.rb +12 -4
- data/spec/mongoid/persistable/deletable_spec.rb +175 -1
- data/spec/mongoid/persistable/destroyable_spec.rb +191 -3
- data/spec/mongoid/persistable/savable_spec.rb +3 -5
- data/spec/mongoid/persistable/updatable_spec.rb +0 -2
- data/spec/mongoid/persistable/upsertable_spec.rb +1 -1
- data/spec/mongoid/persistable_spec.rb +2 -2
- data/spec/mongoid/query_cache_middleware_spec.rb +8 -0
- data/spec/mongoid/query_cache_spec.rb +0 -24
- data/spec/mongoid/reloadable_spec.rb +18 -1
- data/spec/mongoid/shardable_spec.rb +44 -0
- data/spec/mongoid/touchable_spec.rb +104 -16
- data/spec/mongoid/touchable_spec_models.rb +52 -0
- data/spec/mongoid/validatable_spec.rb +1 -1
- data/spec/shared/lib/mrss/cluster_config.rb +3 -8
- data/spec/shared/lib/mrss/constraints.rb +10 -41
- data/spec/shared/lib/mrss/docker_runner.rb +1 -7
- data/spec/shared/lib/mrss/server_version_registry.rb +12 -17
- data/spec/shared/lib/mrss/spec_organizer.rb +1 -18
- data/spec/shared/share/Dockerfile.erb +33 -125
- data/spec/shared/shlib/server.sh +23 -100
- data/spec/shared/shlib/set_env.sh +1 -4
- data/spec/spec_helper.rb +7 -3
- data/spec/support/client_registry.rb +9 -0
- data/spec/support/models/address.rb +0 -4
- data/spec/support/models/bolt.rb +8 -0
- data/spec/support/models/hole.rb +13 -0
- data/spec/support/models/mop.rb +0 -1
- data/spec/support/models/nut.rb +8 -0
- data/spec/support/models/person.rb +6 -9
- data/spec/support/models/sealer.rb +8 -0
- data/spec/support/models/shirt.rb +12 -0
- data/spec/support/models/spacer.rb +8 -0
- data/spec/support/models/threadlocker.rb +8 -0
- data/spec/support/models/washer.rb +8 -0
- data.tar.gz.sig +0 -0
- metadata +609 -545
- metadata.gz.sig +0 -0
- data/lib/mongoid/errors/empty_config_file.rb +0 -26
- data/lib/mongoid/errors/invalid_config_file.rb +0 -26
- data/spec/integration/contextual/empty_spec.rb +0 -142
- data/spec/mongoid/errors/invalid_config_file_spec.rb +0 -32
- data/spec/shared/bin/s3-copy +0 -45
- data/spec/shared/bin/s3-upload +0 -69
- data/spec/shared/lib/mrss/event_subscriber.rb +0 -200
- data/spec/shared/share/haproxy-1.conf +0 -16
- data/spec/shared/share/haproxy-2.conf +0 -17
- data/spec/support/cluster_config.rb +0 -158
@@ -5,544 +5,542 @@ module Mongoid
|
|
5
5
|
module Association
|
6
6
|
module Referenced
|
7
7
|
class HasMany
|
8
|
-
module Targets
|
9
|
-
|
10
|
-
# This class is the wrapper for all referenced associations that have a
|
11
|
-
# target that can be a criteria or array of _loaded documents. This
|
12
|
-
# handles both cases or a combination of the two.
|
13
|
-
class Enumerable
|
14
|
-
extend Forwardable
|
15
|
-
include ::Enumerable
|
16
|
-
|
17
|
-
# The three main instance variables are collections of documents.
|
18
|
-
#
|
19
|
-
# @attribute [rw] _added Documents that have been appended.
|
20
|
-
# @attribute [rw] _loaded Persisted documents that have been _loaded.
|
21
|
-
# @attribute [rw] _unloaded A criteria representing persisted docs.
|
22
|
-
attr_accessor :_added, :_loaded, :_unloaded
|
23
|
-
|
24
|
-
def_delegators [], :is_a?, :kind_of?
|
25
|
-
|
26
|
-
# Check if the enumerable is equal to the other object.
|
27
|
-
#
|
28
|
-
# @example Check equality.
|
29
|
-
# enumerable == []
|
30
|
-
#
|
31
|
-
# @param [ Enumerable ] other The other enumerable.
|
32
|
-
#
|
33
|
-
# @return [ true, false ] If the objects are equal.
|
34
|
-
#
|
35
|
-
# @since 2.1.0
|
36
|
-
def ==(other)
|
37
|
-
return false unless other.respond_to?(:entries)
|
38
|
-
entries == other.entries
|
39
|
-
end
|
40
8
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
9
|
+
# This class is the wrapper for all referenced associations that have a
|
10
|
+
# target that can be a criteria or array of _loaded documents. This
|
11
|
+
# handles both cases or a combination of the two.
|
12
|
+
class Enumerable
|
13
|
+
extend Forwardable
|
14
|
+
include ::Enumerable
|
15
|
+
|
16
|
+
# The three main instance variables are collections of documents.
|
17
|
+
#
|
18
|
+
# @attribute [rw] _added Documents that have been appended.
|
19
|
+
# @attribute [rw] _loaded Persisted documents that have been _loaded.
|
20
|
+
# @attribute [rw] _unloaded A criteria representing persisted docs.
|
21
|
+
attr_accessor :_added, :_loaded, :_unloaded
|
22
|
+
|
23
|
+
def_delegators [], :is_a?, :kind_of?
|
24
|
+
|
25
|
+
# Check if the enumerable is equal to the other object.
|
26
|
+
#
|
27
|
+
# @example Check equality.
|
28
|
+
# enumerable == []
|
29
|
+
#
|
30
|
+
# @param [ Enumerable ] other The other enumerable.
|
31
|
+
#
|
32
|
+
# @return [ true, false ] If the objects are equal.
|
33
|
+
#
|
34
|
+
# @since 2.1.0
|
35
|
+
def ==(other)
|
36
|
+
return false unless other.respond_to?(:entries)
|
37
|
+
entries == other.entries
|
38
|
+
end
|
55
39
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
40
|
+
# Check equality of the enumerable against the provided object for case
|
41
|
+
# statements.
|
42
|
+
#
|
43
|
+
# @example Check case equality.
|
44
|
+
# enumerable === Array
|
45
|
+
#
|
46
|
+
# @param [ Object ] other The object to check.
|
47
|
+
#
|
48
|
+
# @return [ true, false ] If the objects are equal in a case.
|
49
|
+
#
|
50
|
+
# @since 3.1.4
|
51
|
+
def ===(other)
|
52
|
+
other.class == Class ? (Array == other || Enumerable == other) : self == other
|
53
|
+
end
|
70
54
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
#
|
86
|
-
# @since 2.1.0
|
87
|
-
def clear
|
88
|
-
if block_given?
|
89
|
-
in_memory { |doc| yield(doc) }
|
90
|
-
end
|
91
|
-
_loaded.clear and _added.clear
|
92
|
-
end
|
55
|
+
# Append a document to the enumerable.
|
56
|
+
#
|
57
|
+
# @example Append the document.
|
58
|
+
# enumerable << document
|
59
|
+
#
|
60
|
+
# @param [ Document ] document The document to append.
|
61
|
+
#
|
62
|
+
# @return [ Document ] The document.
|
63
|
+
#
|
64
|
+
# @since 2.1.0
|
65
|
+
def <<(document)
|
66
|
+
_added[document._id] = document
|
67
|
+
self
|
68
|
+
end
|
93
69
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
70
|
+
alias :push :<<
|
71
|
+
|
72
|
+
# Clears out all the documents in this enumerable. If passed a block it
|
73
|
+
# will yield to each document that is in memory.
|
74
|
+
#
|
75
|
+
# @example Clear out the enumerable.
|
76
|
+
# enumerable.clear
|
77
|
+
#
|
78
|
+
# @example Clear out the enumerable with a block.
|
79
|
+
# enumerable.clear do |doc|
|
80
|
+
# doc.unbind
|
81
|
+
# end
|
82
|
+
#
|
83
|
+
# @return [ Array<Document> ] The cleared out _added docs.
|
84
|
+
#
|
85
|
+
# @since 2.1.0
|
86
|
+
def clear
|
87
|
+
if block_given?
|
88
|
+
in_memory { |doc| yield(doc) }
|
106
89
|
end
|
90
|
+
_loaded.clear and _added.clear
|
91
|
+
end
|
92
|
+
|
93
|
+
# Clones each document in the enumerable.
|
94
|
+
#
|
95
|
+
# @note This loads all documents into memory.
|
96
|
+
#
|
97
|
+
# @example Clone the enumerable.
|
98
|
+
# enumerable.clone
|
99
|
+
#
|
100
|
+
# @return [ Array<Document> ] An array clone of the enumerable.
|
101
|
+
#
|
102
|
+
# @since 2.1.6
|
103
|
+
def clone
|
104
|
+
collect { |doc| doc.clone }
|
105
|
+
end
|
107
106
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
end
|
107
|
+
# Delete the supplied document from the enumerable.
|
108
|
+
#
|
109
|
+
# @example Delete the document.
|
110
|
+
# enumerable.delete(document)
|
111
|
+
#
|
112
|
+
# @param [ Document ] document The document to delete.
|
113
|
+
#
|
114
|
+
# @return [ Document ] The deleted document.
|
115
|
+
#
|
116
|
+
# @since 2.1.0
|
117
|
+
def delete(document)
|
118
|
+
doc = (_loaded.delete(document._id) || _added.delete(document._id))
|
119
|
+
unless doc
|
120
|
+
if _unloaded && _unloaded.where(_id: document._id).exists?
|
121
|
+
yield(document) if block_given?
|
122
|
+
return document
|
125
123
|
end
|
126
|
-
yield(doc) if block_given?
|
127
|
-
doc
|
128
124
|
end
|
125
|
+
yield(doc) if block_given?
|
126
|
+
doc
|
127
|
+
end
|
129
128
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
end
|
150
|
-
self
|
129
|
+
# Deletes every document in the enumerable for where the block returns
|
130
|
+
# true.
|
131
|
+
#
|
132
|
+
# @note This operation loads all documents from the database.
|
133
|
+
#
|
134
|
+
# @example Delete all matching documents.
|
135
|
+
# enumerable.delete_if do |doc|
|
136
|
+
# dod._id == _id
|
137
|
+
# end
|
138
|
+
#
|
139
|
+
# @return [ Array<Document> ] The remaining docs.
|
140
|
+
#
|
141
|
+
# @since 2.1.0
|
142
|
+
def delete_if(&block)
|
143
|
+
load_all!
|
144
|
+
deleted = in_memory.select(&block)
|
145
|
+
deleted.each do |doc|
|
146
|
+
_loaded.delete(doc._id)
|
147
|
+
_added.delete(doc._id)
|
151
148
|
end
|
149
|
+
self
|
150
|
+
end
|
152
151
|
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
end
|
188
|
-
else
|
189
|
-
unloaded_documents.each do |doc|
|
190
|
-
document = _added.delete(doc._id) || _loaded.delete(doc._id) || doc
|
191
|
-
_loaded[document._id] = document
|
192
|
-
set_base(document)
|
193
|
-
yield(document)
|
194
|
-
end
|
152
|
+
# Iterating over this enumerable has to handle a few different
|
153
|
+
# scenarios.
|
154
|
+
#
|
155
|
+
# If the enumerable has its criteria _loaded into memory then it yields
|
156
|
+
# to all the _loaded docs and all the _added docs.
|
157
|
+
#
|
158
|
+
# If the enumerable has not _loaded the criteria then it iterates over
|
159
|
+
# the cursor while loading the documents and then iterates over the
|
160
|
+
# _added docs.
|
161
|
+
#
|
162
|
+
# If no block is passed then it returns an enumerator containing all
|
163
|
+
# docs.
|
164
|
+
#
|
165
|
+
# @example Iterate over the enumerable.
|
166
|
+
# enumerable.each do |doc|
|
167
|
+
# puts doc
|
168
|
+
# end
|
169
|
+
#
|
170
|
+
# @example return an enumerator containing all the docs
|
171
|
+
#
|
172
|
+
# a = enumerable.each
|
173
|
+
#
|
174
|
+
# @return [ true ] That the enumerable is now _loaded.
|
175
|
+
#
|
176
|
+
# @since 2.1.0
|
177
|
+
def each
|
178
|
+
unless block_given?
|
179
|
+
return to_enum
|
180
|
+
end
|
181
|
+
if _loaded?
|
182
|
+
_loaded.each_pair do |id, doc|
|
183
|
+
document = _added.delete(doc._id) || doc
|
184
|
+
set_base(document)
|
185
|
+
yield(document)
|
195
186
|
end
|
196
|
-
|
197
|
-
|
187
|
+
else
|
188
|
+
unloaded_documents.each do |doc|
|
189
|
+
document = _added.delete(doc._id) || _loaded.delete(doc._id) || doc
|
190
|
+
_loaded[document._id] = document
|
191
|
+
set_base(document)
|
192
|
+
yield(document)
|
198
193
|
end
|
199
|
-
@executed = true
|
200
194
|
end
|
201
|
-
|
202
|
-
|
203
|
-
# whether or not it is _loaded.
|
204
|
-
#
|
205
|
-
# @example Is the enumerable empty?
|
206
|
-
# enumerable.empty?
|
207
|
-
#
|
208
|
-
# @return [ true, false ] If the enumerable is empty.
|
209
|
-
#
|
210
|
-
# @since 2.1.0
|
211
|
-
def empty?
|
212
|
-
if _loaded?
|
213
|
-
in_memory.count == 0
|
214
|
-
else
|
215
|
-
_unloaded.count + _added.count == 0
|
216
|
-
end
|
195
|
+
_added.each_pair do |id, doc|
|
196
|
+
yield(doc)
|
217
197
|
end
|
198
|
+
@executed = true
|
199
|
+
end
|
218
200
|
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
# either the paramater or the block is delegated to the standard
|
234
|
-
# library Enumerable module.
|
235
|
-
#
|
236
|
-
# Note that when Enumerable's any? method is invoked with both
|
237
|
-
# a block and a pattern, it only uses the pattern.
|
238
|
-
#
|
239
|
-
# @param [ Object ] condition The condition that documents
|
240
|
-
# must satisfy. See Enumerable documentation for details.
|
241
|
-
#
|
242
|
-
# @return [ true, false ] If the association has any documents.
|
243
|
-
def any?(*args)
|
244
|
-
return super if args.any? || block_given?
|
245
|
-
|
246
|
-
if _loaded?
|
247
|
-
in_memory.length > 0
|
248
|
-
else
|
249
|
-
_unloaded.exists? || _added.length > 0
|
250
|
-
end
|
201
|
+
# Is the enumerable empty? Will determine if the count is zero based on
|
202
|
+
# whether or not it is _loaded.
|
203
|
+
#
|
204
|
+
# @example Is the enumerable empty?
|
205
|
+
# enumerable.empty?
|
206
|
+
#
|
207
|
+
# @return [ true, false ] If the enumerable is empty.
|
208
|
+
#
|
209
|
+
# @since 2.1.0
|
210
|
+
def empty?
|
211
|
+
if _loaded?
|
212
|
+
in_memory.count == 0
|
213
|
+
else
|
214
|
+
_unloaded.count + _added.count == 0
|
251
215
|
end
|
216
|
+
end
|
252
217
|
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
218
|
+
# Returns whether the association has any documents, optionally
|
219
|
+
# subject to the provided filters.
|
220
|
+
#
|
221
|
+
# This method returns true if the association has any persisted
|
222
|
+
# documents and if it has any not yet persisted documents.
|
223
|
+
#
|
224
|
+
# If the association is already loaded, this method inspects the
|
225
|
+
# loaded documents and does not query the database. If the
|
226
|
+
# association is not loaded, the argument-less and block-less
|
227
|
+
# version does not load the association; the other versions
|
228
|
+
# (that delegate to Enumerable) may or may not load the association
|
229
|
+
# completely depending on whether it is iterated to completion.
|
230
|
+
#
|
231
|
+
# This method can take a parameter and a block. The behavior with
|
232
|
+
# either the paramater or the block is delegated to the standard
|
233
|
+
# library Enumerable module.
|
234
|
+
#
|
235
|
+
# Note that when Enumerable's any? method is invoked with both
|
236
|
+
# a block and a pattern, it only uses the pattern.
|
237
|
+
#
|
238
|
+
# @param [ Object ] condition The condition that documents
|
239
|
+
# must satisfy. See Enumerable documentation for details.
|
240
|
+
#
|
241
|
+
# @return [ true, false ] If the association has any documents.
|
242
|
+
def any?(*args)
|
243
|
+
return super if args.any? || block_given?
|
244
|
+
|
245
|
+
if _loaded?
|
246
|
+
in_memory.length > 0
|
247
|
+
else
|
248
|
+
_unloaded.exists? || _added.length > 0
|
277
249
|
end
|
250
|
+
end
|
278
251
|
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
252
|
+
# Get the first document in the enumerable. Will check the persisted
|
253
|
+
# documents first. Does not load the entire enumerable.
|
254
|
+
#
|
255
|
+
# @example Get the first document.
|
256
|
+
# enumerable.first
|
257
|
+
#
|
258
|
+
# @note Automatically adding a sort on _id when no other sort is
|
259
|
+
# defined on the criteria has the potential to cause bad performance issues.
|
260
|
+
# If you experience unexpected poor performance when using #first or #last,
|
261
|
+
# use the option { id_sort: :none }.
|
262
|
+
# Be aware that #first/#last won't guarantee order in this case.
|
263
|
+
#
|
264
|
+
# @param [ Hash ] opts The options for the query returning the first document.
|
265
|
+
#
|
266
|
+
# @option opts [ :none ] :id_sort Don't apply a sort on _id.
|
267
|
+
#
|
268
|
+
# @return [ Document ] The first document found.
|
269
|
+
#
|
270
|
+
# @since 2.1.0
|
271
|
+
def first(opts = {})
|
272
|
+
_loaded.try(:values).try(:first) ||
|
273
|
+
_added[(ul = _unloaded.try(:first, opts)).try(:_id)] ||
|
274
|
+
ul ||
|
275
|
+
_added.values.try(:first)
|
276
|
+
end
|
277
|
+
|
278
|
+
# Initialize the new enumerable either with a criteria or an array.
|
279
|
+
#
|
280
|
+
# @example Initialize the enumerable with a criteria.
|
281
|
+
# Enumberable.new(Post.where(:person_id => id))
|
282
|
+
#
|
283
|
+
# @example Initialize the enumerable with an array.
|
284
|
+
# Enumerable.new([ post ])
|
285
|
+
#
|
286
|
+
# @param [ Criteria, Array<Document> ] target The wrapped object.
|
287
|
+
#
|
288
|
+
# @since 2.1.0
|
289
|
+
def initialize(target, base = nil, association = nil)
|
290
|
+
@_base = base
|
291
|
+
@_association = association
|
292
|
+
if target.is_a?(Criteria)
|
293
|
+
@_added, @executed, @_loaded, @_unloaded = {}, false, {}, target
|
294
|
+
else
|
295
|
+
@_added, @executed = {}, true
|
296
|
+
@_loaded = target.inject({}) do |_target, doc|
|
297
|
+
_target[doc._id] = doc if doc
|
298
|
+
_target
|
301
299
|
end
|
302
300
|
end
|
301
|
+
end
|
303
302
|
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
303
|
+
# Does the target include the provided document?
|
304
|
+
#
|
305
|
+
# @example Does the target include the document?
|
306
|
+
# enumerable.include?(document)
|
307
|
+
#
|
308
|
+
# @param [ Document ] doc The document to check.
|
309
|
+
#
|
310
|
+
# @return [ true, false ] If the document is in the target.
|
311
|
+
#
|
312
|
+
# @since 3.0.0
|
313
|
+
def include?(doc)
|
314
|
+
return super unless _unloaded
|
315
|
+
_unloaded.where(_id: doc._id).exists? || _added.has_key?(doc._id)
|
316
|
+
end
|
318
317
|
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
318
|
+
# Inspection will just inspect the entries for nice array-style
|
319
|
+
# printing.
|
320
|
+
#
|
321
|
+
# @example Inspect the enumerable.
|
322
|
+
# enumerable.inspect
|
323
|
+
#
|
324
|
+
# @return [ String ] The inspected enum.
|
325
|
+
#
|
326
|
+
# @since 2.1.0
|
327
|
+
def inspect
|
328
|
+
entries.inspect
|
329
|
+
end
|
331
330
|
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
end
|
331
|
+
# Return all the documents in the enumerable that have been _loaded or
|
332
|
+
# _added.
|
333
|
+
#
|
334
|
+
# @note When passed a block it yields to each document.
|
335
|
+
#
|
336
|
+
# @example Get the in memory docs.
|
337
|
+
# enumerable.in_memory
|
338
|
+
#
|
339
|
+
# @return [ Array<Document> ] The in memory docs.
|
340
|
+
#
|
341
|
+
# @since 2.1.0
|
342
|
+
def in_memory
|
343
|
+
docs = (_loaded.values + _added.values)
|
344
|
+
docs.each do |doc|
|
345
|
+
yield(doc) if block_given?
|
348
346
|
end
|
347
|
+
end
|
349
348
|
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
349
|
+
# Get the last document in the enumerable. Will check the new
|
350
|
+
# documents first. Does not load the entire enumerable.
|
351
|
+
#
|
352
|
+
# @example Get the last document.
|
353
|
+
# enumerable.last
|
354
|
+
#
|
355
|
+
# @note Automatically adding a sort on _id when no other sort is
|
356
|
+
# defined on the criteria has the potential to cause bad performance issues.
|
357
|
+
# If you experience unexpected poor performance when using #first or #last,
|
358
|
+
# use the option { id_sort: :none }.
|
359
|
+
# Be aware that #first/#last won't guarantee order in this case.
|
360
|
+
#
|
361
|
+
# @param [ Hash ] opts The options for the query returning the first document.
|
362
|
+
#
|
363
|
+
# @option opts [ :none ] :id_sort Don't apply a sort on _id.
|
364
|
+
#
|
365
|
+
# @return [ Document ] The last document found.
|
366
|
+
#
|
367
|
+
# @since 2.1.0
|
368
|
+
def last(opts = {})
|
369
|
+
_added.values.try(:last) ||
|
370
|
+
_loaded.try(:values).try(:last) ||
|
371
|
+
_added[(ul = _unloaded.try(:last, opts)).try(:_id)] ||
|
372
|
+
ul
|
373
|
+
end
|
375
374
|
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
375
|
+
# Loads all the documents in the enumerable from the database.
|
376
|
+
#
|
377
|
+
# @example Load all the documents.
|
378
|
+
# enumerable.load_all!
|
379
|
+
#
|
380
|
+
# @return [ true ] That the enumerable is _loaded.
|
381
|
+
#
|
382
|
+
# @since 2.1.0
|
383
|
+
alias :load_all! :entries
|
384
|
+
|
385
|
+
# Has the enumerable been _loaded? This will be true if the criteria has
|
386
|
+
# been executed or we manually load the entire thing.
|
387
|
+
#
|
388
|
+
# @example Is the enumerable _loaded?
|
389
|
+
# enumerable._loaded?
|
390
|
+
#
|
391
|
+
# @return [ true, false ] If the enumerable has been _loaded.
|
392
|
+
#
|
393
|
+
# @since 2.1.0
|
394
|
+
def _loaded?
|
395
|
+
!!@executed
|
396
|
+
end
|
398
397
|
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
398
|
+
# Provides the data needed to Marshal.dump an enumerable proxy.
|
399
|
+
#
|
400
|
+
# @example Dump the proxy.
|
401
|
+
# Marshal.dump(proxy)
|
402
|
+
#
|
403
|
+
# @return [ Array<Object> ] The dumped data.
|
404
|
+
#
|
405
|
+
# @since 3.0.15
|
406
|
+
def marshal_dump
|
407
|
+
[_added, _loaded, _unloaded, @executed]
|
408
|
+
end
|
410
409
|
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
410
|
+
# Loads the data needed to Marshal.load an enumerable proxy.
|
411
|
+
#
|
412
|
+
# @example Load the proxy.
|
413
|
+
# Marshal.load(proxy)
|
414
|
+
#
|
415
|
+
# @return [ Array<Object> ] The dumped data.
|
416
|
+
#
|
417
|
+
# @since 3.0.15
|
418
|
+
def marshal_load(data)
|
419
|
+
@_added, @_loaded, @_unloaded, @executed = data
|
420
|
+
end
|
422
421
|
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
422
|
+
# Reset the enumerable back to its persisted state.
|
423
|
+
#
|
424
|
+
# @example Reset the enumerable.
|
425
|
+
# enumerable.reset
|
426
|
+
#
|
427
|
+
# @return [ false ] Always false.
|
428
|
+
#
|
429
|
+
# @since 2.1.0
|
430
|
+
def reset
|
431
|
+
_loaded.clear
|
432
|
+
_added.clear
|
433
|
+
@executed = false
|
434
|
+
end
|
436
435
|
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
436
|
+
# Resets the underlying unloaded criteria object with a new one. Used
|
437
|
+
# my HABTM associations to keep the underlying array in sync.
|
438
|
+
#
|
439
|
+
# @example Reset the unloaded documents.
|
440
|
+
# enumerable.reset_unloaded(criteria)
|
441
|
+
#
|
442
|
+
# @param [ Criteria ] criteria The criteria to replace with.
|
443
|
+
#
|
444
|
+
# @since 3.0.14
|
445
|
+
def reset_unloaded(criteria)
|
446
|
+
@_unloaded = criteria if _unloaded.is_a?(Criteria)
|
447
|
+
end
|
449
448
|
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
449
|
+
# Does this enumerable respond to the provided method?
|
450
|
+
#
|
451
|
+
# @example Does the enumerable respond to the method?
|
452
|
+
# enumerable.respond_to?(:sum)
|
453
|
+
#
|
454
|
+
# @param [ String, Symbol ] name The name of the method.
|
455
|
+
# @param [ true, false ] include_private Whether to include private
|
456
|
+
# methods.
|
457
|
+
#
|
458
|
+
# @return [ true, false ] Whether the enumerable responds.
|
459
|
+
#
|
460
|
+
# @since 2.1.0
|
461
|
+
def respond_to?(name, include_private = false)
|
462
|
+
[].respond_to?(name, include_private) || super
|
463
|
+
end
|
465
464
|
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
end
|
465
|
+
# Gets the total size of this enumerable. This is a combination of all
|
466
|
+
# the persisted and unpersisted documents.
|
467
|
+
#
|
468
|
+
# @example Get the size.
|
469
|
+
# enumerable.size
|
470
|
+
#
|
471
|
+
# @return [ Integer ] The size of the enumerable.
|
472
|
+
#
|
473
|
+
# @since 2.1.0
|
474
|
+
def size
|
475
|
+
count = (_unloaded ? _unloaded.count : _loaded.count)
|
476
|
+
if count.zero?
|
477
|
+
count + _added.count
|
478
|
+
else
|
479
|
+
count + _added.values.count { |d| d.new_record? }
|
482
480
|
end
|
481
|
+
end
|
483
482
|
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
483
|
+
alias :length :size
|
484
|
+
|
485
|
+
# Send #to_json to the entries.
|
486
|
+
#
|
487
|
+
# @example Get the enumerable as json.
|
488
|
+
# enumerable.to_json
|
489
|
+
#
|
490
|
+
# @param [ Hash ] options Optional parameters.
|
491
|
+
#
|
492
|
+
# @return [ String ] The entries all _loaded as a string.
|
493
|
+
#
|
494
|
+
# @since 2.2.0
|
495
|
+
def to_json(options = {})
|
496
|
+
entries.to_json(options)
|
497
|
+
end
|
499
498
|
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
499
|
+
# Send #as_json to the entries, without encoding.
|
500
|
+
#
|
501
|
+
# @example Get the enumerable as json.
|
502
|
+
# enumerable.as_json
|
503
|
+
#
|
504
|
+
# @param [ Hash ] options Optional parameters.
|
505
|
+
#
|
506
|
+
# @return [ Hash ] The entries all _loaded as a hash.
|
507
|
+
#
|
508
|
+
# @since 2.2.0
|
509
|
+
def as_json(options = {})
|
510
|
+
entries.as_json(options)
|
511
|
+
end
|
513
512
|
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
513
|
+
# Return all the unique documents in the enumerable.
|
514
|
+
#
|
515
|
+
# @note This operation loads all documents from the database.
|
516
|
+
#
|
517
|
+
# @example Get all the unique documents.
|
518
|
+
# enumerable.uniq
|
519
|
+
#
|
520
|
+
# @return [ Array<Document> ] The unique documents.
|
521
|
+
#
|
522
|
+
# @since 2.1.0
|
523
|
+
def uniq
|
524
|
+
entries.uniq
|
525
|
+
end
|
527
526
|
|
528
|
-
|
527
|
+
private
|
529
528
|
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
end
|
529
|
+
def set_base(document)
|
530
|
+
if @_association.is_a?(Referenced::HasMany)
|
531
|
+
document.set_relation(@_association.inverse, @_base) if @_association
|
534
532
|
end
|
533
|
+
end
|
535
534
|
|
536
|
-
|
537
|
-
|
538
|
-
|
535
|
+
def method_missing(name, *args, &block)
|
536
|
+
entries.send(name, *args, &block)
|
537
|
+
end
|
539
538
|
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
end
|
539
|
+
def unloaded_documents
|
540
|
+
if _unloaded.selector._mongoid_unsatisfiable_criteria?
|
541
|
+
[]
|
542
|
+
else
|
543
|
+
_unloaded
|
546
544
|
end
|
547
545
|
end
|
548
546
|
end
|