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
-
.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.
- .uri_from(string, host:, object_type:) ⇒ Object
Class Method Details
.bold_text(string) ⇒ String
Output a string using bold escape sequences to the output TTY text.
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.
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.
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”.
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"
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.
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) = {} [: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.
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.
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 |