Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/morpheus/ext/string.rb
Instance Method Summary collapse
- #camelcase ⇒ Object
- #dasherize ⇒ Object
- #pluralize ⇒ Object
- #singularize ⇒ Object
- #titleize ⇒ Object
- #underscore ⇒ Object
- #upcamelcase ⇒ Object
Instance Method Details
#camelcase ⇒ Object
48 49 50 51 52 |
# File 'lib/morpheus/ext/string.rb', line 48 def camelcase value = self.underscore.gsub(/\_([a-z])/) do $1.upcase end value = value[0, 1].downcase + value[1..-1] value end |
#dasherize ⇒ Object
62 63 64 |
# File 'lib/morpheus/ext/string.rb', line 62 def dasherize self.underscore.gsub("_", "-") end |
#pluralize ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/morpheus/ext/string.rb', line 3 def pluralize # hacky version of this until we want to depend on ActiveSupport value = self.dup if value == "" value elsif value[-1].chr == "y" value[0..-2] + "ies" elsif value[-1].chr == "s" if value[-2..-1] == "ss" value + "es" else value end else value + "s" end end |
#singularize ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/morpheus/ext/string.rb', line 21 def singularize # hacky version of this until we want to depend on ActiveSupport value = self.dup if value == "" value elsif value.size > 3 && value[-3..-1] == "ies" value[0..-4] + "y" elsif value.size > 2 && value[-3..-1] == "ses" value[0..-2] elsif value[-1] == "s" value[0..-2] else value end end |
#titleize ⇒ Object
58 59 60 |
# File 'lib/morpheus/ext/string.rb', line 58 def titleize self.underscore.split("_").map(&:capitalize).join(" ") end |
#underscore ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/morpheus/ext/string.rb', line 37 def underscore value = self.dup value.gsub!(/::/, '/') value.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2') value.gsub!(/([a-z\d])([A-Z])/,'\1_\2') value.tr!("-", "_") value.tr!(" ", "_") value.downcase! value end |
#upcamelcase ⇒ Object
54 55 56 |
# File 'lib/morpheus/ext/string.rb', line 54 def upcamelcase self.camelcase.capitalize end |