mongo_mapper_ign 0.7.7 → 0.7.8
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.
- data/LICENSE +20 -20
- data/README.rdoc +34 -34
- data/bin/mmconsole +60 -60
- data/lib/mongo_mapper.rb +123 -123
- data/lib/mongo_mapper/document.rb +292 -292
- data/lib/mongo_mapper/embedded_document.rb +71 -71
- data/lib/mongo_mapper/plugins.rb +36 -36
- data/lib/mongo_mapper/plugins/associations.rb +115 -115
- data/lib/mongo_mapper/plugins/associations/base.rb +124 -124
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +31 -31
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +26 -26
- data/lib/mongo_mapper/plugins/associations/collection.rb +21 -21
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +39 -39
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +160 -144
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +29 -29
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +130 -130
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -32
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +24 -24
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -14
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +42 -42
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +70 -70
- data/lib/mongo_mapper/plugins/associations/proxy.rb +125 -125
- data/lib/mongo_mapper/plugins/callbacks.rb +241 -241
- data/lib/mongo_mapper/plugins/clone.rb +13 -13
- data/lib/mongo_mapper/plugins/descendants.rb +16 -16
- data/lib/mongo_mapper/plugins/dirty.rb +119 -119
- data/lib/mongo_mapper/plugins/equality.rb +23 -23
- data/lib/mongo_mapper/plugins/identity_map.rb +123 -123
- data/lib/mongo_mapper/plugins/inspect.rb +14 -14
- data/lib/mongo_mapper/plugins/keys.rb +322 -322
- data/lib/mongo_mapper/plugins/keys/key.rb +53 -53
- data/lib/mongo_mapper/plugins/logger.rb +17 -17
- data/lib/mongo_mapper/plugins/modifiers.rb +111 -111
- data/lib/mongo_mapper/plugins/pagination.rb +24 -24
- data/lib/mongo_mapper/plugins/pagination/proxy.rb +72 -72
- data/lib/mongo_mapper/plugins/persistence.rb +96 -96
- data/lib/mongo_mapper/plugins/protected.rb +46 -46
- data/lib/mongo_mapper/plugins/rails.rb +57 -57
- data/lib/mongo_mapper/plugins/serialization.rb +92 -92
- data/lib/mongo_mapper/plugins/serialization/array.rb +56 -56
- data/lib/mongo_mapper/plugins/serialization/xml_serializer.rb +239 -239
- data/lib/mongo_mapper/plugins/timestamps.rb +21 -21
- data/lib/mongo_mapper/plugins/userstamps.rb +14 -14
- data/lib/mongo_mapper/plugins/validations.rb +46 -46
- data/lib/mongo_mapper/query.rb +28 -28
- data/lib/mongo_mapper/support.rb +197 -197
- data/lib/mongo_mapper/support/descendant_appends.rb +46 -46
- data/lib/mongo_mapper/support/find.rb +77 -77
- data/lib/mongo_mapper/version.rb +3 -3
- metadata +4 -25
@@ -1,241 +1,241 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
# Almost all of this callback stuff is pulled directly from ActiveSupport
|
3
|
-
# in the interest of support rails 2 and 3 at the same time and is the
|
4
|
-
# same copyright as rails.
|
5
|
-
module MongoMapper
|
6
|
-
module Plugins
|
7
|
-
module Callbacks
|
8
|
-
def self.configure(model)
|
9
|
-
model.class_eval do
|
10
|
-
define_callbacks(
|
11
|
-
:before_save, :after_save,
|
12
|
-
:before_create, :after_create,
|
13
|
-
:before_update, :after_update,
|
14
|
-
:before_validation, :after_validation,
|
15
|
-
:before_validation_on_create, :after_validation_on_create,
|
16
|
-
:before_validation_on_update, :after_validation_on_update,
|
17
|
-
:before_destroy, :after_destroy,
|
18
|
-
:validate_on_create, :validate_on_update,
|
19
|
-
:validate
|
20
|
-
)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
module ClassMethods
|
25
|
-
def define_callbacks(*callbacks)
|
26
|
-
callbacks.each do |callback|
|
27
|
-
class_eval <<-"end_eval"
|
28
|
-
def self.#{callback}(*methods, &block)
|
29
|
-
callbacks = CallbackChain.build(:#{callback}, *methods, &block)
|
30
|
-
@#{callback}_callbacks ||= CallbackChain.new
|
31
|
-
@#{callback}_callbacks.concat callbacks
|
32
|
-
end
|
33
|
-
|
34
|
-
def self.#{callback}_callback_chain
|
35
|
-
@#{callback}_callbacks ||= CallbackChain.new
|
36
|
-
|
37
|
-
if superclass.respond_to?(:#{callback}_callback_chain)
|
38
|
-
CallbackChain.new(
|
39
|
-
superclass.#{callback}_callback_chain +
|
40
|
-
@#{callback}_callbacks
|
41
|
-
)
|
42
|
-
else
|
43
|
-
@#{callback}_callbacks
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end_eval
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
module InstanceMethods
|
52
|
-
def valid?
|
53
|
-
action = new? ? 'create' : 'update'
|
54
|
-
run_callbacks(:before_validation)
|
55
|
-
run_callbacks("before_validation_on_#{action}".to_sym)
|
56
|
-
result = super
|
57
|
-
run_callbacks("after_validation_on_#{action}".to_sym)
|
58
|
-
run_callbacks(:after_validation)
|
59
|
-
result
|
60
|
-
end
|
61
|
-
|
62
|
-
# Overriding validatable's valid_for_group? to integrate validation callbacks
|
63
|
-
def valid_for_group?(group) #:nodoc:
|
64
|
-
errors.clear
|
65
|
-
run_before_validations
|
66
|
-
run_callbacks(:validate)
|
67
|
-
new? ? run_callbacks(:validate_on_create) : run_callbacks(:validate_on_update)
|
68
|
-
self.class.validate_children(self, group)
|
69
|
-
self.validate_group(group)
|
70
|
-
errors.empty?
|
71
|
-
end
|
72
|
-
|
73
|
-
def destroy
|
74
|
-
run_callbacks(:before_destroy)
|
75
|
-
result = super
|
76
|
-
run_callbacks(:after_destroy)
|
77
|
-
result
|
78
|
-
end
|
79
|
-
|
80
|
-
def run_callbacks(kind, options = {}, &block)
|
81
|
-
callback_chain_method = "#{kind}_callback_chain"
|
82
|
-
return unless self.class.respond_to?(callback_chain_method)
|
83
|
-
self.class.send(callback_chain_method).run(self, options, &block)
|
84
|
-
self.embedded_associations.each do |association|
|
85
|
-
if association.one?
|
86
|
-
self.send(association.name).run_callbacks(kind, options, &block)
|
87
|
-
else
|
88
|
-
self.send(association.name).each do |document|
|
89
|
-
document.run_callbacks(kind, options, &block)
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
private
|
96
|
-
def create_or_update(*args)
|
97
|
-
run_callbacks(:before_save)
|
98
|
-
if result = super
|
99
|
-
run_callbacks(:after_save)
|
100
|
-
end
|
101
|
-
result
|
102
|
-
end
|
103
|
-
|
104
|
-
def create(*args)
|
105
|
-
run_callbacks(:before_create)
|
106
|
-
result = super
|
107
|
-
run_callbacks(:after_create)
|
108
|
-
result
|
109
|
-
end
|
110
|
-
|
111
|
-
def update(*args)
|
112
|
-
run_callbacks(:before_update)
|
113
|
-
result = super
|
114
|
-
run_callbacks(:after_update)
|
115
|
-
result
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
class CallbackChain < Array
|
120
|
-
def self.build(kind, *methods, &block)
|
121
|
-
methods, options = extract_options(*methods, &block)
|
122
|
-
methods.map! { |method| Callback.new(kind, method, options) }
|
123
|
-
new(methods)
|
124
|
-
end
|
125
|
-
|
126
|
-
def run(object, options = {}, &terminator)
|
127
|
-
enumerator = options[:enumerator] || :each
|
128
|
-
|
129
|
-
unless block_given?
|
130
|
-
send(enumerator) { |callback| callback.call(object) }
|
131
|
-
else
|
132
|
-
send(enumerator) do |callback|
|
133
|
-
result = callback.call(object)
|
134
|
-
break result if terminator.call(result, object)
|
135
|
-
end
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
# TODO: Decompose into more Array like behavior
|
140
|
-
def replace_or_append!(chain)
|
141
|
-
if index = index(chain)
|
142
|
-
self[index] = chain
|
143
|
-
else
|
144
|
-
self << chain
|
145
|
-
end
|
146
|
-
self
|
147
|
-
end
|
148
|
-
|
149
|
-
def find(callback, &block)
|
150
|
-
select { |c| c == callback && (!block_given? || yield(c)) }.first
|
151
|
-
end
|
152
|
-
|
153
|
-
def delete(callback)
|
154
|
-
super(callback.is_a?(Callback) ? callback : find(callback))
|
155
|
-
end
|
156
|
-
|
157
|
-
private
|
158
|
-
def self.extract_options(*methods, &block)
|
159
|
-
methods.flatten!
|
160
|
-
options = methods.extract_options!
|
161
|
-
methods << block if block_given?
|
162
|
-
return methods, options
|
163
|
-
end
|
164
|
-
|
165
|
-
def extract_options(*methods, &block)
|
166
|
-
self.class.extract_options(*methods, &block)
|
167
|
-
end
|
168
|
-
end
|
169
|
-
|
170
|
-
class Callback
|
171
|
-
attr_reader :kind, :method, :identifier, :options
|
172
|
-
|
173
|
-
def initialize(kind, method, options = {})
|
174
|
-
@kind = kind
|
175
|
-
@method = method
|
176
|
-
@identifier = options[:identifier]
|
177
|
-
@options = options
|
178
|
-
end
|
179
|
-
|
180
|
-
def ==(other)
|
181
|
-
case other
|
182
|
-
when Callback
|
183
|
-
(self.identifier && self.identifier == other.identifier) || self.method == other.method
|
184
|
-
else
|
185
|
-
(self.identifier && self.identifier == other) || self.method == other
|
186
|
-
end
|
187
|
-
end
|
188
|
-
|
189
|
-
def eql?(other)
|
190
|
-
self == other
|
191
|
-
end
|
192
|
-
|
193
|
-
def dup
|
194
|
-
self.class.new(@kind, @method, @options.dup)
|
195
|
-
end
|
196
|
-
|
197
|
-
def hash
|
198
|
-
if @identifier
|
199
|
-
@identifier.hash
|
200
|
-
else
|
201
|
-
@method.hash
|
202
|
-
end
|
203
|
-
end
|
204
|
-
|
205
|
-
def call(*args, &block)
|
206
|
-
evaluate_method(method, *args, &block) if should_run_callback?(*args)
|
207
|
-
rescue LocalJumpError
|
208
|
-
raise ArgumentError,
|
209
|
-
"Cannot yield from a Proc type filter. The Proc must take two " +
|
210
|
-
"arguments and execute #call on the second argument."
|
211
|
-
end
|
212
|
-
|
213
|
-
private
|
214
|
-
def evaluate_method(method, *args, &block)
|
215
|
-
case method
|
216
|
-
when Symbol
|
217
|
-
object = args.shift
|
218
|
-
object.send(method, *args, &block)
|
219
|
-
when String
|
220
|
-
eval(method, args.first.instance_eval { binding })
|
221
|
-
when Proc, Method
|
222
|
-
method.call(*args, &block)
|
223
|
-
else
|
224
|
-
if method.respond_to?(kind)
|
225
|
-
method.send(kind, *args, &block)
|
226
|
-
else
|
227
|
-
raise ArgumentError,
|
228
|
-
"Callbacks must be a symbol denoting the method to call, a string to be evaluated, " +
|
229
|
-
"a block to be invoked, or an object responding to the callback method."
|
230
|
-
end
|
231
|
-
end
|
232
|
-
end
|
233
|
-
|
234
|
-
def should_run_callback?(*args)
|
235
|
-
[options[:if]].flatten.compact.all? { |a| evaluate_method(a, *args) } &&
|
236
|
-
![options[:unless]].flatten.compact.any? { |a| evaluate_method(a, *args) }
|
237
|
-
end
|
238
|
-
end
|
239
|
-
end
|
240
|
-
end
|
241
|
-
end
|
1
|
+
# encoding: UTF-8
|
2
|
+
# Almost all of this callback stuff is pulled directly from ActiveSupport
|
3
|
+
# in the interest of support rails 2 and 3 at the same time and is the
|
4
|
+
# same copyright as rails.
|
5
|
+
module MongoMapper
|
6
|
+
module Plugins
|
7
|
+
module Callbacks
|
8
|
+
def self.configure(model)
|
9
|
+
model.class_eval do
|
10
|
+
define_callbacks(
|
11
|
+
:before_save, :after_save,
|
12
|
+
:before_create, :after_create,
|
13
|
+
:before_update, :after_update,
|
14
|
+
:before_validation, :after_validation,
|
15
|
+
:before_validation_on_create, :after_validation_on_create,
|
16
|
+
:before_validation_on_update, :after_validation_on_update,
|
17
|
+
:before_destroy, :after_destroy,
|
18
|
+
:validate_on_create, :validate_on_update,
|
19
|
+
:validate
|
20
|
+
)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module ClassMethods
|
25
|
+
def define_callbacks(*callbacks)
|
26
|
+
callbacks.each do |callback|
|
27
|
+
class_eval <<-"end_eval"
|
28
|
+
def self.#{callback}(*methods, &block)
|
29
|
+
callbacks = CallbackChain.build(:#{callback}, *methods, &block)
|
30
|
+
@#{callback}_callbacks ||= CallbackChain.new
|
31
|
+
@#{callback}_callbacks.concat callbacks
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.#{callback}_callback_chain
|
35
|
+
@#{callback}_callbacks ||= CallbackChain.new
|
36
|
+
|
37
|
+
if superclass.respond_to?(:#{callback}_callback_chain)
|
38
|
+
CallbackChain.new(
|
39
|
+
superclass.#{callback}_callback_chain +
|
40
|
+
@#{callback}_callbacks
|
41
|
+
)
|
42
|
+
else
|
43
|
+
@#{callback}_callbacks
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end_eval
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
module InstanceMethods
|
52
|
+
def valid?
|
53
|
+
action = new? ? 'create' : 'update'
|
54
|
+
run_callbacks(:before_validation)
|
55
|
+
run_callbacks("before_validation_on_#{action}".to_sym)
|
56
|
+
result = super
|
57
|
+
run_callbacks("after_validation_on_#{action}".to_sym)
|
58
|
+
run_callbacks(:after_validation)
|
59
|
+
result
|
60
|
+
end
|
61
|
+
|
62
|
+
# Overriding validatable's valid_for_group? to integrate validation callbacks
|
63
|
+
def valid_for_group?(group) #:nodoc:
|
64
|
+
errors.clear
|
65
|
+
run_before_validations
|
66
|
+
run_callbacks(:validate)
|
67
|
+
new? ? run_callbacks(:validate_on_create) : run_callbacks(:validate_on_update)
|
68
|
+
self.class.validate_children(self, group)
|
69
|
+
self.validate_group(group)
|
70
|
+
errors.empty?
|
71
|
+
end
|
72
|
+
|
73
|
+
def destroy
|
74
|
+
run_callbacks(:before_destroy)
|
75
|
+
result = super
|
76
|
+
run_callbacks(:after_destroy)
|
77
|
+
result
|
78
|
+
end
|
79
|
+
|
80
|
+
def run_callbacks(kind, options = {}, &block)
|
81
|
+
callback_chain_method = "#{kind}_callback_chain"
|
82
|
+
return unless self.class.respond_to?(callback_chain_method)
|
83
|
+
self.class.send(callback_chain_method).run(self, options, &block)
|
84
|
+
self.embedded_associations.each do |association|
|
85
|
+
if association.one?
|
86
|
+
self.send(association.name).run_callbacks(kind, options, &block)
|
87
|
+
else
|
88
|
+
self.send(association.name).each do |document|
|
89
|
+
document.run_callbacks(kind, options, &block)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
private
|
96
|
+
def create_or_update(*args)
|
97
|
+
run_callbacks(:before_save)
|
98
|
+
if result = super
|
99
|
+
run_callbacks(:after_save)
|
100
|
+
end
|
101
|
+
result
|
102
|
+
end
|
103
|
+
|
104
|
+
def create(*args)
|
105
|
+
run_callbacks(:before_create)
|
106
|
+
result = super
|
107
|
+
run_callbacks(:after_create)
|
108
|
+
result
|
109
|
+
end
|
110
|
+
|
111
|
+
def update(*args)
|
112
|
+
run_callbacks(:before_update)
|
113
|
+
result = super
|
114
|
+
run_callbacks(:after_update)
|
115
|
+
result
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
class CallbackChain < Array
|
120
|
+
def self.build(kind, *methods, &block)
|
121
|
+
methods, options = extract_options(*methods, &block)
|
122
|
+
methods.map! { |method| Callback.new(kind, method, options) }
|
123
|
+
new(methods)
|
124
|
+
end
|
125
|
+
|
126
|
+
def run(object, options = {}, &terminator)
|
127
|
+
enumerator = options[:enumerator] || :each
|
128
|
+
|
129
|
+
unless block_given?
|
130
|
+
send(enumerator) { |callback| callback.call(object) }
|
131
|
+
else
|
132
|
+
send(enumerator) do |callback|
|
133
|
+
result = callback.call(object)
|
134
|
+
break result if terminator.call(result, object)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
# TODO: Decompose into more Array like behavior
|
140
|
+
def replace_or_append!(chain)
|
141
|
+
if index = index(chain)
|
142
|
+
self[index] = chain
|
143
|
+
else
|
144
|
+
self << chain
|
145
|
+
end
|
146
|
+
self
|
147
|
+
end
|
148
|
+
|
149
|
+
def find(callback, &block)
|
150
|
+
select { |c| c == callback && (!block_given? || yield(c)) }.first
|
151
|
+
end
|
152
|
+
|
153
|
+
def delete(callback)
|
154
|
+
super(callback.is_a?(Callback) ? callback : find(callback))
|
155
|
+
end
|
156
|
+
|
157
|
+
private
|
158
|
+
def self.extract_options(*methods, &block)
|
159
|
+
methods.flatten!
|
160
|
+
options = methods.extract_options!
|
161
|
+
methods << block if block_given?
|
162
|
+
return methods, options
|
163
|
+
end
|
164
|
+
|
165
|
+
def extract_options(*methods, &block)
|
166
|
+
self.class.extract_options(*methods, &block)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
class Callback
|
171
|
+
attr_reader :kind, :method, :identifier, :options
|
172
|
+
|
173
|
+
def initialize(kind, method, options = {})
|
174
|
+
@kind = kind
|
175
|
+
@method = method
|
176
|
+
@identifier = options[:identifier]
|
177
|
+
@options = options
|
178
|
+
end
|
179
|
+
|
180
|
+
def ==(other)
|
181
|
+
case other
|
182
|
+
when Callback
|
183
|
+
(self.identifier && self.identifier == other.identifier) || self.method == other.method
|
184
|
+
else
|
185
|
+
(self.identifier && self.identifier == other) || self.method == other
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
def eql?(other)
|
190
|
+
self == other
|
191
|
+
end
|
192
|
+
|
193
|
+
def dup
|
194
|
+
self.class.new(@kind, @method, @options.dup)
|
195
|
+
end
|
196
|
+
|
197
|
+
def hash
|
198
|
+
if @identifier
|
199
|
+
@identifier.hash
|
200
|
+
else
|
201
|
+
@method.hash
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
def call(*args, &block)
|
206
|
+
evaluate_method(method, *args, &block) if should_run_callback?(*args)
|
207
|
+
rescue LocalJumpError
|
208
|
+
raise ArgumentError,
|
209
|
+
"Cannot yield from a Proc type filter. The Proc must take two " +
|
210
|
+
"arguments and execute #call on the second argument."
|
211
|
+
end
|
212
|
+
|
213
|
+
private
|
214
|
+
def evaluate_method(method, *args, &block)
|
215
|
+
case method
|
216
|
+
when Symbol
|
217
|
+
object = args.shift
|
218
|
+
object.send(method, *args, &block)
|
219
|
+
when String
|
220
|
+
eval(method, args.first.instance_eval { binding })
|
221
|
+
when Proc, Method
|
222
|
+
method.call(*args, &block)
|
223
|
+
else
|
224
|
+
if method.respond_to?(kind)
|
225
|
+
method.send(kind, *args, &block)
|
226
|
+
else
|
227
|
+
raise ArgumentError,
|
228
|
+
"Callbacks must be a symbol denoting the method to call, a string to be evaluated, " +
|
229
|
+
"a block to be invoked, or an object responding to the callback method."
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
def should_run_callback?(*args)
|
235
|
+
[options[:if]].flatten.compact.all? { |a| evaluate_method(a, *args) } &&
|
236
|
+
![options[:unless]].flatten.compact.any? { |a| evaluate_method(a, *args) }
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|