-
Notifications
You must be signed in to change notification settings - Fork 93
/
integer_filter.rb
46 lines (40 loc) · 1.31 KB
/
integer_filter.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
module Mutations
class IntegerFilter < AdditionalFilter
@default_options = {
:nils => false, # true allows an explicit nil to be valid. Overrides any other options
:empty_is_nil => false, # if true, treat empty string as if it were nil
:min => nil, # lowest value, inclusive
:max => nil, # highest value, inclusive
:in => nil, # Can be an array like %w(3 4 5)
}
def filter(data)
# Handle nil case
if data.nil?
return [nil, nil] if options[:nils]
return [nil, :nils]
end
# Now check if it's empty:
if data == ""
if options[:empty_is_nil]
return [nil, (:nils unless options[:nils])]
else
return [data, :empty]
end
end
# Ensure it's the correct data type (Integer)
if !data.is_a?(Integer)
if data.is_a?(String) && data =~ /^-?\d/
data = data.to_i
else
return [data, :integer]
end
end
return [data, :min] if options[:min] && data < options[:min]
return [data, :max] if options[:max] && data > options[:max]
# Ensure it matches `in`
return [data, :in] if options[:in] && !options[:in].include?(data)
# We win, it's valid!
[data, nil]
end
end
end