Class: SleepingKingStudios::Tools::HashTools
- Defined in:
- lib/sleeping_king_studios/tools/hash_tools.rb
Overview
Tools for working with hash-like enumerable objects.
Constant Summary collapse
- HASH_METHODS =
Expected methods that a Hash-like object should implement.
%i[[] count each each_key each_pair].freeze
Instance Method Summary collapse
-
#convert_keys_to_strings(hsh) ⇒ Hash
(also: #stringify_keys)
Returns a deep copy of the hash with the keys converted to strings.
-
#convert_keys_to_symbols(hsh) ⇒ Hash
(also: #symbolize_keys)
Returns a deep copy of the hash with the keys converted to symbols.
-
#deep_dup(hsh) ⇒ Hash
Creates a deep copy of the Hash.
-
#deep_freeze(hsh) ⇒ Hash
Freezes the hash and performs a deep freeze on each hash key and value.
- #fetch(hsh, key, default = UNDEFINED, indifferent_key: false, &block) ⇒ Object
-
#generate_binding(hsh) ⇒ Binding
Generates a Binding with the hash values as local variables.
-
#hash?(obj) ⇒ Boolean
Returns true if the object is or appears to be a Hash.
-
#immutable?(hsh) ⇒ Boolean
Returns true if the hash is immutable.
-
#mutable?(hsh) ⇒ Boolean
Returns true if the hash or any of its contents are mutable.
Methods inherited from Base
#initialize, instance, #toolbelt
Constructor Details
This class inherits a constructor from SleepingKingStudios::Tools::Base
Instance Method Details
#convert_keys_to_strings(hsh) ⇒ Hash Also known as: stringify_keys
Returns a deep copy of the hash with the keys converted to strings.
46 47 48 49 50 51 52 53 54 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 46 def convert_keys_to_strings(hsh) require_hash!(hsh) hsh.each.with_object({}) do |(key, value), cpy| sym = key.to_s cpy[sym] = convert_value_to_stringified_hash(value) end end |
#convert_keys_to_symbols(hsh) ⇒ Hash Also known as: symbolize_keys
Returns a deep copy of the hash with the keys converted to symbols.
77 78 79 80 81 82 83 84 85 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 77 def convert_keys_to_symbols(hsh) require_hash!(hsh) hsh.each.with_object({}) do |(key, value), cpy| sym = key.to_s.intern cpy[sym] = convert_value_to_symbolic_hash(value) end end |
#deep_dup(hsh) ⇒ Hash
Creates a deep copy of the Hash.
109 110 111 112 113 114 115 116 117 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 109 def deep_dup(hsh) require_hash!(hsh) object_tools = toolbelt.object_tools hsh.each.with_object({}) do |(key, value), copy| copy[object_tools.deep_dup key] = object_tools.deep_dup(value) end end |
#deep_freeze(hsh) ⇒ Hash
Freezes the hash and performs a deep freeze on each hash key and value.
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 135 def deep_freeze(hsh) require_hash!(hsh) hsh.freeze object_tools = toolbelt.object_tools hsh.each do |key, value| object_tools.deep_freeze key object_tools.deep_freeze value end end |
#fetch(hsh, key, default = nil, indifferent_key: false) ⇒ Object #fetch(hsh, key, indifferent_key: false) {|key| ... } ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 190 def fetch(hsh, key, default = UNDEFINED, indifferent_key: false, &block) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity require_hash!(hsh) if hsh.respond_to?(:fetch) return native_fetch(hsh, key, default, indifferent_key:, &block) end return hsh[key] if hsh.key?(key) if indifferent_key && (key.is_a?(String) || key.is_a?(Symbol)) indifferent_key = key.is_a?(String) ? key.to_sym : key.to_s return hsh[indifferent_key] if hsh.key?(indifferent_key) end return block.call(key) if block_given? return default unless default == UNDEFINED raise KeyError, "key not found: #{key.inspect}" end |
#generate_binding(hsh) ⇒ Binding
Generates a Binding with the hash values as local variables.
This method is primarily used when rendering ERB templates.
231 232 233 234 235 236 237 238 239 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 231 def generate_binding(hsh) require_hash!(hsh) toolbelt.core_tools.empty_binding.tap do |binding| hsh.each do |key, value| binding.local_variable_set key, value end end end |
#hash?(obj) ⇒ Boolean
Returns true if the object is or appears to be a Hash.
This method checks for the method signatures of the object. A Hash-like method will define all of the the #[], #count, #each, #each_key, and #each_value methods.
258 259 260 261 262 263 264 265 266 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 258 def hash?(obj) return true if obj.is_a?(Hash) HASH_METHODS.each do |method_name| return false unless obj.respond_to?(method_name) end true end |
#immutable?(hsh) ⇒ Boolean
Returns true if the hash is immutable.
A hash is considered immutable if the hash itself is frozen and each key and value in the hash is immutable.
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 292 def immutable?(hsh) require_hash!(hsh) return false unless hsh.frozen? object_tools = toolbelt.object_tools hsh.each do |key, value| unless object_tools.immutable?(key) && object_tools.immutable?(value) return false end end true end |
#mutable?(hsh) ⇒ Boolean
Returns true if the hash or any of its contents are mutable.
317 318 319 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 317 def mutable?(hsh) !immutable?(hsh) end |