class Deranged::Parser

Attributes

string[R]

Public Class Methods

new(string) click to toggle source
# File lib/deranged/parser.rb, line 5
def initialize(string)
  @string = string.to_s
end

Public Instance Methods

to_range() click to toggle source
# File lib/deranged/parser.rb, line 9
def to_range
  return nil if numbers.empty?
  numbers.first..numbers.last
end
to_range!() click to toggle source
# File lib/deranged/parser.rb, line 14
def to_range!
  raise InvalidRangeString if to_range.nil?
  to_range
end

Private Instance Methods

cast_method() click to toggle source
# File lib/deranged/parser.rb, line 41
def cast_method
  sanitized_string =~ /\.[0-9]/ ? :to_f : :to_i
end
numbers() click to toggle source
# File lib/deranged/parser.rb, line 23
def numbers
  @numbers ||= sanitized_string.split.map(&cast_method)
end
sanitized_string() click to toggle source

TODO: support non-US number formats

# File lib/deranged/parser.rb, line 28
def sanitized_string
  @sanitized_string ||= (
    str = string.dup
    # Remove thousand separator commas
    str.gsub! /(\d)(,)(?=\d{3}\b)/, '\1'

    # replace any non number or decimal characters with spaces
    str.gsub! /[^0-9\.]/, ' '

    str
  )
end