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.
-
#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
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.
45 46 47 48 49 50 51 52 53 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 45 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.
76 77 78 79 80 81 82 83 84 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 76 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.
108 109 110 111 112 113 114 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 108 def deep_dup(hsh) require_hash!(hsh) hsh.each.with_object({}) do |(key, value), copy| copy[ObjectTools.deep_dup key] = ObjectTools.deep_dup(value) end end |
#deep_freeze(hsh) ⇒ Hash
Freezes the hash and performs a deep freeze on each hash key and value.
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 132 def deep_freeze(hsh) require_hash!(hsh) hsh.freeze hsh.each do |key, value| ObjectTools.deep_freeze key ObjectTools.deep_freeze value end end |
#generate_binding(hsh) ⇒ Binding
Generates a Binding with the hash values as local variables.
160 161 162 163 164 165 166 167 168 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 160 def generate_binding(hsh) require_hash!(hsh) CoreTools.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.
187 188 189 190 191 192 193 194 195 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 187 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.
221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 221 def immutable?(hsh) require_hash!(hsh) return false unless hsh.frozen? hsh.each do |key, value| unless ObjectTools.immutable?(key) && ObjectTools.immutable?(value) return false end end true end |
#mutable?(hsh) ⇒ Boolean
Returns true if the hash or any of its contents are mutable.
244 245 246 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 244 def mutable?(hsh) !immutable?(hsh) end |