mongo_mapper 0.18.0 → 0.18.1
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
- data/lib/mongo_mapper/exceptions.rb +7 -1
- data/lib/mongo_mapper/extensions/symbol.rb +3 -0
- data/lib/mongo_mapper/plugins/embedded_document.rb +10 -0
- data/lib/mongo_mapper/plugins/keys/key.rb +14 -0
- data/lib/mongo_mapper/plugins/keys.rb +58 -7
- data/lib/mongo_mapper/plugins/scopes.rb +57 -27
- data/lib/mongo_mapper/plugins.rb +30 -8
- data/lib/mongo_mapper/version.rb +1 -1
- data/lib/mongo_mapper.rb +1 -0
- data/spec/examples.txt +1664 -1638
- data/spec/functional/embedded_document_spec.rb +10 -0
- data/spec/functional/keys_spec.rb +80 -0
- data/spec/functional/scopes_spec.rb +170 -4
- data/spec/functional/stats_spec.rb +59 -13
- data/spec/unit/key_spec.rb +19 -0
- data/spec/unit/plugins_spec.rb +44 -0
- metadata +5 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 070e83a811199050ec61377dc5a69a44aa19a737486632dde0f59dc5e86fbeb1
|
|
4
|
+
data.tar.gz: 94ed7b07b627b1b3ff92025e02c89d68558e6c7eeccf9337b27fd269dfb80948
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 32066190e451388cf35bd65167f0a8f9112eb097f4dbf0584e86c4ce0a3fb4f01ee4891ee794064d30056817080a61ffb0b2db0989d6cc6f3dfaa2b180f3b117
|
|
7
|
+
data.tar.gz: da85cfaf7ea47b586b5cecd6bd323b98d266cc58e2cc53c6c2975f96f00d13255aa6b7e328e26daa781c90026a4f2a18a6f18699231bb91fa27736ff12125b56
|
|
@@ -30,4 +30,10 @@ module MongoMapper
|
|
|
30
30
|
super("Declare either attr_protected or attr_accessible for #{name}, but not both.")
|
|
31
31
|
end
|
|
32
32
|
end
|
|
33
|
-
|
|
33
|
+
|
|
34
|
+
class NoRootDocument < Error
|
|
35
|
+
def initialize(name)
|
|
36
|
+
super("Can not save embedded document #{name} without a root document.")
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -27,12 +27,16 @@ module MongoMapper
|
|
|
27
27
|
end
|
|
28
28
|
|
|
29
29
|
def save(options={})
|
|
30
|
+
ensure_root_document
|
|
31
|
+
|
|
30
32
|
_root_document.try(:save, options).tap do |result|
|
|
31
33
|
persist(options) if result
|
|
32
34
|
end
|
|
33
35
|
end
|
|
34
36
|
|
|
35
37
|
def save!(options={})
|
|
38
|
+
ensure_root_document
|
|
39
|
+
|
|
36
40
|
valid? || raise(DocumentNotValid.new(self))
|
|
37
41
|
_root_document.try(:save!, options).tap do |result|
|
|
38
42
|
persist(options) if result
|
|
@@ -48,6 +52,12 @@ module MongoMapper
|
|
|
48
52
|
def _root_document
|
|
49
53
|
_parent_document.try(:_root_document)
|
|
50
54
|
end
|
|
55
|
+
|
|
56
|
+
private
|
|
57
|
+
|
|
58
|
+
def ensure_root_document
|
|
59
|
+
raise NoRootDocument.new(self.class.name) unless _root_document
|
|
60
|
+
end
|
|
51
61
|
end
|
|
52
62
|
end
|
|
53
63
|
end
|
|
@@ -106,6 +106,20 @@ module MongoMapper
|
|
|
106
106
|
RESERVED_KEYS.include?(@name)
|
|
107
107
|
end
|
|
108
108
|
|
|
109
|
+
# Names of the accessor methods this key will define. Empty when
|
|
110
|
+
# accessors are :skip or :none. The reader also defines
|
|
111
|
+
# name_before_type_cast.
|
|
112
|
+
def accessor_method_names
|
|
113
|
+
names = []
|
|
114
|
+
if read_accessor?
|
|
115
|
+
names << @name
|
|
116
|
+
names << "#{@name}_before_type_cast"
|
|
117
|
+
end
|
|
118
|
+
names << "#{@name}=" if write_accessor?
|
|
119
|
+
names << "#{@name}?" if predicate_accessor?
|
|
120
|
+
names
|
|
121
|
+
end
|
|
122
|
+
|
|
109
123
|
def read_accessor?
|
|
110
124
|
any_accessor? ["read"]
|
|
111
125
|
end
|
|
@@ -58,7 +58,10 @@ module MongoMapper
|
|
|
58
58
|
Key.new(*args).tap do |key|
|
|
59
59
|
keys[key.name] = key
|
|
60
60
|
keys[key.abbr] = key if key.abbr
|
|
61
|
-
|
|
61
|
+
if key.valid_ruby_name? && !key.reserved_name?
|
|
62
|
+
warn_on_internal_method_clash(key)
|
|
63
|
+
create_accessors_for(key)
|
|
64
|
+
end
|
|
62
65
|
create_key_in_descendants(*args)
|
|
63
66
|
create_indexes_for(key)
|
|
64
67
|
create_validations_for(key)
|
|
@@ -139,7 +142,54 @@ module MongoMapper
|
|
|
139
142
|
if key_accessors_module_defined?
|
|
140
143
|
const_get 'MongoMapperKeys'
|
|
141
144
|
else
|
|
142
|
-
|
|
145
|
+
mod = Module.new
|
|
146
|
+
# Mark accessor modules so a later key (an _id override, or _type
|
|
147
|
+
# copied onto a subclass) is not treated as a clash with itself.
|
|
148
|
+
mod.define_singleton_method(:key_accessor_module?) { true }
|
|
149
|
+
const_set 'MongoMapperKeys', mod
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# Warn when an accessor this key will define is already a method on
|
|
154
|
+
# this model. Reserved names still raise from Key. __dynamic keys and
|
|
155
|
+
# keys with accessors :skip / :none define nothing, so they do not
|
|
156
|
+
# warn. Methods created by an earlier key (accessor module, ActiveModel
|
|
157
|
+
# attribute methods, or name_before_type_cast on the model class) are
|
|
158
|
+
# not clashes. This will raise in a future release.
|
|
159
|
+
def warn_on_internal_method_clash(key)
|
|
160
|
+
return if key.dynamic?
|
|
161
|
+
|
|
162
|
+
clashes = key.accessor_method_names.select do |method_name|
|
|
163
|
+
clashes_with_existing_method?(method_name)
|
|
164
|
+
end
|
|
165
|
+
return if clashes.empty?
|
|
166
|
+
|
|
167
|
+
Kernel.warn "[DEPRECATION] Key name `#{key.name}` clashes with an internal MongoMapper method (#{clashes.join(', ')}) and will raise in a future release."
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def clashes_with_existing_method?(method_name)
|
|
171
|
+
name = method_name.to_sym
|
|
172
|
+
if method_defined?(name) || private_method_defined?(name)
|
|
173
|
+
owner = instance_method(name).owner
|
|
174
|
+
return false if owner.respond_to?(:key_accessor_module?) && owner&.key_accessor_module?
|
|
175
|
+
return false if attribute_method_module?(owner)
|
|
176
|
+
return false if name.to_s.end_with?("_before_type_cast") &&
|
|
177
|
+
owner.is_a?(Class) && ancestors.include?(owner)
|
|
178
|
+
|
|
179
|
+
return true
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
false
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# ActiveModel#define_attribute_methods stores its reader module in this
|
|
186
|
+
# ivar. Calling generated_attribute_methods would create one, so look
|
|
187
|
+
# it up without touching it. The ivar lives on the class that defined
|
|
188
|
+
# the key, which may be an ancestor.
|
|
189
|
+
def attribute_method_module?(owner)
|
|
190
|
+
ancestors.any? do |ancestor|
|
|
191
|
+
ancestor.instance_variable_defined?(:@generated_attribute_methods) &&
|
|
192
|
+
owner.equal?(ancestor.instance_variable_get(:@generated_attribute_methods))
|
|
143
193
|
end
|
|
144
194
|
end
|
|
145
195
|
|
|
@@ -378,12 +428,13 @@ module MongoMapper
|
|
|
378
428
|
key_name_sym = key_name.to_sym
|
|
379
429
|
if @_dynamic_attributes && @_dynamic_attributes.key?(key_name_sym)
|
|
380
430
|
@_dynamic_attributes[key_name_sym]
|
|
381
|
-
elsif key = keys[
|
|
382
|
-
|
|
383
|
-
|
|
431
|
+
elsif key = keys[key_name_sym.name]
|
|
432
|
+
ivar = key.ivar
|
|
433
|
+
if ivar && instance_variable_defined?(ivar)
|
|
434
|
+
value = instance_variable_get(ivar)
|
|
384
435
|
else
|
|
385
|
-
if
|
|
386
|
-
instance_variable_set
|
|
436
|
+
if ivar
|
|
437
|
+
instance_variable_set ivar, key.get(nil)
|
|
387
438
|
else
|
|
388
439
|
@_dynamic_attributes[key_name_sym] = key.get(nil)
|
|
389
440
|
end
|
|
@@ -30,10 +30,6 @@ module MongoMapper
|
|
|
30
30
|
self._scopes ||= {}
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
-
def active_scopes
|
|
34
|
-
Thread.current["mongo_mapper_#{name}_active_scopes"] ||= []
|
|
35
|
-
end
|
|
36
|
-
|
|
37
33
|
def default_scopes
|
|
38
34
|
@default_scopes ||= begin
|
|
39
35
|
superclass.respond_to?(:default_scopes) ?
|
|
@@ -42,16 +38,30 @@ module MongoMapper
|
|
|
42
38
|
end
|
|
43
39
|
end
|
|
44
40
|
|
|
41
|
+
# @param Hash options
|
|
42
|
+
# @options options [Boolean] :unscoped Specify to return plain query object
|
|
45
43
|
def query(options = {})
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
44
|
+
# Dup so :unscoped can be removed without mutating a hash the caller
|
|
45
|
+
# still holds, and so a frozen hash does not raise.
|
|
46
|
+
options = options.dup
|
|
47
|
+
|
|
48
|
+
if options.delete(:unscoped)
|
|
49
|
+
super(options)
|
|
50
|
+
elsif (context = current_context)
|
|
51
|
+
# with_scope / unscoped already captured default and active criteria
|
|
52
|
+
# on this query. amend applies both criteria and query options
|
|
53
|
+
# (limit, sort, ...) the same way Querying#query does. where()
|
|
54
|
+
# would fold those options into the criteria hash.
|
|
55
|
+
context.clone.amend(options)
|
|
56
|
+
elsif applying_default_scopes?
|
|
57
|
+
# A default scope is running and called back into query. Applying
|
|
58
|
+
# default scopes again recurses. Skip them for this call only.
|
|
59
|
+
# The previous implementation cleared the shared @default_scopes
|
|
60
|
+
# array to get the same effect, which other threads could observe.
|
|
61
|
+
super(options)
|
|
62
|
+
else
|
|
63
|
+
apply_default_scopes(super(options))
|
|
52
64
|
end
|
|
53
|
-
|
|
54
|
-
res
|
|
55
65
|
end
|
|
56
66
|
|
|
57
67
|
def default_scope(*args, &block)
|
|
@@ -66,28 +76,52 @@ module MongoMapper
|
|
|
66
76
|
default_scopes
|
|
67
77
|
end
|
|
68
78
|
|
|
69
|
-
def with_scope(
|
|
70
|
-
|
|
79
|
+
def with_scope(scope = {})
|
|
80
|
+
prev, self.current_context = current_context, process_scope(query, scope)
|
|
71
81
|
yield
|
|
72
82
|
ensure
|
|
73
|
-
|
|
83
|
+
self.current_context = prev
|
|
74
84
|
end
|
|
75
85
|
|
|
76
86
|
def unscoped
|
|
77
|
-
|
|
78
|
-
old_active_scopes = active_scopes.dup
|
|
79
|
-
|
|
80
|
-
@default_scopes = []
|
|
81
|
-
active_scopes.clear
|
|
82
|
-
|
|
87
|
+
prev, self.current_context = current_context, query(unscoped: true)
|
|
83
88
|
yield
|
|
84
89
|
ensure
|
|
85
|
-
|
|
86
|
-
active_scopes.concat(old_active_scopes)
|
|
90
|
+
self.current_context = prev
|
|
87
91
|
end
|
|
88
92
|
|
|
89
93
|
private
|
|
90
94
|
|
|
95
|
+
def current_context
|
|
96
|
+
Thread.current["mongo_mapper_#{name}_current_context"]
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def current_context=(context)
|
|
100
|
+
Thread.current["mongo_mapper_#{name}_current_context"] = context
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def applying_default_scopes_key
|
|
104
|
+
"mongo_mapper_#{name}_applying_default_scopes"
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def applying_default_scopes?
|
|
108
|
+
Thread.current[applying_default_scopes_key]
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def apply_default_scopes(context)
|
|
112
|
+
key = applying_default_scopes_key
|
|
113
|
+
previous = Thread.current[key]
|
|
114
|
+
Thread.current[key] = true
|
|
115
|
+
|
|
116
|
+
default_scopes.flatten.each do |scope|
|
|
117
|
+
context = process_scope(context, scope)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
context
|
|
121
|
+
ensure
|
|
122
|
+
Thread.current[key] = previous
|
|
123
|
+
end
|
|
124
|
+
|
|
91
125
|
def process_scope(context, scope, *args)
|
|
92
126
|
if scope.is_a?(Proc)
|
|
93
127
|
scope = context.instance_exec(*args, &scope)
|
|
@@ -98,10 +132,6 @@ module MongoMapper
|
|
|
98
132
|
scope
|
|
99
133
|
end
|
|
100
134
|
|
|
101
|
-
def all_anonymous_scopes
|
|
102
|
-
[default_scopes + active_scopes].flatten
|
|
103
|
-
end
|
|
104
|
-
|
|
105
135
|
RESTRICTED_CLASS_METHODS = %w(private public protected allocate new name parent superclass)
|
|
106
136
|
private_constant :RESTRICTED_CLASS_METHODS
|
|
107
137
|
|
data/lib/mongo_mapper/plugins.rb
CHANGED
|
@@ -1,14 +1,36 @@
|
|
|
1
1
|
# encoding: UTF-8
|
|
2
2
|
module MongoMapper
|
|
3
3
|
module Plugins
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
# Holds weak references to the classes that include a plugin host
|
|
5
|
+
# (MongoMapper::Document / EmbeddedDocument), so that classes discarded by
|
|
6
|
+
# code reloading (e.g. Rails development mode) can be garbage collected.
|
|
7
|
+
#
|
|
8
|
+
# ActiveSupport::DescendantsTracker::DescendantsArray used to provide this,
|
|
9
|
+
# but it was removed in Rails 7.1 and falling back to a plain Array kept
|
|
10
|
+
# every reloaded model class (and everything it references) alive forever.
|
|
11
|
+
class WeakDescendants
|
|
12
|
+
include Enumerable
|
|
13
|
+
|
|
14
|
+
def initialize
|
|
15
|
+
@map = ObjectSpace::WeakMap.new
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def <<(klass)
|
|
19
|
+
# Use the class as the value too: older Rubies cannot hold immediates
|
|
20
|
+
# (e.g. true) in a WeakMap.
|
|
21
|
+
@map[klass] = klass
|
|
22
|
+
self
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def each(&block)
|
|
26
|
+
return to_enum(:each) unless block_given?
|
|
27
|
+
@map.each_value(&block)
|
|
28
|
+
self
|
|
29
|
+
end
|
|
8
30
|
end
|
|
9
31
|
|
|
10
32
|
def self.direct_descendants_hash
|
|
11
|
-
@direct_descendants_hash ||= Hash.new { |h,k| h[k] =
|
|
33
|
+
@direct_descendants_hash ||= Hash.new { |h,k| h[k] = WeakDescendants.new }
|
|
12
34
|
end
|
|
13
35
|
|
|
14
36
|
def plugins
|
|
@@ -23,16 +45,16 @@ module MongoMapper
|
|
|
23
45
|
end
|
|
24
46
|
|
|
25
47
|
def direct_descendants
|
|
26
|
-
Plugins.direct_descendants_hash[self]
|
|
48
|
+
Plugins.direct_descendants_hash[self].to_a
|
|
27
49
|
end
|
|
28
50
|
|
|
29
51
|
def included(base = nil)
|
|
30
|
-
|
|
52
|
+
Plugins.direct_descendants_hash[self] << base if base
|
|
31
53
|
super
|
|
32
54
|
end
|
|
33
55
|
|
|
34
56
|
def inherited(subclass)
|
|
35
|
-
|
|
57
|
+
Plugins.direct_descendants_hash[self] << subclass
|
|
36
58
|
super
|
|
37
59
|
end
|
|
38
60
|
end
|
data/lib/mongo_mapper/version.rb
CHANGED
data/lib/mongo_mapper.rb
CHANGED
|
@@ -23,6 +23,7 @@ module MongoMapper
|
|
|
23
23
|
autoload :AccessibleOrProtected, 'mongo_mapper/exceptions'
|
|
24
24
|
autoload :InvalidKey, 'mongo_mapper/exceptions'
|
|
25
25
|
autoload :NotSupported, 'mongo_mapper/exceptions'
|
|
26
|
+
autoload :NoRootDocument, 'mongo_mapper/exceptions'
|
|
26
27
|
|
|
27
28
|
autoload :Document, 'mongo_mapper/document'
|
|
28
29
|
autoload :EmbeddedDocument, 'mongo_mapper/embedded_document'
|