-
Notifications
You must be signed in to change notification settings - Fork 39
/
attributes.rb
301 lines (237 loc) · 8.97 KB
/
attributes.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
require "treetop"
module SearchCopGrammar
module Attributes
class Collection
attr_reader :query_info, :key
INCLUDED_OPERATORS = [:matches, :eq, :not_eq, :gt, :gteq, :lt, :lteq].freeze
def initialize(query_info, key)
raise(SearchCop::UnknownColumn, "Unknown column #{key}") unless query_info.scope.reflection.attributes[key]
@query_info = query_info
@key = key
end
def eql?(other)
self == other
end
def ==(other)
other.is_a?(self.class) && [query_info.model, key] == [query_info.model, other.key]
end
def hash
[query_info.model, key].hash
end
[:eq, :not_eq, :lt, :lteq, :gt, :gteq].each do |method|
define_method method do |value|
attributes.collect! { |attribute| attribute.send method, value }.inject(:or)
end
end
def generator(generator, value)
attributes.collect! do |attribute|
SearchCopGrammar::Nodes::Generator.new(attribute, generator: generator, value: value)
end.inject(:or)
end
def matches(value)
if fulltext?
SearchCopGrammar::Nodes::MatchesFulltext.new self, value.to_s
else
attributes.collect! { |attribute| attribute.matches value }.inject(:or)
end
end
def fulltext?
(query_info.scope.reflection.options[key] || {})[:type] == :fulltext
end
def compatible?(value)
attributes.all? { |attribute| attribute.compatible? value }
end
def options
query_info.scope.reflection.options[key]
end
def attributes
@attributes ||= query_info.scope.reflection.attributes[key].collect { |attribute_definition| attribute_for attribute_definition }
end
def klass_for_association(name)
reflections = query_info.model.reflections
return reflections[name].klass if reflections[name]
return reflections[name.to_sym].klass if reflections[name.to_sym]
nil
end
def klass_for(name)
alias_value = query_info.scope.reflection.aliases[name]
return alias_value if alias_value.is_a?(Class)
value = alias_value || name
klass_for_association(value) || value.classify.constantize
end
def alias_for(name)
(query_info.scope.reflection.aliases[name] && name) || klass_for(name).table_name
end
def attribute_for(attribute_definition)
query_info.references.push attribute_definition
table, column_with_fields = attribute_definition.split(".")
column, *fields = column_with_fields.split("->")
klass = klass_for(table)
raise(SearchCop::UnknownAttribute, "Unknown attribute #{attribute_definition}") unless klass.columns_hash[column]
Attributes.const_get(klass.columns_hash[column].type.to_s.classify).new(klass, alias_for(table), column, fields, options)
end
def generator_for(name)
generators[name]
end
def valid_operator?(operator)
(INCLUDED_OPERATORS + generators.keys).include?(operator)
end
def generators
query_info.scope.reflection.generators
end
end
class Base
attr_reader :attribute, :table_alias, :column_name, :field_names, :options
def initialize(klass, table_alias, column_name, field_names, options = {})
@attribute = klass.arel_table.alias(table_alias)[column_name]
@klass = klass
@table_alias = table_alias
@column_name = column_name
@field_names = field_names
@options = (options || {})
end
def map(value)
value
end
def compatible?(value)
map value
true
rescue SearchCop::IncompatibleDatatype
false
end
def fulltext?
false
end
{ eq: "Equality", not_eq: "NotEqual", lt: "LessThan", lteq: "LessThanOrEqual", gt: "GreaterThan", gteq: "GreaterThanOrEqual", matches: "Matches" }.each do |method, class_name|
define_method method do |value|
raise(SearchCop::IncompatibleDatatype, "Incompatible datatype for #{value}") unless compatible?(value)
SearchCopGrammar::Nodes.const_get(class_name).new(self, map(value))
end
end
def method_missing(name, *args, &block)
if @attribute.respond_to?(name)
@attribute.send(name, *args, &block)
else
super
end
end
def respond_to_missing?(*args)
@attribute.respond_to?(*args) || super
end
end
class String < Base
def matches_value(value)
res = value.gsub(/[%_\\]/) { |char| "\\#{char}" }
if value.strip =~ /^\*|\*$/
res = res.gsub(/^\*/, "%") if options[:left_wildcard] != false
res = res.gsub(/\*$/, "%") if options[:right_wildcard] != false
return res
end
res = "%#{res}" if options[:left_wildcard] != false
res = "#{res}%" if options[:right_wildcard] != false
res
end
def matches(value)
super matches_value(value)
end
end
class Text < String; end
class Jsonb < String; end
class Json < String; end
class Hstore < String; end
class WithoutMatches < Base
def matches(value)
eq value
end
end
class Float < WithoutMatches
def compatible?(value)
return true if value.to_s =~ /^-?[0-9]+(\.[0-9]+)?$/
false
end
def map(value)
value.to_f
end
end
class Integer < Float
def map(value)
value.to_i
end
end
class Decimal < Float; end
class Datetime < WithoutMatches
def parse(value)
return value..value unless value.is_a?(::String)
if value =~ /^[0-9]+ (hour|day|week|month|year)s{0,1} (ago)$/
number, period, ago = value.split(" ")
time = number.to_i.send(period.to_sym).send(ago.to_sym)
time..::Time.now
elsif value =~ /^[0-9]{4}$/
::Time.new(value).beginning_of_year..::Time.new(value).end_of_year
elsif value =~ %r{^([0-9]{4})(\.|-|/)([0-9]{1,2})$}
::Time.new(Regexp.last_match(1), Regexp.last_match(3), 15).beginning_of_month..::Time.new(Regexp.last_match(1), Regexp.last_match(3), 15).end_of_month
elsif value =~ %r{^([0-9]{1,2})(\.|-|/)([0-9]{4})$}
::Time.new(Regexp.last_match(3), Regexp.last_match(1), 15).beginning_of_month..::Time.new(Regexp.last_match(3), Regexp.last_match(1), 15).end_of_month
elsif value =~ %r{^[0-9]{4}(\.|-|/)[0-9]{1,2}(\.|-|/)[0-9]{1,2}$} || value =~ %r{^[0-9]{1,2}(\.|-|/)[0-9]{1,2}(\.|-|/)[0-9]{4}$}
time = ::Time.parse(value)
time.beginning_of_day..time.end_of_day
elsif value =~ %r{[0-9]{4}(\.|-|/)[0-9]{1,2}(\.|-|/)[0-9]{1,2}} || value =~ %r{[0-9]{1,2}(\.|-|/)[0-9]{1,2}(\.|-|/)[0-9]{4}}
time = ::Time.parse(value)
time..time
else
raise ArgumentError
end
rescue ArgumentError
raise SearchCop::IncompatibleDatatype, "Incompatible datatype for #{value}"
end
def map(value)
parse(value).first
end
def eq(value)
between parse(value)
end
def not_eq(value)
between(parse(value)).not
end
def gt(value)
super parse(value).last
end
def between(range)
gteq(range.first).and(lteq(range.last))
end
end
class Timestamp < Datetime; end
class Timestamptz < Datetime; end
class Date < Datetime
def parse(value)
return value..value unless value.is_a?(::String)
if value =~ /^[0-9]+ (day|week|month|year)s{0,1} (ago)$/
number, period, ago = value.split(" ")
time = number.to_i.send(period.to_sym).send(ago.to_sym)
time.to_date..::Date.today
elsif value =~ /^[0-9]{4}$/
::Date.new(value.to_i).beginning_of_year..::Date.new(value.to_i).end_of_year
elsif value =~ %r{^([0-9]{4})(\.|-|/)([0-9]{1,2})$}
::Date.new(Regexp.last_match(1).to_i, Regexp.last_match(3).to_i, 15).beginning_of_month..::Date.new(Regexp.last_match(1).to_i, Regexp.last_match(3).to_i, 15).end_of_month
elsif value =~ %r{^([0-9]{1,2})(\.|-|/)([0-9]{4})$}
::Date.new(Regexp.last_match(3).to_i, Regexp.last_match(1).to_i, 15).beginning_of_month..::Date.new(Regexp.last_match(3).to_i, Regexp.last_match(1).to_i, 15).end_of_month
elsif value =~ %r{[0-9]{4}(\.|-|/)[0-9]{1,2}(\.|-|/)[0-9]{1,2}} || value =~ %r{[0-9]{1,2}(\.|-|/)[0-9]{1,2}(\.|-|/)[0-9]{4}}
date = ::Date.parse(value)
date..date
else
raise ArgumentError
end
rescue ArgumentError
raise SearchCop::IncompatibleDatatype, "Incompatible datatype for #{value}"
end
end
class Time < Datetime; end
class Boolean < WithoutMatches
def map(value)
return true if value.to_s =~ /^(1|true|yes)$/i
return false if value.to_s =~ /^(0|false|no)$/i
raise SearchCop::IncompatibleDatatype, "Incompatible datatype for #{value}"
end
end
end
end