Module: Lifer::Utilities

Defined in:
lib/lifer/utilities.rb

Overview

A module namespace for any weird utilities that are used pseudo-globally. Ensure that these are actually useful globally, though. :-)

Class Method Summary collapse

Class Method Details

.bold_text(string) ⇒ String

Output a string using bold escape sequences to the output TTY text.

Parameters:

  • string (String)

    The string to display in bold.

Returns:

  • (String)

    The string, but in bold.



12
13
14
# File 'lib/lifer/utilities.rb', line 12

def bold_text(string)
  "\e[1m#{string}\e[0m"
end

.classify(string_constant) ⇒ Class, Module

Given a string path, classify it into a namespaced Ruby constant. If the constant does not exist, we raise a helpful error.

Examples:

Result

classify("my/class_name/that_exists") #=> My::ClassName::ThatExists

Parameters:

  • string_constant (String)

    A string that maps to a Ruby constant.

Returns:

  • (Class, Module)

Raises:

  • (RuntimeError)


30
31
32
33
34
35
36
37
38
# File 'lib/lifer/utilities.rb', line 30

def classify(string_constant)
  Object.const_get camelize(string_constant)
rescue NameError => exception
  raise I18n.t(
    "utilities.classify_error",
    string_constant:,
    camel_cased_string_constant: camelize(string_constant)
  )
end

.date_as_iso8601(datetime) ⇒ String

Takes a date and renders it in ISO 8601 format.

Parameters:

  • datetime (Date, Time, DateTime, String)

    A representation of a date.

Returns:

  • (String)

    An ISO 8601 representation of that date.



44
45
46
47
48
49
50
# File 'lib/lifer/utilities.rb', line 44

def date_as_iso8601(datetime)
  return unless (data = DateTime.parse(datetime.to_s))

  data.strftime("%Y-%m-%dT%H:%M:%S%:z")
rescue Date::Error
  nil
end

.file_extension(path) ⇒ String

Given a path, figure out what the extension is. It supports multi-extensions like “.html.erb”.

Parameters:

  • path (Pathname, String)

    The path to a file.

Returns:

  • (String)

    The extension (i.e. “.html.erb”).



57
58
59
# File 'lib/lifer/utilities.rb', line 57

def file_extension(path)
  File.basename(path.to_s.downcase).match(/(?<=.)\..*/).to_s
end

.handleize(string) ⇒ String

Given any string, normalize it into a “kabab-case”, single-word string.

Input:  "Hi, how are you?"
Output: "hi-how-are-you"

Parameters:

  • string (String)

    Any string.

Returns:

  • (String)

    The kabab-cased output.



68
# File 'lib/lifer/utilities.rb', line 68

def handleize(string) = parameterize(string, separator: "-")

.parallelized(collection) {|Object| ... } ⇒ Array

Parallelize and fan out a collection of work in child processes. If any of the child processes results in an error, we raise it and halt the program.

Parameters:

  • collection (Array)

    A collection to operate on.

Yields:

  • (Object)

    A function to transform each collection item (in parallel).

Returns:

  • (Array)

    The mapped results of the operation.

Raises:

  • (Exception)

    Any exception thrown by a child process.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/lifer/utilities.rb', line 78

def parallelized(collection, &block)
  options = {}
  options[:in_threads] = 0 if Lifer.parallelism_disabled?

  results = Parallel.map(collection, **options) do |collection_item|
    begin
      yield collection_item
    rescue => error
      error
    end
  end

  first_error = results.detect { _1.is_a? Exception }
  raise first_error if first_error

  results
end

.stringify_keys(hash) ⇒ Hash

Given a hash, take all of its keys (and sub-keys) and convert them into strings.

Parameters:

  • hash (Hash)

    Any hash.

Returns:

  • (Hash)

    The hash with keys transformed to strings.



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/lifer/utilities.rb', line 101

def stringify_keys(hash)
  stringified_hash = {}

  hash.each do |key, value|
    stringified_hash[(key.to_s rescue key) || key] =
      value.is_a?(Hash) ? stringify_keys(value) : value

    stringify_keys(value) if value.is_a?(Hash)
  end

  stringified_hash
end

.symbolize_keys(hash) ⇒ Hash

Given a hash, take all of its keys (and sub-keys) and convert them into symbols.

Parameters:

  • hash (Hash)

    Any hash.

Returns:

  • (Hash)

    The hash with keys transformed to symbols.



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/lifer/utilities.rb', line 119

def symbolize_keys(hash)
  symbolized_hash = {}

  hash.each do |key, value|
    symbolized_hash[(key.to_sym rescue key) || key] =
      value.is_a?(Hash) ? symbolize_keys(value) : value

    symbolize_keys(value) if value.is_a?(Hash)
  end

  symbolized_hash
end