module StringHelper
  extend self

  # from Rails' ActiveSupport
  def underscore(string)
    string.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
  end

  def underscore_class(klass, without_ancestors=true)
    class_name = if without_ancestors
      klass.to_s.split('::').last
    else
      klass.to_s
    end
    StringHelper.underscore(class_name)
  end

  # from Rails' ActiveSupport
  def camelize(term)
    string = term.to_s
    string = string.sub(/^[a-z\d]*/) { $&.capitalize }
    string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }
    string.gsub!(/\//, '::')
    string
  end

  # rough simplification
  def pluralize(string)
    "#{string}s"
  end
end