Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/ams_layout/core_ext/string.rb

Overview

Re-open String class and add snakecase method.

Instance Method Summary collapse

Instance Method Details

#ams_layout_snakecaseObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ams_layout/core_ext/string.rb', line 13

def ams_layout_snakecase
  # Strip everything but alphanumerics, :, _, - and space
  # Replace :: with /
  # Separate CamelCased text with _
  # Remove :
  # Replace space and - with _
  # Replace multiple _ with one _
  self.gsub(/[^a-zA-Z0-9:_\s-]/, '').
  gsub(/::/, '/').
  gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
  gsub(/([a-z\d])([A-Z])/,'\1_\2').
  gsub(/:/, '').
  gsub(/[\s-]/, '_').
  gsub(/(_)+/,'_').
  downcase
end