Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/utils/string_patches.rb
Overview
This is primarily to ensure that consistent functionality is maintained and migration names and camelize works the same way it does in Rails. These were taken directly from the Rails repo.
Instance Method Summary collapse
Instance Method Details
#camelize(uppercase_first_letter = true) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/utils/string_patches.rb', line 14 def camelize(uppercase_first_letter = true) string = self if uppercase_first_letter string = string.sub(/^[a-z\d]*/) { |match| match.capitalize } else string = string.sub(/^(?:(?=\b|[A-Z_])|\w)/) { |match| match.downcase } end string.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }.gsub("/", "::") end |
#underscore ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/utils/string_patches.rb', line 6 def underscore self.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end |