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
-
.bold_text(string) ⇒ String
Output a string using bold escape sequences to the output TTY text.
-
.classify(string_constant) ⇒ Class, Module
Given a string path, classify it into a namespaced Ruby constant.
-
.date_as_iso8601(datetime) ⇒ String
Takes a date and renders it in ISO 8601 format.
-
.file_extension(path) ⇒ String
Given a path, figure out what the extension is.
-
.handleize(string) ⇒ String
Given any string, normalize it into a “kabab-case”, single-word string.
-
.parallelized(collection) {|Object| ... } ⇒ Array
Parallelize and fan out a collection of work in child processes.
-
.stringify_keys(hash) ⇒ Hash
Given a hash, take all of its keys (and sub-keys) and convert them into strings.
-
.symbolize_keys(hash) ⇒ Hash
Given a hash, take all of its keys (and sub-keys) and convert them into symbols.
Class Method Details
.bold_text(string) ⇒ String
Output a string using bold escape sequences to the output TTY text.
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.
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.
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”.
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"
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.
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) = {} [:in_threads] = 0 if Lifer.parallelism_disabled? results = Parallel.map(collection, **) 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.
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.
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 |