Class: OptParseValidator::OptIntegerRange

Inherits:
OptBase
  • Object
show all
Defined in:
lib/opt_parse_validator/opts/integer_range.rb

Overview

Implementation of the Integer Range Option

Instance Attribute Summary

Attributes inherited from OptBase

#attrs, #option, #required

Instance Method Summary collapse

Methods inherited from OptBase

#choices, #default, #initialize, #normalize, #required?, #required_unless, #to_long, #to_s, #to_sym, #value_if_empty

Constructor Details

This class inherits a constructor from OptParseValidator::OptBase

Instance Method Details

#separatorString

Returns:

  • (String)


17
18
19
# File 'lib/opt_parse_validator/opts/integer_range.rb', line 17

def separator
  attrs[:separator] || '-'
end

#validate(value) ⇒ Range

Parameters:

  • value (String)

Returns:

  • (Range)


7
8
9
10
11
12
13
14
# File 'lib/opt_parse_validator/opts/integer_range.rb', line 7

def validate(value)
  a = super(value).split(separator)

  fail "Incorrect number of ranges found: #{a.size}, should be 2" unless a.size == 2
  fail 'Argument is not a valid integer range' unless a.first.to_i.to_s == a.first && a.last.to_i.to_s == a.last

  (a.first.to_i..a.last.to_i)
end