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

#advanced?, #alias?, #choices, #default, #help_message_for_default, #help_messages, #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

#append_help_messagesVoid

Returns:

  • (Void)


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

def append_help_messages
  option << "Range separator to use: '#{separator}'"

  super
end

#separatorString

Returns:

  • (String)


30
31
32
# File 'lib/opt_parse_validator/opts/integer_range.rb', line 30

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

#validate(value) ⇒ Range

Parameters:

  • value (String)

Returns:

  • (Range)

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/opt_parse_validator/opts/integer_range.rb', line 16

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

  raise Error, "Incorrect number of ranges found: #{a.size}, should be 2" unless a.size == 2

  first_integer = a.first.to_i
  last_integer  = a.last.to_i

  raise Error, 'Argument is not a valid integer range' unless first_integer.to_s == a.first && last_integer.to_s == a.last

  (first_integer..last_integer)
end