Module: Macmillan::Utils::Helper::StringConversionHelper

Included in:
HashKeyHelper
Defined in:
lib/macmillan/utils/helper/string_conversion_helper.rb

Class Method Summary collapse

Class Method Details

.camelcase_to_snakecase_symbol(string) ⇒ Object



29
30
31
# File 'lib/macmillan/utils/helper/string_conversion_helper.rb', line 29

def camelcase_to_snakecase_symbol(string)
  snakecase_string(string).to_sym
end

.snakecase_string(string) ⇒ Object

shamelessly ripped out of the ‘facets’ gem



8
9
10
11
12
13
14
15
16
# File 'lib/macmillan/utils/helper/string_conversion_helper.rb', line 8

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

.upper_camelcase_string(string) ⇒ Object

also shamelessly ripped out of the ‘facets’ gem



19
20
21
22
23
24
25
26
27
# File 'lib/macmillan/utils/helper/string_conversion_helper.rb', line 19

def upper_camelcase_string(string)
  separators = ['_', '\s']

  separators.each do |s|
    string = string.gsub(/(?:#{s}+)([a-z])/) { Regexp.last_match(1).upcase }
  end

  string.gsub(/(\A|\s)([a-z])/) { Regexp.last_match(1) + Regexp.last_match(2).upcase }
end