Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-lokalise-api/utils/string_utils.rb

Overview

Initial code taken from Facets gem by Rubyworks github.com/rubyworks/facets/blob/master/lib/core/facets/string/snakecase.rb

Instance Method Summary collapse

Instance Method Details

#base_class_nameObject

Turn ‘Module::Nested::ClassName` to just `ClassName`



20
21
22
# File 'lib/ruby-lokalise-api/utils/string_utils.rb', line 20

def base_class_name
  split('::').last
end

#remove_trailing_slashObject



24
25
26
# File 'lib/ruby-lokalise-api/utils/string_utils.rb', line 24

def remove_trailing_slash
  gsub %r{/\z}, ''
end

#snakecaseObject

Underscore a string such that camelcase, dashes and spaces are replaced by underscores.



9
10
11
12
13
14
15
16
17
# File 'lib/ruby-lokalise-api/utils/string_utils.rb', line 9

def snakecase
  base_class_name.
    gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
    gsub(/([a-z\d])([A-Z])/, '\1_\2').
    tr('-', '_').
    gsub(/\s/, '_').
    gsub(/__+/, '_').
    downcase
end