class KnowItAll::StringHelper

Constants

CLASSIFY_SEPARATOR
CLASSIFY_WORD_SEPARATOR
DASHERIZE_SEPARATOR
EMPTY_STRING
NAMESPACE_SEPARATOR
UNDERSCORE_DIVISION_TARGET
UNDERSCORE_SEPARATOR

Public Class Methods

classify(string) click to toggle source
# File lib/know_it_all/string_helper.rb, line 15
def self.classify(string)
  string = string.to_s

  words = underscore(string).split(CLASSIFY_WORD_SEPARATOR).map!(&:capitalize)
  delimiters = string.scan(CLASSIFY_WORD_SEPARATOR)

  delimiters.map! do |delimiter|
    delimiter == CLASSIFY_SEPARATOR ? EMPTY_STRING : NAMESPACE_SEPARATOR
  end

  words.zip(delimiters).join
end
underscore(string) click to toggle source
# File lib/know_it_all/string_helper.rb, line 28
def self.underscore(string)
  new_string = string.gsub(NAMESPACE_SEPARATOR, UNDERSCORE_SEPARATOR)
  new_string.gsub!(/([A-Z\d]+)([A-Z][a-z])/, UNDERSCORE_DIVISION_TARGET)
  new_string.gsub!(/([a-z\d])([A-Z])/, UNDERSCORE_DIVISION_TARGET)
  new_string.gsub!(/[[:space:]]|\-/, UNDERSCORE_DIVISION_TARGET)
  new_string.downcase
end