Class: String

Inherits:
Object show all
Defined in:
lib/gluez/string.rb

Overview

Gluez does a lot of text processing and maniputation. The string class is extended to dry up code.

Instance Method Summary collapse

Instance Method Details

#camelize(first_letter_in_uppercase = true) ⇒ Object

Return the string as camelcased version e.g. some_text_value becomes SomeTextValue.



34
35
36
37
38
39
40
41
# File 'lib/gluez/string.rb', line 34

def camelize(first_letter_in_uppercase=true)
  word = self.dup
  if first_letter_in_uppercase
    word.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
  else
    word.to_s[0].chr.downcase + word.camelize[1..-1]
  end
end

#multiline_stripObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/gluez/string.rb', line 15

def multiline_strip
  lines = self.split("\n")
  first = lines[0]
  
  idx = 0
  while first[idx] == ' '
    idx += 1
  end
  
  if idx > 0
    lines = lines.map do |line|
      line[idx, line.length]
    end
  end
  
  lines.join("\n")
end

#underscoreObject

Return the string as underscored version e.g. SomeTextValue becomes some_text_value.



5
6
7
8
9
10
11
12
13
# File 'lib/gluez/string.rb', line 5

def underscore
  word = self.dup
  word.gsub!(/::/, '/')
  word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  word.tr!("-", "_")
  word.downcase!
  word
end