Class: Google::StringUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/google/string_utils.rb

Overview

Helper class to process and mutate strings.

Class Method Summary collapse

Class Method Details

.camelize(source, style = :lower) ⇒ Object

Converts string from underscore to camel case



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/google/string_utils.rb', line 18

def self.camelize(source, style = :lower)
  camelized = source.gsub(/_(.)/, &:upcase).delete('_')
  case style
  when :lower
    camelized[0] = camelized[0].downcase
  when :upper
    camelized[0] = camelized[0].upcase
  else
    raise "Unknown camel case style: #{style}"
  end
  camelized
end

.symbolize(key) ⇒ Object

rubocop:disable Style/SafeNavigation # support Ruby < 2.3.0



47
48
49
# File 'lib/google/string_utils.rb', line 47

def self.symbolize(key)
  key.to_sym unless key.nil?
end

.uncombine(source) ⇒ Object

Add spaces before every capitalized word except first.



42
43
44
# File 'lib/google/string_utils.rb', line 42

def self.uncombine(source)
  source.gsub(/(?=[A-Z])/, ' ').strip
end

.underscore(source) ⇒ Object

Converts string from camel case to underscore



32
33
34
35
36
37
38
39
# File 'lib/google/string_utils.rb', line 32

def self.underscore(source)
  source.gsub(/::/, '/')
        .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
        .gsub(/([a-z\d])([A-Z])/, '\1_\2')
        .tr('-', '_')
        .tr('.', '_')
        .downcase
end