Class: App::UtilsStrings

Inherits:
Object
  • Object
show all
Defined in:
lib/core/utils_strings.rb

Class Method Summary collapse

Class Method Details

.remove_surrounding_slashes(string) ⇒ Object

Remove preceding/trailing slashes from a string (and trim preceding/trailing whitespace).

Parameters:

  • String
    • The string to be trimmed (and returned).

Returns:

  • String



26
27
28
29
30
31
32
# File 'lib/core/utils_strings.rb', line 26

def self.remove_surrounding_slashes(string)
    string.strip!
    validate_string(string)
    string.gsub!(/\A\/+/, '')
    string.gsub!(/\/+\z/, '')
    string
end

.snake_case_to_camel_case(string) ⇒ Object

Convert ‘snake_case’ or ‘SnAKE_cAse’ to ‘SnakeCase’.

Parameters:

  • String

Returns:

  • String



8
9
10
11
12
13
# File 'lib/core/utils_strings.rb', line 8

def self.snake_case_to_camel_case(string)
    validate_string(string)
    string.downcase!
    return string if string !~ /_/ && string =~ /[A-Z]+.*/
    string.split('_').map { |e| e.capitalize }.join
end

.snake_case_to_camel_case_lower(string) ⇒ Object

Convert ‘snake_case’ or ‘SnAKE_cAse’ to ‘snakeCase’.

Parameters:

  • String

Returns:

  • String



18
19
20
21
# File 'lib/core/utils_strings.rb', line 18

def self.snake_case_to_camel_case_lower(string)
    string = snake_case_to_camel_case(string)
    "#{string[0, 1].downcase}#{string[1..-1]}"
end

.validate_string(string) ⇒ Object



36
37
38
39
40
# File 'lib/core/utils_strings.rb', line 36

def self.validate_string(string)
    if string.nil? || !string.is_a?(String)
        raise RuntimeError, "Expected String, got: #{string.class}"
    end
end