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. :-)

Defined Under Namespace

Classes: AmbiguousURIError

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.



14
15
16
# File 'lib/lifer/utilities.rb', line 14

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)


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

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.



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

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”).



59
60
61
# File 'lib/lifer/utilities.rb', line 59

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.



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

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.



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

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.



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

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.



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

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

.uri_from(string, host:, object_type:) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/lifer/utilities.rb', line 134

def uri_from(string, host:, object_type:)
  uri = string && URI.parse(string.strip)

  if uri && uri.relative? && uri.to_s.start_with?("/")
    "%s%s" % [host, uri]
  elsif uri && uri.relative? && !uri.to_s.start_with?("/")
    raise AmbiguousURIError
  elsif uri&.absolute?
    uri.to_s
  end
rescue AmbiguousURIError
  Lifer::Message.error "utilities.ambiguous_uri_error",
    object_type: object_type.inspect,
    uri: uri&.to_s
  nil
end