module Arcanus::Utils

A miscellaneous set of utility functions.

Public Instance Methods

camel_case(string) click to toggle source

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

@param [String] string @return [String]

# File lib/arcanus/utils.rb, line 10
def camel_case(string)
  string.split(/_|-| /)
        .map { |part| part.sub(/^\w/, &:upcase) }
        .join
end
deep_dup(hash) click to toggle source

Returns a deep copy of the specified hash.

@param hash [Hash] @return [Hash]

# 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) click to toggle source

Convert string containing camel case or spaces into snake case.

@see stackoverflow.com/questions/1509915/converting-camel-case-to-underscore-case-in-ruby

@param [String] string @return [String]

# 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