module Surrealist::StringUtils

A helper class for strings transformations.

Constants

DASH
DASH_REGEXP1
DASH_REGEXP2
EMPTY_STRING
NAMESPACES_SEPARATOR
UNDERSCORE
UNDERSCORE_REGEXP
UNDERSCORE_SUBSTITUTE

Public Class Methods

break_namespaces(klass, camelize, nesting_level) click to toggle source

Extracts n amount of classes from a namespaces and returns a nested hash.

@param [String] klass full namespace as a string. @param [Boolean] camelize optional camelize argument. @param [Integer] nesting_level level of required nesting.

@example 3 levels

klass = 'Business::System::Cashier::Reports::Withdraws'
break_namespaces(klass, camelize: false, nesting_level: 3)
# => { cashier: { reports: { withdraws: {} } } }

@raise Surrealist::InvalidNestingLevel if nesting level is specified as 0.

@return [Hash] a nested hash.

# File lib/surrealist/string_utils.rb, line 72
def break_namespaces(klass, camelize, nesting_level)
  Surrealist::ExceptionRaiser.raise_invalid_nesting!(nesting_level) unless nesting_level.positive?

  klass.split(NAMESPACES_SEPARATOR).last(nesting_level).reverse!.inject({}) do |a, n|
    key = (camelize ? camelize(uncapitalize(n), false) : underscore(n)).to_sym

    { key => a }
  end
end
camelize(snake_string, first_upper = true) click to toggle source

Camelizes a string.

@param [String] snake_string a string to be camelized. @param [Boolean] first_upper should the first letter be capitalized.

@return [String] camelized string.

# File lib/surrealist/string_utils.rb, line 36
def camelize(snake_string, first_upper = true)
  if first_upper
    snake_string.to_s.gsub(UNDERSCORE_REGEXP) { Regexp.last_match[1].capitalize }
  else
    parts = snake_string.split(UNDERSCORE, 2)
    parts[0].concat(camelize(parts[1])) if parts.size > 1
    parts[0] || EMPTY_STRING
  end
end
extract_class(string) click to toggle source

Extracts bottom-level class from a namespace.

@param [String] string full namespace

@example Extract class

extract_class('Animal::Dog::Collie') # => 'Collie'

@return [String] extracted class

# File lib/surrealist/string_utils.rb, line 54
def extract_class(string)
  uncapitalize(string.split(NAMESPACES_SEPARATOR).last)
end
underscore(string) click to toggle source

Converts a string to snake_case.

@param [String] string a string to be underscored.

@return [String] new underscored string.

# File lib/surrealist/string_utils.rb, line 21
def underscore(string)
  dup = string.gsub(NAMESPACES_SEPARATOR, UNDERSCORE)
  dup.gsub!(DASH_REGEXP1, UNDERSCORE_SUBSTITUTE)
  dup.gsub!(DASH_REGEXP2, UNDERSCORE_SUBSTITUTE)
  dup.tr!(DASH, UNDERSCORE)
  dup.downcase!
  dup
end

Private Class Methods

uncapitalize(string) click to toggle source

Clones a string and converts first character to lower case.

@param [String] string a string to be cloned.

@return [String] new string with lower cased first character.

# File lib/surrealist/string_utils.rb, line 89
def uncapitalize(string)
  str = string.dup
  str[0] = str[0].downcase
  str
end