Class: Stretchy::Types::Range
- Inherits:
-
Base
- Object
- Base
- Stretchy::Types::Range
show all
- Defined in:
- lib/stretchy/types/range.rb
Instance Method Summary
collapse
#errors, included, #require_one!, #require_only_one!, #valid?, #validate!, #validator
Constructor Details
#initialize(opts_or_range = {}, options = {}) ⇒ Range
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
|
# File 'lib/stretchy/types/range.rb', line 17
def initialize(opts_or_range = {}, options = {})
case opts_or_range
when ::Range
@min = opts_or_range.min
@max = opts_or_range.max
@exclusive_min = !!(options[:exclusive_min] || options[:exclusive])
@exclusive_max = !!(options[:exclusive_max] || options[:exclusive])
@inverse = !!options[:inverse]
@should = !!options[:should]
when ::Hash
opts = options.merge(opts_or_range)
@min = opts[:min]
@max = opts[:max]
@exclusive_min = !!(opts[:exclusive_min] || opts[:exclusive])
@exclusive_max = !!(opts[:exclusive_max] || opts[:exclusive])
@inverse = !!opts[:inverse]
@should = !!opts[:should]
when Range
@min = opts_or_range.min
@max = opts_or_range.max
@exclusive_min = opts_or_range.exclusive_min
@exclusive_max = opts_or_range.exclusive_max
else
raise Stretchy::Errors::ContractError.new("Ranges must be a range or a hash - found #{options.class.name}")
end
require_one! :min, :max
validate!
end
|
Instance Method Details
#empty? ⇒ Boolean
48
49
50
|
# File 'lib/stretchy/types/range.rb', line 48
def empty?
!(@min || @max)
end
|
#to_search ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/stretchy/types/range.rb', line 52
def to_search
json = {}
if @exclusive_min && @min
json[:gt] = @min
elsif @min
json[:gte] = @min
end
if @exclusive_max && @max
json[:lt] = @max
elsif @max
json[:lte] = @max
end
json
end
|