class Necromancer::RangeConverters::StringToRangeConverter
An object that converts a String to a Range
Public Instance Methods
call(value, strict: config.strict)
click to toggle source
Convert value to Range type with possible ranges
@param [Object] value
@example
converter.call("0,9") # => (0..9)
@example
converter.call("0-9") # => (0..9)
@api public
# File lib/necromancer/converters/range.rb, line 34 def call(value, strict: config.strict) if match = value.match(SINGLE_DIGIT_MATCHER) digit = cast_to_num(match[:digit]) ::Range.new(digit, digit) elsif match = value.match(DIGIT_MATCHER) open = cast_to_num(match[:open]) close = cast_to_num(match[:close]) ::Range.new(open, close, match[:sep].gsub(/\s*/, "") == "...") elsif match = value.match(LETTER_MATCHER) ::Range.new(match[:open], match[:close], match[:sep].gsub(/\s*/, "") == "...") else strict ? raise_conversion_type(value) : value end end
cast_to_num(str)
click to toggle source
Convert range end to numeric value
@api private
# File lib/necromancer/converters/range.rb, line 53 def cast_to_num(str) Integer(str) rescue ArgumentError Float(str) rescue ArgumentError nil end