Class: Virtuous::StringHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/virtuous/helpers/string_helper.rb

Overview

Helper methods for strings

Class Method Summary collapse

Class Method Details

.camelize(string) ⇒ String

Transform a String from snake_case to CamelCase.

Parameters:

  • string (String)

    the string to transform.

Returns:

  • (String)

    the CamelCase string.



22
23
24
25
26
27
28
# File 'lib/virtuous/helpers/string_helper.rb', line 22

def camelize(string)
  string = string.sub(/^(?:(?=\b|[A-Z_])|\w)/, &:downcase)

  string.gsub(%r{(?:_|(/))([a-z\d]*)}) do
    "#{::Regexp.last_match(1)}#{::Regexp.last_match(2).capitalize}"
  end.gsub('/', '::')
end

.underscore(string) ⇒ String

Transform a String from CamelCase to snake_case.

Parameters:

  • string (String)

    the string to transform.

Returns:

  • (String)

    the snake_case string.



10
11
12
13
14
15
16
# File 'lib/virtuous/helpers/string_helper.rb', line 10

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