Module: CoreExtensions::String::Transformations

Defined in:
lib/core_extensions/string/transformations.rb

Overview

Monkey-patches for String to add some simple missing transformations

Instance Method Summary collapse

Instance Method Details

#humanizeString

Attempt to guess a more human-like view of a string

Returns:



23
24
25
# File 'lib/core_extensions/string/transformations.rb', line 23

def humanize
  gsub(/_id$/, '').tr('_', ' ').capitalize
end

#to_camelString

Convert underscored_text to CamelCase

Returns:



17
18
19
# File 'lib/core_extensions/string/transformations.rb', line 17

def to_camel
  split('_').map(&:capitalize).join
end

#to_underscoreString

Convert CamelCase to underscored_text

Returns:



7
8
9
10
11
12
13
# File 'lib/core_extensions/string/transformations.rb', line 7

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