This repository has been archived by the owner on Apr 17, 2018. It is now read-only.
/
operation.rb
720 lines (646 loc) · 18.5 KB
/
operation.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
module DataMapper
class Query
module Conditions
class Operation
# Factory method to initialize an operation
#
# @example
# operation = Operation.new(:and, comparison)
#
# @param [Symbol] slug
# the identifier for the operation class
# @param [Array] *operands
# the operands to initialize the operation with
#
# @return [AbstractOperation]
# the operation matching the slug
#
# @api semipublic
def self.new(slug, *operands)
if klass = operation_class(slug)
klass.new(*operands)
else
raise ArgumentError, "No Operation class for #{slug.inspect} has been defined"
end
end
# Return an Array of all the slugs for the operation classes
#
# @return [Array]
# the slugs of all the operation classes
#
# @api private
def self.slugs
AbstractOperation.descendants.map { |operation_class| operation_class.slug }
end
class << self
private
# Returns a Hash mapping the slugs to each class
#
# @return [Hash]
# Hash mapping the slug to the class
#
# @api private
def operation_classes
@operation_classes ||= {}
end
# Lookup the operation class based on the slug
#
# @example
# operation_class = Operation.operation_class(:and)
#
# @param [Symbol] slug
# the identifier for the operation class
#
# @return [Class]
# the operation class
#
# @api private
def operation_class(slug)
operation_classes[slug] ||= AbstractOperation.descendants.detect { |operation_class| operation_class.slug == slug }
end
end
end # class Operation
class AbstractOperation
include Extlib::Assertions
include Enumerable
extend Equalizer
equalize :slug, :sorted_operands
# Returns the parent operation
#
# @return [AbstractOperation]
# the parent operation
#
# @api semipublic
attr_accessor :parent
# Returns the child operations and comparisons
#
# @return [Set<AbstractOperation, AbstractComparison, Array>]
# the set of operations and comparisons
#
# @api semipublic
attr_reader :operands
alias children operands
# Returns the classes that inherit from AbstractComparison
#
# @return [Set]
# the descendant classes
#
# @api private
def self.descendants
@descendants ||= Set.new
end
# Hook executed when inheriting from AbstractComparison
#
# @return [undefined]
#
# @api private
def self.inherited(operation_class)
descendants << operation_class
end
# Get and set the slug for the operation class
#
# @param [Symbol] slug
# optionally set the slug for the operation class
#
# @return [Symbol]
# the slug for the operation class
#
# @api semipublic
def self.slug(slug = nil)
slug ? @slug = slug : @slug
end
# Return the comparison class slug
#
# @return [Symbol]
# the comparison class slug
#
# @api private
def slug
self.class.slug
end
# Get the first operand
#
# @return [AbstractOperation, AbstractComparison, Array]
# returns the first operand
#
# @api semipublic
def first
each { |operand| return operand }
nil
end
# Iterate through each operand in the operation
#
# @yield [operand]
# yields to each operand
#
# @yieldparam [AbstractOperation, AbstractComparison, Array] operand
# each operand
#
# @return [self]
# returns the operation
#
# @api semipublic
def each
@operands.each { |op| yield op }
self
end
# Test to see if there are operands
#
# @return [Boolean]
# returns true if there are operands
#
# @api semipublic
def empty?
@operands.empty?
end
# Test to see if there is one operand
#
# @return [Boolean]
# true if there is only one operand
#
# @api semipublic
def one?
@operands.size == 1
end
# Test if the operation is valid
#
# @return [Boolean]
# true if the operation is valid, false if not
#
# @api semipublic
def valid?
any? && all? { |op| valid_operand?(op) }
end
# Add an operand to the operation
#
# @param [AbstractOperation, AbstractComparison, Array] operand
# the operand to add
#
# @return [self]
# the operation
#
# @api semipublic
def <<(operand)
assert_valid_operand_type(operand)
@operands << relate_operand(operand)
self
end
# Add operands to the operation
#
# @param [#each] operands
# the operands to add
#
# @return [self]
# the operation
#
# @api semipublic
def merge(operands)
operands.each { |op| self << op }
self
end
# Return the union with another operand
#
# @param [AbstractOperation] other
# the operand to union with
#
# @return [OrOperation]
# the union of the operation and operand
#
# @api semipublic
def union(other)
Operation.new(:or, dup, other.dup).minimize
end
alias | union
alias + union
# Return the intersection of the operation and another operand
#
# @param [AbstractOperation] other
# the operand to intersect with
#
# @return [AndOperation]
# the intersection of the operation and operand
#
# @api semipublic
def intersection(other)
Operation.new(:and, dup, other.dup).minimize
end
alias & intersection
# Return the difference of the operation and another operand
#
# @param [AbstractOperation] other
# the operand to not match
#
# @return [AndOperation]
# the intersection of the operation and operand
#
# @api semipublic
def difference(other)
Operation.new(:and, dup, Operation.new(:not, other.dup)).minimize
end
alias - difference
# Minimize the operation
#
# @return [self]
# the minimized operation
#
# @api semipublic
def minimize
self
end
# Clear the operands
#
# @return [self]
# the operation
#
# @api semipublic
def clear
@operands.clear
self
end
# Return the string representation of the operation
#
# @return [String]
# the string representation of the operation
#
# @api semipublic
def to_s
empty? ? '' : "(#{sort_by { |op| op.to_s }.map { |op| op.to_s }.join(" #{slug.to_s.upcase} ")})"
end
# Test if the operation is negated
#
# Defaults to return false.
#
# @return [Boolean]
# true if the operation is negated, false if not
#
# @api private
def negated?
parent = self.parent
parent ? parent.negated? : false
end
# Return a list of operands in predictable order
#
# @return [Array<AbstractOperation, AbstractComparison, Array>]
# list of operands sorted in deterministic order
#
# @api private
def sorted_operands
sort_by { |op| op.hash }
end
private
# Initialize an operation
#
# @param [Array<AbstractOperation, AbstractComparison, Array>] *operands
# the operands to include in the operation
#
# @return [AbstractOperation]
# the operation
#
# @api semipublic
def initialize(*operands)
@operands = Set.new
merge(operands)
end
# Copy an operation
#
# @param [AbstractOperation] original
# the original operation
#
# @return [undefined]
#
# @api semipublic
def initialize_copy(*)
@operands = map { |op| op.dup }.to_set
end
# Minimize the operands recursively
#
# @return [undefined]
#
# @api private
def minimize_operands
# FIXME: why does Set#map! not work here?
@operands = map do |op|
relate_operand(op.respond_to?(:minimize) ? op.minimize : op)
end.to_set
end
# Prune empty operands recursively
#
# @return [undefined]
#
# @api private
def prune_operands
@operands.delete_if { |op| op.respond_to?(:empty?) ? op.empty? : false }
end
# Test if the operand is valid
#
# @param [AbstractOperation, AbstractComparison, Array] operand
# the operand to test
#
# @return [Boolean]
# true if the operand is valid
#
# @api private
def valid_operand?(operand)
if operand.respond_to?(:valid?)
operand.valid?
else
true
end
end
# Set self to be the operand's parent
#
# @return [AbstractOperation, AbstractComparison, Array]
# the operand that was related to self
#
# @api privTE
def relate_operand(operand)
operand.parent = self if operand.respond_to?(:parent=)
operand
end
# Assert that the operand is a valid type
#
# @param [AbstractOperation, AbstractComparison, Array] operand
# the operand to test
#
# @return [undefined]
#
# @raise [ArgumentError]
# raised if the operand is not a valid type
#
# @api private
def assert_valid_operand_type(operand)
assert_kind_of 'operand', operand, AbstractOperation, AbstractComparison, Array
end
end # class AbstractOperation
module FlattenOperation
# Add an operand to the operation, flattening the same types
#
# Flattening means that if the operand is the same as the
# operation, we should just include the operand's operands
# in the operation and prune that part of the tree. This results
# in a shallower tree, is faster to match and usually generates
# more efficient queries in the adapters.
#
# @param [AbstractOperation, AbstractComparison, Array] operand
# the operand to add
#
# @return [self]
# the operation
#
# @api semipublic
def <<(operand)
if kind_of?(operand.class)
merge(operand.operands)
else
super
end
end
end # module FlattenOperation
class AndOperation < AbstractOperation
include FlattenOperation
slug :and
# Match the record
#
# @example with a Hash
# operation.matches?({ :id => 1 }) # => true
#
# @example with a Resource
# operation.matches?(Blog::Article.new(:id => 1)) # => true
#
# @param [Resource, Hash] record
# the resource to match
#
# @return [true]
# true if the record matches, false if not
#
# @api semipublic
def matches?(record)
all? { |op| op.respond_to?(:matches?) ? op.matches?(record) : true }
end
# Minimize the operation
#
# @return [self]
# the minimized AndOperation
# @return [AbstractOperation, AbstractComparison, Array]
# the minimized operation
#
# @api semipublic
def minimize
minimize_operands
return Operation.new(:null) if any? && all? { |op| op.nil? }
prune_operands
one? ? first : self
end
end # class AndOperation
class OrOperation < AbstractOperation
include FlattenOperation
slug :or
# Match the record
#
# @param [Resource, Hash] record
# the resource to match
#
# @return [true]
# true if the record matches, false if not
#
# @api semipublic
def matches?(record)
any? { |op| op.respond_to?(:matches?) ? op.matches?(record) : true }
end
# Test if the operation is valid
#
# An OrOperation is valid if one of it's operands is valid.
#
# @return [Boolean]
# true if the operation is valid, false if not
#
# @api semipublic
def valid?
any? { |op| valid_operand?(op) }
end
# Minimize the operation
#
# @return [self]
# the minimized OrOperation
# @return [AbstractOperation, AbstractComparison, Array]
# the minimized operation
#
# @api semipublic
def minimize
minimize_operands
return Operation.new(:null) if any? { |op| op.nil? }
prune_operands
one? ? first : self
end
end # class OrOperation
class NotOperation < AbstractOperation
slug :not
# Match the record
#
# @param [Resource, Hash] record
# the resource to match
#
# @return [true]
# true if the record matches, false if not
#
# @api semipublic
def matches?(record)
operand = self.operand
operand.respond_to?(:matches?) ? !operand.matches?(record) : true
end
# Add an operand to the operation
#
# This will only allow a single operand to be added.
#
# @param [AbstractOperation, AbstractComparison, Array] operand
# the operand to add
#
# @return [self]
# the operation
#
# @api semipublic
def <<(operand)
assert_one_operand(operand)
assert_no_self_reference(operand)
super
end
# Return the only operand in the operation
#
# @return [AbstractOperation, AbstractComparison, Array]
# the operand
#
# @api semipublic
def operand
first
end
# Minimize the operation
#
# @return [self]
# the minimized NotOperation
# @return [AbstractOperation, AbstractComparison, Array]
# the minimized operation
#
# @api semipublic
def minimize
minimize_operands
prune_operands
# factor out double negatives if possible
operand = self.operand
one? && instance_of?(operand.class) ? operand.operand : self
end
# Return the string representation of the operation
#
# @return [String]
# the string representation of the operation
#
# @api semipublic
def to_s
empty? ? '' : "NOT(#{operand.to_s})"
end
# Test if the operation is negated
#
# Defaults to return false.
#
# @return [Boolean]
# true if the operation is negated, false if not
#
# @api private
def negated?
parent = self.parent
parent ? !parent.negated? : true
end
private
# Assert there is only one operand
#
# @param [AbstractOperation, AbstractComparison, Array] operand
# the operand to test
#
# @return [undefined]
#
# @raise [ArgumentError]
# raised if the operand is not a valid type
#
# @api private
def assert_one_operand(operand)
unless empty? || self.operand == operand
raise ArgumentError, "#{self.class} cannot have more than one operand"
end
end
# Assert the operand is not equal to self
#
# @param [AbstractOperation, AbstractComparison, Array] operand
# the operand to test
#
# @return [undefined]
#
# @raise [ArgumentError]
# raised if object is appended to itself
#
# @api private
def assert_no_self_reference(operand)
if equal?(operand)
raise ArgumentError, 'cannot append operand to itself'
end
end
end # class NotOperation
class NullOperation < AbstractOperation
undef_method :<<
undef_method :merge
slug :null
# Match the record
#
# A NullOperation matches every record.
#
# @param [Resource, Hash] record
# the resource to match
#
# @return [true]
# every record matches
#
# @api semipublic
def matches?(record)
record.kind_of?(Hash) || record.kind_of?(Resource)
end
# Test validity of the operation
#
# A NullOperation is always valid.
#
# @return [true]
# always valid
#
# @api semipublic
def valid?
true
end
# Treat the operation the same as nil
#
# @return [true]
# should be treated as nil
#
# @api semipublic
def nil?
true
end
# Inspecting the operation should return the same as nil
#
# @return [String]
# return the string 'nil'
#
# @api semipublic
def inspect
'nil'
end
private
# Initialize a NullOperation
#
# @return [NullOperation]
# the operation
#
# @api semipublic
def initialize
@operands = Set.new
end
end
end # module Conditions
end # class Query
end # module DataMapper