Module: Arcanus::Utils

Included in:
Command::Base
Defined in:
lib/arcanus/utils.rb

Overview

A miscellaneous set of utility functions.

Class Method Summary collapse

Class Method Details

.camel_case(string) ⇒ String

Converts a string containing underscores/hyphens/spaces into CamelCase.

Parameters:

  • string (String)

Returns:

  • (String)

10
11
12
13
14
# File 'lib/arcanus/utils.rb', line 10

def camel_case(string)
  string.split(/_|-| /)
        .map { |part| part.sub(/^\w/, &:upcase) }
        .join
end

.deep_dup(hash) ⇒ Hash

Returns a deep copy of the specified hash.

Parameters:

  • hash (Hash)

Returns:

  • (Hash)

34
35
36
37
38
# File 'lib/arcanus/utils.rb', line 34

def deep_dup(hash)
  hash.each_with_object({}) do |(key, value), dup|
    dup[key] = value.is_a?(Hash) ? deep_dup(value) : value
  end
end

.snake_case(string) ⇒ String

Convert string containing camel case or spaces into snake case.

Parameters:

  • string (String)

Returns:

  • (String)

See Also:

  • Arcanus::Utils.stackoverflowstackoverflow.com/questions/1509915/converting-camel-case-to-underscore-case-in-ruby

22
23
24
25
26
27
28
# File 'lib/arcanus/utils.rb', line 22

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