-
Notifications
You must be signed in to change notification settings - Fork 53
/
range.rb
56 lines (46 loc) · 1.12 KB
/
range.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
# Search operation on a `Range`.
class MetasploitDataModels::Search::Operation::Range < Metasploit::Model::Search::Operation::Base
#
# CONSTANTS
#
# Separates beginning from end of the range.
SEPARATOR = '-'
#
# Validation
#
validate :ordered
validate :range
#
# Instance Methods
#
# Sets `#value` to a `Range` composed by separating `formatted_value` by `-`.
#
# @param formatted_value [#to_s]
# @return [Range<String>]
def value=(formatted_value)
range_arguments = formatted_value.to_s.split(SEPARATOR, 2)
begin
@value = Range.new(*range_arguments)
rescue ArgumentError
@value = formatted_value
end
@value
end
private
# Validates that `#value` is a `Range` with `Range#begin` less than or equal to `Range#begin`
#
# @return [void]
def ordered
if value.is_a?(Range) && value.begin > value.end
errors.add(:value, :order, begin: value.begin.inspect, end: value.end.inspect)
end
end
# Validates that `#value` is a `Range`
#
# @return [void]
def range
unless value.is_a? Range
errors.add(:value, :range)
end
end
end