Class: Ree::StringUtils

Inherits:
Object show all
Defined in:
lib/ree/utils/string_utils.rb

Class Method Summary collapse

Class Method Details

.camelize(string, uppercase_first_letter = true) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/ree/utils/string_utils.rb', line 19

def camelize(string, uppercase_first_letter = true)
  if uppercase_first_letter
    string = string.sub(/^[a-z\d]*/) { |match| match.capitalize }
  else
    string = string.sub(/^(?:(?=\b|[A-Z_])|\w)/) { |match| match.downcase }
  end

  string.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }.gsub("/", "::")
end

.truncate(str, limit = 80) ⇒ Object



5
6
7
# File 'lib/ree/utils/string_utils.rb', line 5

def truncate(str, limit = 80)
  str.length > limit ? "#{str[0..limit]}..." : str
end

.underscore(camel_cased_word) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/ree/utils/string_utils.rb', line 9

def underscore(camel_cased_word)
  return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word)
  word = camel_cased_word.to_s.gsub("::".freeze, "/".freeze)
  word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2'.freeze)
  word.gsub!(/([a-z\d])([A-Z])/, '\1_\2'.freeze)
  word.tr!("-".freeze, "_".freeze)
  word.downcase!
  word
end