Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/motion_model/ext.rb
Instance Method Summary collapse
- #camelize(uppercase_first_letter = true) ⇒ Object
- #empty? ⇒ Boolean
- #humanize ⇒ Object
- #pluralize ⇒ Object
- #singularize ⇒ Object
- #titleize ⇒ Object
- #underscore ⇒ Object
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
10 11 12 |
# File 'lib/motion_model/ext.rb', line 10 def empty? self.length < 1 end |
#humanize ⇒ Object
2 3 4 |
# File 'lib/motion_model/ext.rb', line 2 def humanize self.split(/_|-| /).join(' ') end |
#pluralize ⇒ Object
14 15 16 |
# File 'lib/motion_model/ext.rb', line 14 def pluralize Inflector.inflections.pluralize self end |
#singularize ⇒ Object
18 19 20 |
# File 'lib/motion_model/ext.rb', line 18 def singularize Inflector.inflections.singularize self end |
#titleize ⇒ Object
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 |
#underscore ⇒ Object
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 |