module DateParser::Utils

Extra utility functions for broader use in the gem.

Public Class Methods

clean_out_punctuation(str) click to toggle source

Removes punctuation.

# File lib/date_parser/utils.rb, line 27
def Utils.clean_out_punctuation(str)
  str.gsub(/[^a-z0-9\s\/-]/i, '')
end
clean_str(str) click to toggle source

Removes punctuation and downcases the str.

# File lib/date_parser/utils.rb, line 32
def Utils.clean_str(str)
  clean_out_punctuation(str).downcase
end
delete_at_indices(array, range) click to toggle source

Performs delete_at for a range of integers

Assumes that the integers in range are contiguous, and sorted in ascending order.

# File lib/date_parser/utils.rb, line 40
def Utils.delete_at_indices(array, range)
  first_val = range.first
  for _ in range do
    array.delete_at(first_val)
  end
  
  return array
end
descended_from?(obj, ancestor, nil_accepted = true) click to toggle source

Checks to see if an object is descended from an ancestor (or is the ancestor) nil_accepted is a flag that checks

# File lib/date_parser/utils.rb, line 51
def Utils.descended_from?(obj, ancestor, nil_accepted = true)
  return obj.nil? ? nil_accepted : obj.class.ancestors.include?(ancestor)
end
is_int?(str) click to toggle source

Determine whether or not a String is a base 10 integer.

# File lib/date_parser/utils.rb, line 9
def Utils.is_int?(str)
  str.to_i.to_s == str || strong_is_int?(str)
end
strong_is_int?(str) click to toggle source

A more rigorous check to see if the String is an int.

# File lib/date_parser/utils.rb, line 14
def Utils.strong_is_int?(str)
  nums = ("0".."9").to_a
  
  for char in str.each_char do
    if ! nums.include? char
      return false
    end
  end
  
  return true
end