Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/motion_model/ext.rb

Instance Method Summary collapse

Instance Method Details

#camelize(uppercase_first_letter = true) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/motion_model/ext.rb', line 22

def camelize(uppercase_first_letter = true)
  string = self.dup
  string.gsub!(/(?:_|(\/))([a-z\d]*)/i) do
    new_word = $2.downcase
    new_word[0] = new_word[0].upcase
    new_word = "/#{new_word}" if $1 == '/'
    new_word
  end
  if uppercase_first_letter && uppercase_first_letter != :lower
    string[0] = string[0].upcase
  else
    string[0] = string[0].downcase
  end
  string.gsub!('/', '::')
  string
end

#empty?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/motion_model/ext.rb', line 10

def empty?
  self.length < 1
end

#humanizeObject



2
3
4
# File 'lib/motion_model/ext.rb', line 2

def humanize
  self.split(/_|-| /).join(' ')
end

#pluralizeObject



14
15
16
# File 'lib/motion_model/ext.rb', line 14

def pluralize
  Inflector.inflections.pluralize self
end

#singularizeObject



18
19
20
# File 'lib/motion_model/ext.rb', line 18

def singularize
  Inflector.inflections.singularize self
end

#titleizeObject



6
7
8
# File 'lib/motion_model/ext.rb', line 6

def titleize
  self.split(/_|-| /).each{|word| word[0...1] = word[0...1].upcase}.join(' ')
end

#underscoreObject



39
40
41
42
43
44
45
46
47
# File 'lib/motion_model/ext.rb', line 39

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