Class: TTY::Coercer::Range

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/coercer/range.rb

Overview

A class responsible for range type coercion

Class Method Summary collapse

Class Method Details

.coerce(value) ⇒ Object

Coerce value to Range type with possible ranges

Examples:

coerce('0-9') # => (0..9)

Parameters:

  • value (Object)


17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tty/coercer/range.rb', line 17

def self.coerce(value)
  case value.to_s
  when /\A(\-?\d+)\Z/
    ::Range.new($1.to_i, $1.to_i)
  when /\A(-?\d+?)(\.{2}\.?|-|,)(-?\d+)\Z/
    ::Range.new($1.to_i, $3.to_i, $2 == '...')
  when /\A(\w)(\.{2}\.?|-|,)(\w)\Z/
    ::Range.new($1.to_s, $3.to_s, $2 == '...')
  else
    raise InvalidArgument, "#{value} could not be coerced into Range type"
  end
end