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 =
i[[] count each each_key each_pair].freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#convert_keys_to_strings(hsh) ⇒ Hash
(also: #stringify_keys)
Returns a copy of the hash with the keys converted to strings.
-
#convert_keys_to_symbols(hsh) ⇒ Hash
(also: #symbolize_keys)
Returns a copy of the hash with the keys converted to symbols.
-
#deep_dup(hsh) ⇒ Hash
Creates a deep copy of the object by returning a new Hash with deep copies of each key and value.
-
#deep_freeze(hsh) ⇒ Object
Freezes the hash and performs a deep freeze on each hash key and value.
- #generate_binding(hsh) ⇒ Object
-
#hash?(hsh) ⇒ Boolean
Returns true if the object is or appears to be a Hash.
-
#immutable?(hsh) ⇒ Boolean
Returns true if the hash is immutable, i.e.
-
#mutable?(hsh) ⇒ Boolean
Returns true if the hash is mutable.
Methods inherited from Base
Class Method Details
.stringify_keys ⇒ Object
21 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 21 alias stringify_keys convert_keys_to_strings |
.symbolize_keys ⇒ Object
22 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 22 alias symbolize_keys convert_keys_to_symbols |
Instance Method Details
#convert_keys_to_strings(hsh) ⇒ Hash Also known as: stringify_keys
Returns a copy of the hash with the keys converted to strings.
30 31 32 33 34 35 36 37 38 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 30 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 copy of the hash with the keys converted to symbols.
46 47 48 49 50 51 52 53 54 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 46 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 object by returning a new Hash with deep copies of each key and value.
63 64 65 66 67 68 69 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 63 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) ⇒ Object
Freezes the hash and performs a deep freeze on each hash key and value.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 75 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) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 86 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?(hsh) ⇒ Boolean
Returns true if the object is or appears to be a Hash.
101 102 103 104 105 106 107 108 109 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 101 def hash?(hsh) return true if hsh.is_a?(Hash) HASH_METHODS.each do |method_name| return false unless hsh.respond_to?(method_name) end true end |
#immutable?(hsh) ⇒ Boolean
Returns true if the hash is immutable, i.e. if the hash is frozen and each hash key and hash value are immutable.
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 117 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 is mutable.
138 139 140 |
# File 'lib/sleeping_king_studios/tools/hash_tools.rb', line 138 def mutable?(hsh) !immutable?(hsh) end |