mongoid 7.2.1 → 7.3.1
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.tar.gz.sig +0 -0
- data/README.md +1 -1
- data/Rakefile +16 -0
- data/lib/config/locales/en.yml +2 -2
- data/lib/mongoid/association/accessors.rb +13 -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 +10 -3
- data/lib/mongoid/association/nested/many.rb +1 -1
- data/lib/mongoid/association/nested/one.rb +4 -2
- data/lib/mongoid/association/proxy.rb +6 -1
- 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 +2 -2
- data/lib/mongoid/association/referenced/has_one/nested_builder.rb +2 -2
- data/lib/mongoid/attributes.rb +32 -14
- data/lib/mongoid/attributes/projector.rb +120 -0
- data/lib/mongoid/cacheable.rb +2 -2
- data/lib/mongoid/clients.rb +1 -1
- data/lib/mongoid/clients/factory.rb +22 -8
- data/lib/mongoid/config.rb +19 -2
- data/lib/mongoid/contextual/aggregable/mongo.rb +10 -8
- data/lib/mongoid/copyable.rb +1 -1
- data/lib/mongoid/criteria.rb +4 -5
- data/lib/mongoid/criteria/findable.rb +1 -1
- data/lib/mongoid/criteria/queryable/expandable.rb +0 -24
- data/lib/mongoid/criteria/queryable/extensions.rb +0 -4
- data/lib/mongoid/criteria/queryable/extensions/boolean.rb +1 -1
- data/lib/mongoid/criteria/queryable/mergeable.rb +46 -20
- data/lib/mongoid/criteria/queryable/selectable.rb +8 -8
- data/lib/mongoid/document.rb +1 -15
- data/lib/mongoid/errors/delete_restriction.rb +8 -9
- data/lib/mongoid/errors/mongoid_error.rb +1 -1
- 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/matcher.rb +26 -43
- 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/elem_match.rb +2 -1
- data/lib/mongoid/matcher/expression.rb +9 -14
- data/lib/mongoid/matcher/field_expression.rb +4 -5
- 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/persistable/deletable.rb +1 -2
- data/lib/mongoid/persistable/destroyable.rb +8 -2
- data/lib/mongoid/persistable/updatable.rb +27 -2
- data/lib/mongoid/query_cache.rb +35 -29
- data/lib/mongoid/reloadable.rb +5 -0
- data/lib/mongoid/selectable.rb +5 -7
- data/lib/mongoid/shardable.rb +21 -5
- 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/rails/generators/mongoid/config/config_generator.rb +8 -1
- data/lib/rails/generators/mongoid/config/templates/mongoid.yml +1 -1
- data/spec/integration/app_spec.rb +139 -82
- 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 +30 -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/elem_match.yml +46 -0
- data/spec/integration/matcher_operator_data/implicit_traversal.yml +96 -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/lite_spec_helper.rb +4 -3
- data/spec/mongoid/association/depending_spec.rb +391 -352
- data/spec/mongoid/association/nested/one_spec.rb +18 -14
- data/spec/mongoid/association/referenced/belongs_to/proxy_spec.rb +25 -8
- 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_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 +333 -0
- data/spec/mongoid/clients/factory_spec.rb +48 -0
- data/spec/mongoid/config_spec.rb +32 -0
- data/spec/mongoid/contextual/atomic_spec.rb +17 -4
- 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 -275
- data/spec/mongoid/document_fields_spec.rb +26 -0
- data/spec/mongoid/document_query_spec.rb +51 -0
- data/spec/mongoid/document_spec.rb +13 -13
- data/spec/mongoid/errors/delete_restriction_spec.rb +1 -1
- data/spec/mongoid/errors/mongoid_error_spec.rb +20 -8
- 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/matcher/extract_attribute_data/numeric_keys.yml +104 -0
- data/spec/mongoid/matcher/extract_attribute_data/traversal.yml +68 -88
- data/spec/mongoid/matcher/extract_attribute_spec.rb +3 -13
- 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/settable_spec.rb +30 -0
- 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/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/bin/get-mongodb-download-url +17 -0
- data/spec/shared/bin/s3-copy +45 -0
- data/spec/shared/bin/s3-upload +69 -0
- data/spec/shared/lib/mrss/cluster_config.rb +19 -4
- data/spec/shared/lib/mrss/constraints.rb +46 -8
- data/spec/shared/lib/mrss/docker_runner.rb +10 -1
- data/spec/shared/lib/mrss/lite_constraints.rb +16 -0
- data/spec/shared/lib/mrss/server_version_registry.rb +79 -33
- data/spec/shared/lib/mrss/spec_organizer.rb +32 -2
- data/spec/shared/lib/mrss/utils.rb +15 -0
- data/spec/shared/share/Dockerfile.erb +122 -29
- data/spec/shared/share/haproxy-1.conf +16 -0
- data/spec/shared/share/haproxy-2.conf +17 -0
- data/spec/shared/shlib/server.sh +58 -11
- data/spec/shared/shlib/set_env.sh +4 -1
- data/spec/spec_helper.rb +7 -3
- data/spec/support/client_registry.rb +9 -0
- data/spec/support/models/bolt.rb +8 -0
- data/spec/support/models/hole.rb +13 -0
- data/spec/support/models/mop.rb +9 -0
- data/spec/support/models/nut.rb +8 -0
- data/spec/support/models/person.rb +6 -0
- 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/spec/support/spec_config.rb +8 -0
- metadata +636 -528
- metadata.gz.sig +0 -0
- data/spec/support/cluster_config.rb +0 -158
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 408fcc1f85dce0c9a16140dda363526896fe1fb37f627e33f15ef6d1a2628de0
|
4
|
+
data.tar.gz: '084168385d0912909c5292100334c8eb7edf162d72bcd1cd51673fd8140013ca'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 193c335677afc72c56eebc7d64a0167cb4c9112e7b0a81baa39d6433a7c8b04d1a456e451777823c5678219bd73ad46198c8bd650e0cfb89b1ee87e77d4b62d4
|
7
|
+
data.tar.gz: b994ad71c11e4c9f94074fc082c91ffc479146bb0a9af87b28a4c7fa1bc338f737cb902d4098d50241ffb62cf9e6ba1deb2cdd3ffb951e1afa09cf3c02450e2c
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
data/README.md
CHANGED
data/Rakefile
CHANGED
@@ -66,6 +66,22 @@ task :ci do
|
|
66
66
|
spec_organizer.run
|
67
67
|
end
|
68
68
|
|
69
|
+
task :bucket, %i(buckets) do |task, args|
|
70
|
+
buckets = args[:buckets]
|
71
|
+
buckets = if buckets.nil? || buckets.empty?
|
72
|
+
[nil]
|
73
|
+
else
|
74
|
+
buckets.split(':').map do |bucket|
|
75
|
+
if bucket.empty?
|
76
|
+
nil
|
77
|
+
else
|
78
|
+
bucket.to_sym
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
spec_organizer.run_buckets(*buckets)
|
83
|
+
end
|
84
|
+
|
69
85
|
task :default => :spec
|
70
86
|
|
71
87
|
desc "Generate all documentation"
|
data/lib/config/locales/en.yml
CHANGED
@@ -578,9 +578,9 @@ en:
|
|
578
578
|
resolution: "Try persisting the document with valid data or remove
|
579
579
|
the validations."
|
580
580
|
delete_restriction:
|
581
|
-
message: "Cannot
|
581
|
+
message: "Cannot destroy %{document} because of dependent '%{relation}'."
|
582
582
|
summary: "When defining '%{relation}' with a :dependent => :restrict_with_error,
|
583
|
-
Mongoid will raise an error when attempting to
|
583
|
+
Mongoid will raise an error when attempting to destroy the
|
584
584
|
%{document} when the child '%{relation}' still has documents in it."
|
585
585
|
resolution: "Don't attempt to delete the parent %{document} when
|
586
586
|
it has children, or change the dependent option on the association."
|
@@ -121,6 +121,9 @@ module Mongoid
|
|
121
121
|
# (embedded) association with the given key, or nil if no projection
|
122
122
|
# is to be performed.
|
123
123
|
#
|
124
|
+
# Also returns nil if exclusionary projection was requested but it does
|
125
|
+
# not exclude the field of the association.
|
126
|
+
#
|
124
127
|
# For example, if __selected_fields is {'a' => 1, 'b.c' => 2, 'b.c.f' => 3},
|
125
128
|
# and assoc_key is 'b', return value would be {'c' => 2, 'c.f' => 3}.
|
126
129
|
#
|
@@ -132,6 +135,15 @@ module Mongoid
|
|
132
135
|
def _mongoid_filter_selected_fields(assoc_key)
|
133
136
|
return nil unless __selected_fields
|
134
137
|
|
138
|
+
# If the list of fields was specified using #without instead of #only
|
139
|
+
# and the provided list does not include the association, any of its
|
140
|
+
# fields should be allowed.
|
141
|
+
if __selected_fields.values.all? { |v| v == 0 } &&
|
142
|
+
__selected_fields.keys.none? { |k| k.split('.', 2).first == assoc_key }
|
143
|
+
then
|
144
|
+
return nil
|
145
|
+
end
|
146
|
+
|
135
147
|
projecting_assoc = false
|
136
148
|
|
137
149
|
filtered = {}
|
@@ -300,7 +312,7 @@ module Mongoid
|
|
300
312
|
ids_method = "#{association.name.to_s.singularize}_ids"
|
301
313
|
association.inverse_class.tap do |klass|
|
302
314
|
klass.re_define_method(ids_method) do
|
303
|
-
send(association.name).only(:
|
315
|
+
send(association.name).only(:_id).map(&:_id)
|
304
316
|
end
|
305
317
|
end
|
306
318
|
end
|
@@ -78,13 +78,13 @@ module Mongoid
|
|
78
78
|
# the appropriate strategy to perform the operation.
|
79
79
|
#
|
80
80
|
# @example Execute cascades.
|
81
|
-
# document.
|
81
|
+
# document.apply_destroy_dependencies!
|
82
82
|
#
|
83
83
|
# @since 2.0.0.rc.1
|
84
|
-
def
|
84
|
+
def apply_destroy_dependencies!
|
85
85
|
self.class._all_dependents.each do |association|
|
86
|
-
if association.try(:dependent)
|
87
|
-
send("_dependent_#{
|
86
|
+
if dependent = association.try(:dependent)
|
87
|
+
send("_dependent_#{dependent}!", association)
|
88
88
|
end
|
89
89
|
end
|
90
90
|
end
|
@@ -329,7 +329,7 @@ module Mongoid
|
|
329
329
|
self.path = doc.atomic_path unless path
|
330
330
|
execute_callback :before_remove, doc
|
331
331
|
unless _assigning?
|
332
|
-
doc.
|
332
|
+
doc.apply_destroy_dependencies!
|
333
333
|
doc.run_before_callbacks(:destroy) if method == :destroy
|
334
334
|
end
|
335
335
|
_target.delete_one(doc)
|
@@ -83,8 +83,15 @@ module Mongoid
|
|
83
83
|
|
84
84
|
alias :new :build
|
85
85
|
|
86
|
-
# Clear the association. Will delete the documents from the db
|
87
|
-
# already persisted.
|
86
|
+
# Clear the association. Will delete the documents from the db
|
87
|
+
# if they are already persisted.
|
88
|
+
#
|
89
|
+
# If the host document is not persisted but its _id matches a
|
90
|
+
# persisted document, calling #clear on an association will remove
|
91
|
+
# the association's documents from the database even though the
|
92
|
+
# set of documents in the application (as loaded in the host)
|
93
|
+
# is different from what is in the database, and the host may
|
94
|
+
# not contain any persisted documents in the association either.
|
88
95
|
#
|
89
96
|
# @example Clear the association.
|
90
97
|
# person.addresses.clear
|
@@ -332,7 +339,7 @@ module Mongoid
|
|
332
339
|
private
|
333
340
|
|
334
341
|
def object_already_related?(document)
|
335
|
-
_target.any? { |existing| existing.
|
342
|
+
_target.any? { |existing| existing._id && existing === document }
|
336
343
|
end
|
337
344
|
|
338
345
|
# Appends the document to the target array, updating the index on the
|
@@ -181,7 +181,7 @@ module Mongoid
|
|
181
181
|
first = existing.first
|
182
182
|
converted = first ? convert_id(first.class, id) : id
|
183
183
|
|
184
|
-
if existing.where(
|
184
|
+
if existing.where(_id: converted).exists?
|
185
185
|
# document exists in association
|
186
186
|
doc = existing.find(converted)
|
187
187
|
if destroyable?(attrs)
|
@@ -71,7 +71,8 @@ module Mongoid
|
|
71
71
|
#
|
72
72
|
# @since 2.0.0
|
73
73
|
def acceptable_id?
|
74
|
-
id =
|
74
|
+
id = association.klass.extract_id_field(attributes)
|
75
|
+
id = convert_id(existing.class, id)
|
75
76
|
existing._id == id || id.nil? || (existing._id != id && update_only?)
|
76
77
|
end
|
77
78
|
|
@@ -84,7 +85,8 @@ module Mongoid
|
|
84
85
|
#
|
85
86
|
# @since 2.0.0
|
86
87
|
def delete?
|
87
|
-
|
88
|
+
id = association.klass.extract_id_field(attributes)
|
89
|
+
destroyable? && !id.nil?
|
88
90
|
end
|
89
91
|
|
90
92
|
# Can the existing association potentially be destroyed?
|
@@ -16,7 +16,7 @@ module Mongoid
|
|
16
16
|
# We undefine most methods to get them sent through to the target.
|
17
17
|
instance_methods.each do |method|
|
18
18
|
undef_method(method) unless
|
19
|
-
method =~ /\A(__.*|send|object_id|equal\?|respond_to\?|tap|public_send|extend_proxy|extend_proxies)\z/
|
19
|
+
method =~ /\A(?:__.*|send|object_id|equal\?|respond_to\?|respond_to_missing\?|tap|public_send|extend_proxy|extend_proxies)\z/
|
20
20
|
end
|
21
21
|
|
22
22
|
include Threaded::Lifecycle
|
@@ -137,6 +137,11 @@ module Mongoid
|
|
137
137
|
_target.send(name, *args, &block)
|
138
138
|
end
|
139
139
|
|
140
|
+
# @api private
|
141
|
+
def respond_to_missing?(name, *args)
|
142
|
+
_target.respond_to?(name, *args)
|
143
|
+
end
|
144
|
+
|
140
145
|
# When the base document illegally references an embedded document this
|
141
146
|
# error will get raised.
|
142
147
|
#
|
@@ -63,8 +63,8 @@ module Mongoid
|
|
63
63
|
self.before_callback_halted = false
|
64
64
|
else
|
65
65
|
__autosaving__ do
|
66
|
-
if
|
67
|
-
Array(
|
66
|
+
if assoc_value = ivar(association.name)
|
67
|
+
Array(assoc_value).each do |doc|
|
68
68
|
doc.with(persistence_context) do |d|
|
69
69
|
d.save
|
70
70
|
end
|
@@ -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
|