dm-core 0.10.1 → 0.10.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +29 -0
- data/.document +5 -0
- data/.gitignore +27 -0
- data/LICENSE +20 -0
- data/{README.txt → README.rdoc} +14 -3
- data/Rakefile +23 -22
- data/VERSION +1 -0
- data/dm-core.gemspec +201 -10
- data/lib/dm-core.rb +32 -23
- data/lib/dm-core/adapters.rb +0 -1
- data/lib/dm-core/adapters/data_objects_adapter.rb +230 -151
- data/lib/dm-core/adapters/mysql_adapter.rb +7 -8
- data/lib/dm-core/adapters/oracle_adapter.rb +39 -59
- data/lib/dm-core/adapters/postgres_adapter.rb +0 -1
- data/lib/dm-core/adapters/sqlite3_adapter.rb +5 -0
- data/lib/dm-core/adapters/sqlserver_adapter.rb +114 -0
- data/lib/dm-core/adapters/yaml_adapter.rb +0 -5
- data/lib/dm-core/associations/many_to_many.rb +118 -56
- data/lib/dm-core/associations/many_to_one.rb +48 -21
- data/lib/dm-core/associations/one_to_many.rb +8 -30
- data/lib/dm-core/associations/one_to_one.rb +1 -5
- data/lib/dm-core/associations/relationship.rb +89 -97
- data/lib/dm-core/collection.rb +299 -184
- data/lib/dm-core/core_ext/enumerable.rb +28 -0
- data/lib/dm-core/core_ext/kernel.rb +0 -2
- data/lib/dm-core/migrations.rb +314 -170
- data/lib/dm-core/model.rb +97 -66
- data/lib/dm-core/model/descendant_set.rb +1 -1
- data/lib/dm-core/model/hook.rb +0 -3
- data/lib/dm-core/model/property.rb +7 -10
- data/lib/dm-core/model/relationship.rb +79 -26
- data/lib/dm-core/model/scope.rb +3 -4
- data/lib/dm-core/property.rb +152 -90
- data/lib/dm-core/property_set.rb +18 -37
- data/lib/dm-core/query.rb +452 -153
- data/lib/dm-core/query/conditions/comparison.rb +266 -173
- data/lib/dm-core/query/conditions/operation.rb +499 -57
- data/lib/dm-core/query/direction.rb +0 -3
- data/lib/dm-core/query/operator.rb +0 -4
- data/lib/dm-core/query/path.rb +10 -12
- data/lib/dm-core/query/sort.rb +4 -10
- data/lib/dm-core/repository.rb +10 -6
- data/lib/dm-core/resource.rb +343 -148
- data/lib/dm-core/spec/adapter_shared_spec.rb +17 -1
- data/lib/dm-core/spec/data_objects_adapter_shared_spec.rb +277 -17
- data/lib/dm-core/support/chainable.rb +0 -2
- data/lib/dm-core/support/equalizer.rb +27 -3
- data/lib/dm-core/transaction.rb +75 -75
- data/lib/dm-core/type.rb +19 -5
- data/lib/dm-core/types/discriminator.rb +4 -4
- data/lib/dm-core/types/object.rb +2 -7
- data/lib/dm-core/types/paranoid_boolean.rb +8 -2
- data/lib/dm-core/types/paranoid_datetime.rb +8 -2
- data/lib/dm-core/version.rb +1 -1
- data/script/performance.rb +7 -7
- data/script/profile.rb +6 -6
- data/spec/lib/collection_helpers.rb +2 -2
- data/spec/lib/pending_helpers.rb +22 -3
- data/spec/lib/rspec_immediate_feedback_formatter.rb +1 -0
- data/spec/public/associations/many_to_many_spec.rb +6 -4
- data/spec/public/associations/many_to_one_spec.rb +10 -1
- data/spec/public/associations/many_to_one_with_boolean_cpk_spec.rb +39 -0
- data/spec/public/associations/one_to_many_spec.rb +4 -3
- data/spec/public/associations/one_to_one_spec.rb +19 -1
- data/spec/public/associations/one_to_one_with_boolean_cpk_spec.rb +45 -0
- data/spec/public/collection_spec.rb +4 -3
- data/spec/public/migrations_spec.rb +144 -0
- data/spec/public/model/relationship_spec.rb +115 -55
- data/spec/public/model_spec.rb +13 -13
- data/spec/public/property/object_spec.rb +106 -0
- data/spec/public/property_spec.rb +18 -14
- data/spec/public/resource_spec.rb +10 -1
- data/spec/public/sel_spec.rb +16 -49
- data/spec/public/setup_spec.rb +1 -1
- data/spec/public/shared/association_collection_shared_spec.rb +6 -14
- data/spec/public/shared/collection_finder_shared_spec.rb +267 -0
- data/spec/public/shared/collection_shared_spec.rb +214 -217
- data/spec/public/shared/finder_shared_spec.rb +259 -365
- data/spec/public/shared/resource_shared_spec.rb +524 -248
- data/spec/public/transaction_spec.rb +27 -3
- data/spec/public/types/discriminator_spec.rb +1 -1
- data/spec/rcov.opts +6 -0
- data/spec/semipublic/adapters/sqlserver_adapter_spec.rb +17 -0
- data/spec/semipublic/associations/many_to_one_spec.rb +3 -20
- data/spec/semipublic/associations_spec.rb +2 -2
- data/spec/semipublic/collection_spec.rb +0 -32
- data/spec/semipublic/model_spec.rb +96 -0
- data/spec/semipublic/property_spec.rb +3 -3
- data/spec/semipublic/query/conditions/comparison_spec.rb +1719 -0
- data/spec/semipublic/query/conditions/operation_spec.rb +1292 -0
- data/spec/semipublic/query_spec.rb +1285 -144
- data/spec/semipublic/resource_spec.rb +0 -24
- data/spec/semipublic/shared/resource_shared_spec.rb +103 -38
- data/spec/spec.opts +1 -1
- data/spec/spec_helper.rb +15 -6
- data/tasks/ci.rake +1 -0
- data/tasks/metrics.rake +37 -0
- data/tasks/spec.rake +41 -0
- data/tasks/yard.rake +9 -0
- data/tasks/yardstick.rake +19 -0
- metadata +99 -29
- data/CONTRIBUTING +0 -51
- data/FAQ +0 -93
- data/History.txt +0 -27
- data/MIT-LICENSE +0 -22
- data/Manifest.txt +0 -121
- data/QUICKLINKS +0 -11
- data/SPECS +0 -35
- data/TODO +0 -1
- data/spec/semipublic/query/conditions_spec.rb +0 -528
- data/tasks/ci.rb +0 -24
- data/tasks/dm.rb +0 -58
- data/tasks/doc.rb +0 -17
- data/tasks/gemspec.rb +0 -23
- data/tasks/hoe.rb +0 -45
- data/tasks/install.rb +0 -18
@@ -1,65 +1,122 @@
|
|
1
1
|
module DataMapper
|
2
2
|
class Query
|
3
3
|
module Conditions
|
4
|
-
class InvalidOperation < ArgumentError; end
|
5
|
-
|
6
4
|
class Operation
|
7
|
-
#
|
5
|
+
# Factory method to initialize an operation
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# operation = Operation.new(:and, comparison)
|
9
|
+
#
|
10
|
+
# @param [Symbol] slug
|
11
|
+
# the identifier for the operation class
|
12
|
+
# @param [Array] *operands
|
13
|
+
# the operands to initialize the operation with
|
14
|
+
#
|
15
|
+
# @return [AbstractOperation]
|
16
|
+
# the operation matching the slug
|
17
|
+
#
|
8
18
|
# @api semipublic
|
9
19
|
def self.new(slug, *operands)
|
10
20
|
if klass = operation_class(slug)
|
11
21
|
klass.new(*operands)
|
12
22
|
else
|
13
|
-
raise "No Operation class for
|
23
|
+
raise ArgumentError, "No Operation class for #{slug.inspect} has been defined"
|
14
24
|
end
|
15
25
|
end
|
16
26
|
|
17
|
-
#
|
18
|
-
#
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
# TODO: document
|
27
|
+
# Return an Array of all the slugs for the operation classes
|
28
|
+
#
|
29
|
+
# @return [Array]
|
30
|
+
# the slugs of all the operation classes
|
31
|
+
#
|
24
32
|
# @api private
|
25
33
|
def self.slugs
|
26
|
-
|
34
|
+
AbstractOperation.descendants.map { |operation_class| operation_class.slug }
|
27
35
|
end
|
28
36
|
|
29
37
|
class << self
|
30
38
|
private
|
31
39
|
|
32
|
-
#
|
40
|
+
# Returns a Hash mapping the slugs to each class
|
41
|
+
#
|
42
|
+
# @return [Hash]
|
43
|
+
# Hash mapping the slug to the class
|
44
|
+
#
|
33
45
|
# @api private
|
34
46
|
def operation_classes
|
35
47
|
@operation_classes ||= {}
|
36
48
|
end
|
49
|
+
|
50
|
+
# Lookup the operation class based on the slug
|
51
|
+
#
|
52
|
+
# @example
|
53
|
+
# operation_class = Operation.operation_class(:and)
|
54
|
+
#
|
55
|
+
# @param [Symbol] slug
|
56
|
+
# the identifier for the operation class
|
57
|
+
#
|
58
|
+
# @return [Class]
|
59
|
+
# the operation class
|
60
|
+
#
|
61
|
+
# @api private
|
62
|
+
def operation_class(slug)
|
63
|
+
operation_classes[slug] ||= AbstractOperation.descendants.detect { |operation_class| operation_class.slug == slug }
|
64
|
+
end
|
37
65
|
end
|
38
66
|
end # class Operation
|
39
67
|
|
40
68
|
class AbstractOperation
|
69
|
+
include Extlib::Assertions
|
41
70
|
include Enumerable
|
42
71
|
extend Equalizer
|
43
72
|
|
44
73
|
equalize :slug, :sorted_operands
|
45
74
|
|
46
|
-
#
|
75
|
+
# Returns the parent operation
|
76
|
+
#
|
77
|
+
# @return [AbstractOperation]
|
78
|
+
# the parent operation
|
79
|
+
#
|
80
|
+
# @api semipublic
|
81
|
+
attr_accessor :parent
|
82
|
+
|
83
|
+
# Returns the child operations and comparisons
|
84
|
+
#
|
85
|
+
# @return [Set<AbstractOperation, AbstractComparison, Array>]
|
86
|
+
# the set of operations and comparisons
|
87
|
+
#
|
47
88
|
# @api semipublic
|
48
89
|
attr_reader :operands
|
49
90
|
|
50
|
-
|
91
|
+
alias children operands
|
92
|
+
|
93
|
+
# Returns the classes that inherit from AbstractComparison
|
94
|
+
#
|
95
|
+
# @return [Set]
|
96
|
+
# the descendant classes
|
97
|
+
#
|
51
98
|
# @api private
|
52
99
|
def self.descendants
|
53
100
|
@descendants ||= Set.new
|
54
101
|
end
|
55
102
|
|
56
|
-
#
|
103
|
+
# Hook executed when inheriting from AbstractComparison
|
104
|
+
#
|
105
|
+
# @return [undefined]
|
106
|
+
#
|
57
107
|
# @api private
|
58
108
|
def self.inherited(operation_class)
|
59
109
|
descendants << operation_class
|
60
110
|
end
|
61
111
|
|
62
|
-
#
|
112
|
+
# Get and set the slug for the operation class
|
113
|
+
#
|
114
|
+
# @param [Symbol] slug
|
115
|
+
# optionally set the slug for the operation class
|
116
|
+
#
|
117
|
+
# @return [Symbol]
|
118
|
+
# the slug for the operation class
|
119
|
+
#
|
63
120
|
# @api semipublic
|
64
121
|
def self.slug(slug = nil)
|
65
122
|
slug ? @slug = slug : @slug
|
@@ -75,69 +132,274 @@ module DataMapper
|
|
75
132
|
self.class.slug
|
76
133
|
end
|
77
134
|
|
78
|
-
#
|
135
|
+
# Iterate through each operand in the operation
|
136
|
+
#
|
137
|
+
# @yield [operand]
|
138
|
+
# yields to each operand
|
139
|
+
#
|
140
|
+
# @yieldparam [AbstractOperation, AbstractComparison, Array] operand
|
141
|
+
# each operand
|
142
|
+
#
|
143
|
+
# @return [self]
|
144
|
+
# returns the operation
|
145
|
+
#
|
79
146
|
# @api semipublic
|
80
147
|
def each
|
81
|
-
@operands.each {
|
148
|
+
@operands.each { |op| yield op }
|
149
|
+
self
|
82
150
|
end
|
83
151
|
|
84
|
-
#
|
152
|
+
# Test if the operation is valid
|
153
|
+
#
|
154
|
+
# @return [Boolean]
|
155
|
+
# true if the operation is valid, false if not
|
156
|
+
#
|
85
157
|
# @api semipublic
|
86
158
|
def valid?
|
87
|
-
|
88
|
-
if operand.respond_to?(:valid?)
|
89
|
-
operand.valid?
|
90
|
-
else
|
91
|
-
true
|
92
|
-
end
|
93
|
-
end
|
159
|
+
any? && all? { |op| valid_operand?(op) }
|
94
160
|
end
|
95
161
|
|
96
|
-
#
|
162
|
+
# Add an operand to the operation
|
163
|
+
#
|
164
|
+
# @param [AbstractOperation, AbstractComparison, Array] operand
|
165
|
+
# the operand to add
|
166
|
+
#
|
167
|
+
# @return [self]
|
168
|
+
# the operation
|
169
|
+
#
|
97
170
|
# @api semipublic
|
98
171
|
def <<(operand)
|
99
|
-
|
172
|
+
assert_valid_operand_type(operand)
|
173
|
+
@operands << relate_operand(operand)
|
100
174
|
self
|
101
175
|
end
|
102
176
|
|
103
|
-
#
|
177
|
+
# Add operands to the operation
|
178
|
+
#
|
179
|
+
# @param [#each] operands
|
180
|
+
# the operands to add
|
181
|
+
#
|
182
|
+
# @return [self]
|
183
|
+
# the operation
|
184
|
+
#
|
104
185
|
# @api semipublic
|
105
|
-
def
|
106
|
-
|
186
|
+
def merge(operands)
|
187
|
+
operands.each { |op| self << op }
|
188
|
+
self
|
189
|
+
end
|
190
|
+
|
191
|
+
# Return the union with another operand
|
192
|
+
#
|
193
|
+
# @param [AbstractOperation] other
|
194
|
+
# the operand to union with
|
195
|
+
#
|
196
|
+
# @return [OrOperation]
|
197
|
+
# the union of the operation and operand
|
198
|
+
#
|
199
|
+
# @api semipublic
|
200
|
+
def union(other)
|
201
|
+
Operation.new(:or, dup, other.dup).minimize
|
202
|
+
end
|
203
|
+
|
204
|
+
alias | union
|
205
|
+
alias + union
|
206
|
+
|
207
|
+
# Return the intersection of the operation and another operand
|
208
|
+
#
|
209
|
+
# @param [AbstractOperation] other
|
210
|
+
# the operand to intersect with
|
211
|
+
#
|
212
|
+
# @return [AndOperation]
|
213
|
+
# the intersection of the operation and operand
|
214
|
+
#
|
215
|
+
# @api semipublic
|
216
|
+
def intersection(other)
|
217
|
+
Operation.new(:and, dup, other.dup).minimize
|
218
|
+
end
|
219
|
+
|
220
|
+
alias & intersection
|
221
|
+
|
222
|
+
# Return the difference of the operation and another operand
|
223
|
+
#
|
224
|
+
# @param [AbstractOperation] other
|
225
|
+
# the operand to not match
|
226
|
+
#
|
227
|
+
# @return [AndOperation]
|
228
|
+
# the intersection of the operation and operand
|
229
|
+
#
|
230
|
+
# @api semipublic
|
231
|
+
def difference(other)
|
232
|
+
Operation.new(:and, dup, Operation.new(:not, other.dup)).minimize
|
233
|
+
end
|
234
|
+
|
235
|
+
alias - difference
|
236
|
+
|
237
|
+
# Minimize the operation
|
238
|
+
#
|
239
|
+
# @return [self]
|
240
|
+
# the minimized operation
|
241
|
+
#
|
242
|
+
# @api semipublic
|
243
|
+
def minimize
|
244
|
+
self
|
245
|
+
end
|
246
|
+
|
247
|
+
# Clear the operands
|
248
|
+
#
|
249
|
+
# @return [self]
|
250
|
+
# the operation
|
251
|
+
#
|
252
|
+
# @api semipublic
|
253
|
+
def clear
|
254
|
+
@operands.clear
|
255
|
+
self
|
256
|
+
end
|
257
|
+
|
258
|
+
# Return the string representation of the operation
|
259
|
+
#
|
260
|
+
# @return [String]
|
261
|
+
# the string representation of the operation
|
262
|
+
#
|
263
|
+
# @api semipublic
|
264
|
+
def to_s
|
265
|
+
empty? ? '' : "(#{sort_by { |op| op.to_s }.map { |op| op.to_s }.join(" #{slug.to_s.upcase} ")})"
|
266
|
+
end
|
267
|
+
|
268
|
+
# Test if the operation is negated
|
269
|
+
#
|
270
|
+
# Defaults to return false.
|
271
|
+
#
|
272
|
+
# @return [Boolean]
|
273
|
+
# true if the operation is negated, false if not
|
274
|
+
#
|
275
|
+
# @api private
|
276
|
+
def negated?
|
277
|
+
parent = self.parent
|
278
|
+
parent ? parent.negated? : false
|
107
279
|
end
|
108
280
|
|
109
281
|
# Return a list of operands in predictable order
|
110
282
|
#
|
111
|
-
# @return [Array<AbstractOperation>]
|
283
|
+
# @return [Array<AbstractOperation, AbstractComparison, Array>]
|
112
284
|
# list of operands sorted in deterministic order
|
113
285
|
#
|
114
286
|
# @api private
|
115
287
|
def sorted_operands
|
116
|
-
|
288
|
+
sort_by { |op| op.hash }
|
117
289
|
end
|
118
290
|
|
119
291
|
private
|
120
292
|
|
121
|
-
#
|
293
|
+
# Initialize an operation
|
294
|
+
#
|
295
|
+
# @param [Array<AbstractOperation, AbstractComparison, Array>] *operands
|
296
|
+
# the operands to include in the operation
|
297
|
+
#
|
298
|
+
# @return [AbstractOperation]
|
299
|
+
# the operation
|
300
|
+
#
|
122
301
|
# @api semipublic
|
123
302
|
def initialize(*operands)
|
124
|
-
@operands =
|
303
|
+
@operands = Set.new
|
304
|
+
merge(operands)
|
125
305
|
end
|
126
306
|
|
127
|
-
#
|
307
|
+
# Copy an operation
|
308
|
+
#
|
309
|
+
# @param [AbstractOperation] original
|
310
|
+
# the original operation
|
311
|
+
#
|
312
|
+
# @return [undefined]
|
313
|
+
#
|
128
314
|
# @api semipublic
|
129
315
|
def initialize_copy(*)
|
130
|
-
@operands =
|
316
|
+
@operands = map { |op| op.dup }.to_set
|
317
|
+
end
|
318
|
+
|
319
|
+
# Minimize the operands recursively
|
320
|
+
#
|
321
|
+
# @return [undefined]
|
322
|
+
#
|
323
|
+
# @api private
|
324
|
+
def minimize_operands
|
325
|
+
# FIXME: why does Set#map! not work here?
|
326
|
+
@operands = map do |op|
|
327
|
+
relate_operand(op.respond_to?(:minimize) ? op.minimize : op)
|
328
|
+
end.to_set
|
329
|
+
end
|
330
|
+
|
331
|
+
# Prune empty operands recursively
|
332
|
+
#
|
333
|
+
# @return [undefined]
|
334
|
+
#
|
335
|
+
# @api private
|
336
|
+
def prune_operands
|
337
|
+
@operands.delete_if { |op| op.respond_to?(:empty?) ? op.empty? : false }
|
338
|
+
end
|
339
|
+
|
340
|
+
# Test if the operand is valid
|
341
|
+
#
|
342
|
+
# @param [AbstractOperation, AbstractComparison, Array] operand
|
343
|
+
# the operand to test
|
344
|
+
#
|
345
|
+
# @return [Boolean]
|
346
|
+
# true if the operand is valid
|
347
|
+
#
|
348
|
+
# @api private
|
349
|
+
def valid_operand?(operand)
|
350
|
+
if operand.respond_to?(:valid?)
|
351
|
+
operand.valid?
|
352
|
+
else
|
353
|
+
true
|
354
|
+
end
|
355
|
+
end
|
356
|
+
|
357
|
+
# Set self to be the operand's parent
|
358
|
+
#
|
359
|
+
# @return [AbstractOperation, AbstractComparison, Array]
|
360
|
+
# the operand that was related to self
|
361
|
+
#
|
362
|
+
# @api privTE
|
363
|
+
def relate_operand(operand)
|
364
|
+
operand.parent = self if operand.respond_to?(:parent=)
|
365
|
+
operand
|
366
|
+
end
|
367
|
+
|
368
|
+
# Assert that the operand is a valid type
|
369
|
+
#
|
370
|
+
# @param [AbstractOperation, AbstractComparison, Array] operand
|
371
|
+
# the operand to test
|
372
|
+
#
|
373
|
+
# @return [undefined]
|
374
|
+
#
|
375
|
+
# @raise [ArgumentError]
|
376
|
+
# raised if the operand is not a valid type
|
377
|
+
#
|
378
|
+
# @api private
|
379
|
+
def assert_valid_operand_type(operand)
|
380
|
+
assert_kind_of 'operand', operand, AbstractOperation, AbstractComparison, Array
|
131
381
|
end
|
132
382
|
end # class AbstractOperation
|
133
383
|
|
134
384
|
module FlattenOperation
|
135
|
-
#
|
385
|
+
# Add an operand to the operation, flattening the same types
|
386
|
+
#
|
387
|
+
# Flattening means that if the operand is the same as the
|
388
|
+
# operation, we should just include the operand's operands
|
389
|
+
# in the operation and prune that part of the tree. This results
|
390
|
+
# in a shallower tree, is faster to match and usually generates
|
391
|
+
# more efficient queries in the adapters.
|
392
|
+
#
|
393
|
+
# @param [AbstractOperation, AbstractComparison, Array] operand
|
394
|
+
# the operand to add
|
395
|
+
#
|
396
|
+
# @return [self]
|
397
|
+
# the operation
|
398
|
+
#
|
136
399
|
# @api semipublic
|
137
400
|
def <<(operand)
|
138
|
-
if
|
139
|
-
|
140
|
-
self
|
401
|
+
if kind_of?(operand.class)
|
402
|
+
merge(operand.operands)
|
141
403
|
else
|
142
404
|
super
|
143
405
|
end
|
@@ -149,10 +411,41 @@ module DataMapper
|
|
149
411
|
|
150
412
|
slug :and
|
151
413
|
|
152
|
-
#
|
414
|
+
# Match the record
|
415
|
+
#
|
416
|
+
# @example with a Hash
|
417
|
+
# operation.matches?({ :id => 1 }) # => true
|
418
|
+
#
|
419
|
+
# @example with a Resource
|
420
|
+
# operation.matches?(Blog::Article.new(:id => 1)) # => true
|
421
|
+
#
|
422
|
+
# @param [Resource, Hash] record
|
423
|
+
# the resource to match
|
424
|
+
#
|
425
|
+
# @return [true]
|
426
|
+
# true if the record matches, false if not
|
427
|
+
#
|
153
428
|
# @api semipublic
|
154
429
|
def matches?(record)
|
155
|
-
|
430
|
+
all? { |op| op.respond_to?(:matches?) ? op.matches?(record) : true }
|
431
|
+
end
|
432
|
+
|
433
|
+
# Minimize the operation
|
434
|
+
#
|
435
|
+
# @return [self]
|
436
|
+
# the minimized AndOperation
|
437
|
+
# @return [AbstractOperation, AbstractComparison, Array]
|
438
|
+
# the minimized operation
|
439
|
+
#
|
440
|
+
# @api semipublic
|
441
|
+
def minimize
|
442
|
+
minimize_operands
|
443
|
+
|
444
|
+
return Operation.new(:null) if any? && all? { |op| op.nil? }
|
445
|
+
|
446
|
+
prune_operands
|
447
|
+
|
448
|
+
one? ? first : self
|
156
449
|
end
|
157
450
|
end # class AndOperation
|
158
451
|
|
@@ -161,47 +454,182 @@ module DataMapper
|
|
161
454
|
|
162
455
|
slug :or
|
163
456
|
|
164
|
-
#
|
457
|
+
# Match the record
|
458
|
+
#
|
459
|
+
# @param [Resource, Hash] record
|
460
|
+
# the resource to match
|
461
|
+
#
|
462
|
+
# @return [true]
|
463
|
+
# true if the record matches, false if not
|
464
|
+
#
|
165
465
|
# @api semipublic
|
166
466
|
def matches?(record)
|
167
|
-
|
467
|
+
any? { |op| op.respond_to?(:matches?) ? op.matches?(record) : true }
|
468
|
+
end
|
469
|
+
|
470
|
+
# Test if the operation is valid
|
471
|
+
#
|
472
|
+
# An OrOperation is valid if one of it's operands is valid.
|
473
|
+
#
|
474
|
+
# @return [Boolean]
|
475
|
+
# true if the operation is valid, false if not
|
476
|
+
#
|
477
|
+
# @api semipublic
|
478
|
+
def valid?
|
479
|
+
any? { |op| valid_operand?(op) }
|
480
|
+
end
|
481
|
+
|
482
|
+
# Minimize the operation
|
483
|
+
#
|
484
|
+
# @return [self]
|
485
|
+
# the minimized OrOperation
|
486
|
+
# @return [AbstractOperation, AbstractComparison, Array]
|
487
|
+
# the minimized operation
|
488
|
+
#
|
489
|
+
# @api semipublic
|
490
|
+
def minimize
|
491
|
+
minimize_operands
|
492
|
+
|
493
|
+
return Operation.new(:null) if any? { |op| op.nil? }
|
494
|
+
|
495
|
+
prune_operands
|
496
|
+
|
497
|
+
one? ? first : self
|
168
498
|
end
|
169
499
|
end # class OrOperation
|
170
500
|
|
171
501
|
class NotOperation < AbstractOperation
|
172
502
|
slug :not
|
173
503
|
|
174
|
-
#
|
504
|
+
# Match the record
|
505
|
+
#
|
506
|
+
# @param [Resource, Hash] record
|
507
|
+
# the resource to match
|
508
|
+
#
|
509
|
+
# @return [true]
|
510
|
+
# true if the record matches, false if not
|
511
|
+
#
|
175
512
|
# @api semipublic
|
176
513
|
def matches?(record)
|
177
|
-
|
514
|
+
operand = self.operand
|
515
|
+
operand.respond_to?(:matches?) ? !operand.matches?(record) : true
|
178
516
|
end
|
179
517
|
|
180
|
-
#
|
518
|
+
# Add an operand to the operation
|
519
|
+
#
|
520
|
+
# This will only allow a single operand to be added.
|
521
|
+
#
|
522
|
+
# @param [AbstractOperation, AbstractComparison, Array] operand
|
523
|
+
# the operand to add
|
524
|
+
#
|
525
|
+
# @return [self]
|
526
|
+
# the operation
|
527
|
+
#
|
181
528
|
# @api semipublic
|
182
529
|
def <<(operand)
|
183
|
-
|
530
|
+
assert_one_operand(operand)
|
531
|
+
assert_no_self_reference(operand)
|
184
532
|
super
|
185
533
|
end
|
186
534
|
|
187
|
-
|
535
|
+
# Return the only operand in the operation
|
536
|
+
#
|
537
|
+
# @return [AbstractOperation, AbstractComparison, Array]
|
538
|
+
# the operand
|
539
|
+
#
|
540
|
+
# @api semipublic
|
541
|
+
def operand
|
542
|
+
first
|
543
|
+
end
|
188
544
|
|
189
|
-
#
|
545
|
+
# Minimize the operation
|
546
|
+
#
|
547
|
+
# @return [self]
|
548
|
+
# the minimized NotOperation
|
549
|
+
# @return [AbstractOperation, AbstractComparison, Array]
|
550
|
+
# the minimized operation
|
551
|
+
#
|
190
552
|
# @api semipublic
|
191
|
-
def
|
192
|
-
|
193
|
-
|
553
|
+
def minimize
|
554
|
+
minimize_operands
|
555
|
+
prune_operands
|
556
|
+
|
557
|
+
# factor out double negatives if possible
|
558
|
+
operand = self.operand
|
559
|
+
one? && instance_of?(operand.class) ? operand.operand : self
|
560
|
+
end
|
561
|
+
|
562
|
+
# Return the string representation of the operation
|
563
|
+
#
|
564
|
+
# @return [String]
|
565
|
+
# the string representation of the operation
|
566
|
+
#
|
567
|
+
# @api semipublic
|
568
|
+
def to_s
|
569
|
+
empty? ? '' : "NOT(#{operand.to_s})"
|
570
|
+
end
|
571
|
+
|
572
|
+
# Test if the operation is negated
|
573
|
+
#
|
574
|
+
# Defaults to return false.
|
575
|
+
#
|
576
|
+
# @return [Boolean]
|
577
|
+
# true if the operation is negated, false if not
|
578
|
+
#
|
579
|
+
# @api private
|
580
|
+
def negated?
|
581
|
+
parent = self.parent
|
582
|
+
parent ? !parent.negated? : true
|
583
|
+
end
|
584
|
+
|
585
|
+
private
|
586
|
+
|
587
|
+
# Assert there is only one operand
|
588
|
+
#
|
589
|
+
# @param [AbstractOperation, AbstractComparison, Array] operand
|
590
|
+
# the operand to test
|
591
|
+
#
|
592
|
+
# @return [undefined]
|
593
|
+
#
|
594
|
+
# @raise [ArgumentError]
|
595
|
+
# raised if the operand is not a valid type
|
596
|
+
#
|
597
|
+
# @api private
|
598
|
+
def assert_one_operand(operand)
|
599
|
+
unless empty? || self.operand == operand
|
600
|
+
raise ArgumentError, "#{self.class} cannot have more than one operand"
|
601
|
+
end
|
602
|
+
end
|
603
|
+
|
604
|
+
# Assert the operand is not equal to self
|
605
|
+
#
|
606
|
+
# @param [AbstractOperation, AbstractComparison, Array] operand
|
607
|
+
# the operand to test
|
608
|
+
#
|
609
|
+
# @return [undefined]
|
610
|
+
#
|
611
|
+
# @raise [ArgumentError]
|
612
|
+
# raised if object is appended to itself
|
613
|
+
#
|
614
|
+
# @api private
|
615
|
+
def assert_no_self_reference(operand)
|
616
|
+
if equal?(operand)
|
617
|
+
raise ArgumentError, 'cannot append operand to itself'
|
618
|
+
end
|
194
619
|
end
|
195
620
|
end # class NotOperation
|
196
621
|
|
197
622
|
class NullOperation < AbstractOperation
|
198
623
|
undef_method :<<
|
624
|
+
undef_method :merge
|
199
625
|
|
200
626
|
slug :null
|
201
627
|
|
202
628
|
# Match the record
|
203
629
|
#
|
204
|
-
#
|
630
|
+
# A NullOperation matches every record.
|
631
|
+
#
|
632
|
+
# @param [Resource, Hash] record
|
205
633
|
# the resource to match
|
206
634
|
#
|
207
635
|
# @return [true]
|
@@ -209,11 +637,13 @@ module DataMapper
|
|
209
637
|
#
|
210
638
|
# @api semipublic
|
211
639
|
def matches?(record)
|
212
|
-
|
640
|
+
record.kind_of?(Hash) || record.kind_of?(Resource)
|
213
641
|
end
|
214
642
|
|
215
643
|
# Test validity of the operation
|
216
644
|
#
|
645
|
+
# A NullOperation is always valid.
|
646
|
+
#
|
217
647
|
# @return [true]
|
218
648
|
# always valid
|
219
649
|
#
|
@@ -241,6 +671,18 @@ module DataMapper
|
|
241
671
|
def inspect
|
242
672
|
'nil'
|
243
673
|
end
|
674
|
+
|
675
|
+
private
|
676
|
+
|
677
|
+
# Initialize a NullOperation
|
678
|
+
#
|
679
|
+
# @return [NullOperation]
|
680
|
+
# the operation
|
681
|
+
#
|
682
|
+
# @api semipublic
|
683
|
+
def initialize
|
684
|
+
@operands = Set.new
|
685
|
+
end
|
244
686
|
end
|
245
687
|
end # module Conditions
|
246
688
|
end # class Query
|