Class: Avataaars::Utils
- Inherits:
-
Object
- Object
- Avataaars::Utils
- Defined in:
- lib/avataaars/utils.rb
Overview
Utility class for Avataaars helper methods
Class Method Summary collapse
-
.deep_transform_keys_in_object(object, &block) ⇒ Object
Deep transform the keys in an object (Hash/Array).
-
.normalize_object(object) ⇒ Object
Recursively normalizes hash objects with camelized string keys.
-
.squish(string) ⇒ Object
Removes leading/trailing whitespaces and squishes inner whitespace with a single space.
Class Method Details
.deep_transform_keys_in_object(object, &block) ⇒ Object
Deep transform the keys in an object (Hash/Array)
Copied from active support
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/avataaars/utils.rb', line 26 def self.deep_transform_keys_in_object(object, &block) case object when Hash object.each_with_object({}) do |(key, value), result| result[yield(key)] = deep_transform_keys_in_object(value, &block) end when Array object.map { |e| deep_transform_keys_in_object(e, &block) } else object end end |
.normalize_object(object) ⇒ Object
Recursively normalizes hash objects with camelized string keys
42 43 44 |
# File 'lib/avataaars/utils.rb', line 42 def self.normalize_object(object) deep_transform_keys_in_object(object) { |k| normalize_key(k) } end |
.squish(string) ⇒ Object
Removes leading/trailing whitespaces and squishes inner whitespace with a single space
N.B. whitespace includes all ‘blank’ characters as well as newlines/carriage returns etc.
13 14 15 16 17 18 |
# File 'lib/avataaars/utils.rb', line 13 def self.squish(string) string .gsub(/\A[[:space:]]+/, '') .gsub(/[[:space:]]+\z/, '') .gsub(/[[:space:]]+/, ' ') end |