Class: Necromancer::RangeConverters::StringToRangeConverter

Inherits:
Converter
  • Object
show all
Defined in:
lib/necromancer/converters/range.rb

Overview

An object that converts a String to a Range

Instance Attribute Summary

Attributes inherited from Converter

#convert, #source, #target

Instance Method Summary collapse

Methods inherited from Converter

create, #fail_conversion_type, #initialize

Constructor Details

This class inherits a constructor from Necromancer::Converter

Instance Method Details

#call(value, options = {}) ⇒ Object

Convert value to Range type with possible ranges

Examples:

converter.call('0,9')  # => (0..9)
converter.call('0-9')  # => (0..9)


28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/necromancer/converters/range.rb', line 28

def call(value, options = {})
  strict = options.fetch(:strict, config.strict)
  case value
  when SINGLE_DIGIT_MATCHER
    ::Range.new($1.to_i, $1.to_i)
  when DIGIT_MATCHER
    ::Range.new($1.to_i, $3.to_i, $2 == '...')
  when LETTER_MATCHER
    ::Range.new($1.to_s, $3.to_s, $2 == '...')
  else
    strict ? fail_conversion_type(value) : value
  end
end