Class: Rnfse::String

Inherits:
String
  • Object
show all
Defined in:
lib/rnfse/string.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.camelize(term, uppercase_first_letter = true) ⇒ Object



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

def self.camelize(term, uppercase_first_letter = true)
  string = term.to_s
  if uppercase_first_letter
    string = string.sub(/^[a-z\d]*/) { $&.capitalize }
  else
    string = string.sub(/^(?:(?=\b|[A-Z_])|\w)/) { $&.downcase }
  end
  string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { $2.capitalize }
  string.gsub!('/', '::')
  self.new(string)
end

.demodulize(path) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/rnfse/string.rb', line 36

def self.demodulize(path)
  path = path.to_s
  self.new( if i = path.rindex('::')
              path[(i+2)..-1]
            else
              path
            end )
end

.underscore(term) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/rnfse/string.rb', line 21

def self.underscore(term)
  word = term.to_s.gsub('::', '/')
  word.gsub!(/(?:([A-Za-z\d])|^)(?=\b|[^a-z])/)
  #{ "#{$1}#{$1 && '_'}#{$2.downcase}" }
  word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  word.tr!("-", "_")
  word.downcase!
  self.new(word)
end

Instance Method Details

#camelizeObject



17
18
19
# File 'lib/rnfse/string.rb', line 17

def camelize
  self.class.camelize(self) 
end

#demodulizeObject



45
46
47
# File 'lib/rnfse/string.rb', line 45

def demodulize
  self.class.demodulize(self)
end

#underscoreObject



32
33
34
# File 'lib/rnfse/string.rb', line 32

def underscore
  self.class.underscore(self)
end