This repository has been archived by the owner on Apr 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 158
/
comparison.rb
436 lines (356 loc) · 10.2 KB
/
comparison.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
module DataMapper
class Query
module Conditions
class Comparison
# TODO: document
# @api semipublic
def self.new(slug, subject, value)
if klass = comparison_class(slug)
klass.new(subject, value)
else
raise "No Comparison class for `#{slug.inspect}' has been defined"
end
end
# TODO: document
# @api semipublic
def self.comparison_class(slug)
comparison_classes[slug] ||= AbstractComparison.descendants.detect { |comparison_class| comparison_class.slug == slug }
end
# TODO: document
# @api private
def self.slugs
@slugs ||= AbstractComparison.descendants.map { |comparison_class| comparison_class.slug }.freeze
end
class << self
private
# TODO: document
# @api private
def comparison_classes
@comparison_classes ||= {}
end
end
end # class Comparison
class AbstractComparison
extend Deprecate
deprecate :property, :subject
# TODO: document
# @api semipublic
attr_reader :subject
# TODO: document
# @api semipublic
attr_reader :value
# TODO: document
# @api private
def self.descendants
@descendants ||= Set.new
end
# TODO: document
# @api private
def self.inherited(comparison_class)
descendants << comparison_class
end
# TODO: document
# @api semipublic
def self.slug(slug = nil)
slug ? @slug = slug : @slug
end
# TODO: document
# @api semipublic
def valid?
@valid
end
# TODO: document
# @api semipublic
def ==(other)
if equal?(other)
return true
end
unless other.class.respond_to?(:slug)
return false
end
unless other.respond_to?(:subject)
return false
end
unless other.respond_to?(:value)
return false
end
cmp?(other, :==)
end
# TODO: document
# @api semipublic
def eql?(other)
if equal?(other)
return true
end
unless instance_of?(other.class)
return false
end
cmp?(other, :eql?)
end
# TODO: document
# @api semipublic
def inspect
"#<#{self.class} @subject=#{@subject.inspect} @value=#{@value.inspect}>"
end
# TODO: document
# @api semipublic
def to_s
"#{@subject} #{comparator_string} #{@value}"
end
private
# TODO: document
# @api semipublic
attr_reader :expected
# TODO: document
# @api semipublic
def initialize(subject, value)
@subject = subject
@value = typecast_value(value)
@valid = valid_value?(@subject, @value)
@expected = expected_value
end
# TODO: document
# @api private
def initialize_copy(*)
@value = @value.dup
end
# TODO: document
# @api private
def cmp?(other, operator)
unless self.class.slug.send(operator, other.class.slug)
return false
end
unless subject.send(operator, other.subject)
return false
end
unless value.send(operator, other.value)
return false
end
true
end
# TODO: document
# @api private
def typecast_value(value)
if subject.respond_to?(:typecast)
subject.value(subject.typecast(value))
else
value
end
end
# TODO: document
# @api semipublic
def record_value(record, subject = @subject, key_type = :source_key)
case record
when Hash then record_value_from_hash(record, subject, key_type)
when Resource then record_value_from_resource(record, subject, key_type)
else
record
end
end
# TODO: document
# @api private
def record_value_from_hash(hash, subject, key_type)
hash.fetch subject, case subject
when Property
hash[subject.field]
when Associations::Relationship
subject.send(key_type).map { |property| record_value(hash, property, key_type) }
end
end
# TODO: document
# @api private
def record_value_from_resource(resource, subject, key_type)
case subject
when Property
subject.get!(resource)
when Associations::Relationship
subject.send(key_type).map { |property| record_value(resource, property, key_type) }
end
end
# TODO: document
# @api semipublic
def expected_value(value = @value)
expected_value = record_value(value, @subject, :target_key)
if @subject.respond_to?(:source_key)
@subject.source_key.typecast(expected_value)
else
expected_value
end
end
# TODO: document
# @api private
def valid_value?(subject, value)
subject.valid?(value)
end
# TODO: document
# @api private
def comparator_string
self.class.name.chomp('Comparison')
end
end # class AbstractComparison
class EqualToComparison < AbstractComparison
slug :eql
# TODO: document
# @api semipublic
def matches?(record)
record_value(record) == expected
end
private
# TODO: document
# @api private
def comparator_string
'='
end
end # class EqualToComparison
class InclusionComparison < AbstractComparison
slug :in
# TODO: document
# @api semipublic
def matches?(record)
record_value = record_value(record)
!record_value.nil? && expected.include?(record_value)
end
private
# TODO: document
# @api private
def typecast_value(value)
if subject.respond_to?(:typecast) && value.respond_to?(:map)
value.map { |val| subject.typecast(val) }
else
value
end
end
# TODO: document
# @api private
def valid_value?(subject, value)
unless value.kind_of?(Array) || value.kind_of?(Range) || value.kind_of?(Set)
return false
end
unless value.any?
return false
end
unless value.all? { |val| super(subject, val) }
return false
end
true
end
# TODO: document
# @api semipublic
def expected_value
@value.map { |value| super(value) }
end
# TODO: document
# @api private
def comparator_string
'IN'
end
end # class InclusionComparison
class RegexpComparison < AbstractComparison
slug :regexp
# TODO: document
# @api semipublic
def matches?(record)
record_value = record_value(record)
!record_value.nil? && record_value =~ expected
end
private
# TODO: document
# @api private
def typecast_value(value)
value
end
# TODO: document
# @api private
def valid_value?(subject, value)
value.kind_of?(Regexp)
end
# TODO: document
# @api private
def comparator_string
'=~'
end
end # class RegexpComparison
# TODO: move this to dm-more with DataObjectsAdapter plugins
class LikeComparison < AbstractComparison
slug :like
# TODO: document
# @api semipublic
def matches?(record)
record_value = record_value(record)
!record_value.nil? && record_value =~ expected
end
private
# TODO: document
# @api semipublic
def expected_value
Regexp.new(@value.to_s.gsub('%', '.*').gsub('_', '.'))
end
# TODO: document
# @api private
def comparator_string
'LIKE'
end
end # class LikeComparison
class GreaterThanComparison < AbstractComparison
slug :gt
# TODO: document
# @api semipublic
def matches?(record)
record_value = record_value(record)
!record_value.nil? && record_value > expected
end
private
# TODO: document
# @api private
def comparator_string
'>'
end
end # class GreaterThanComparison
class LessThanComparison < AbstractComparison
slug :lt
# TODO: document
# @api semipublic
def matches?(record)
record_value = record_value(record)
!record_value.nil? && record_value < expected
end
private
# TODO: document
# @api private
def comparator_string
'<'
end
end # class LessThanComparison
class GreaterThanOrEqualToComparison < AbstractComparison
slug :gte
# TODO: document
# @api semipublic
def matches?(record)
record_value = record_value(record)
!record_value.nil? && record_value >= expected
end
private
# TODO: document
# @api private
def comparator_string
'>='
end
end # class GreaterThanOrEqualToComparison
class LessThanOrEqualToComparison < AbstractComparison
slug :lte
# TODO: document
# @api semipublic
def matches?(record)
record_value = record_value(record)
!record_value.nil? && record_value <= expected
end
private
# TODO: document
# @api private
def comparator_string
'<='
end
end # class LessThanOrEqualToComparison
end # module Conditions
end # class Query
end # module DataMapper