Class: Hash
- Defined in:
- lib/croesus/core_ext/hash.rb,
lib/croesus/core_ext/blank.rb
Overview
Add #blank? method to Hash class.
Defined Under Namespace
Classes: UndefinedPathError
Instance Method Summary collapse
-
#capitalize_keys ⇒ Hash
Returns a new hash with all keys converted to strings and the first letter capitalized.
-
#compact ⇒ Hash
Returns a compacted copy (contains no key/value pairs having nil? values).
-
#normalize_keys ⇒ Hash
Returns a new hash with all keys downcased and converted to symbols.
-
#recursive_fetch(*args, &block) ⇒ Hash, ...
Recursively searchs a nested datastructure for a key and returns the value.
- #recursive_merge(other) ⇒ Object
-
#recursively_capitalize_key ⇒ Hash
Returns a new Hash, recursively converting all keys to strings and the first letter capitalized.
-
#recursively_normalize_keys ⇒ Hash
Returns a new Hash, recursively downcasing and converting all keys to symbols.
-
#recursively_stringify_key ⇒ Hash
Returns a new Hash, recursively converting all keys to strings.
-
#recursively_symbolize_keys ⇒ Hash
Returns a new Hash, recursively converting all keys to symbols.
-
#recursively_transform_keys(&block) ⇒ Hash
Returns a new hash, recursively converting all keys by the block operation.
-
#stringify_keys ⇒ Hash
Returns a new hash with all keys converted to strings.
-
#symbolize_keys ⇒ Hash
Returns a new hash with all keys converted to symbols.
-
#transform_keys ⇒ Hash
Returns a new hash with all keys converted using the block operation.
Instance Method Details
#capitalize_keys ⇒ Hash
Returns a new hash with all keys converted to strings and the first letter capitalized.
118 119 120 |
# File 'lib/croesus/core_ext/hash.rb', line 118 def capitalize_keys transform_keys { |key| key.Upcase.to_s.capitalize rescue key } end |
#compact ⇒ Hash
Returns a compacted copy (contains no key/value pairs having nil? values)
31 32 33 |
# File 'lib/croesus/core_ext/hash.rb', line 31 def compact select { |_, value| !value.nil? } end |
#normalize_keys ⇒ Hash
Returns a new hash with all keys downcased and converted to symbols.
68 69 70 |
# File 'lib/croesus/core_ext/hash.rb', line 68 def normalize_keys transform_keys { |key| key.downcase.to_sym rescue key } end |
#recursive_fetch(*args, &block) ⇒ Hash, ...
Recursively searchs a nested datastructure for a key and returns the value. If a block is provided its value will be returned if the key does not exist
145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/croesus/core_ext/hash.rb', line 145 def recursive_fetch(*args, &block) args.reduce(self) do |obj, arg| begin arg = Integer(arg) if obj.is_a? Array obj.fetch(arg) rescue ArgumentError, IndexError, NoMethodError => e break block.call(arg) if block raise UndefinedPathError, "Could not fetch path (#{args.join(' > ')}) at #{arg}", e.backtrace end end end |
#recursive_merge(other) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/croesus/core_ext/hash.rb', line 157 def recursive_merge(other) hash = self.dup other.each do |key, value| myval = self[key] if value.is_a?(Hash) && myval.is_a?(Hash) hash[key] = myval.recursive_merge(value) else hash[key] = value end end hash end |
#recursively_capitalize_key ⇒ Hash
Returns a new Hash, recursively converting all keys to strings and the first letter capitalized.
127 128 129 |
# File 'lib/croesus/core_ext/hash.rb', line 127 def recursively_capitalize_key recursively_transform_keys { |key| key.to_s.capitalize rescue key } end |
#recursively_normalize_keys ⇒ Hash
Returns a new Hash, recursively downcasing and converting all keys to symbols.
77 78 79 |
# File 'lib/croesus/core_ext/hash.rb', line 77 def recursively_normalize_keys recursively_transform_keys { |key| key.downcase.to_sym rescue key } end |
#recursively_stringify_key ⇒ Hash
Returns a new Hash, recursively converting all keys to strings.
109 110 111 |
# File 'lib/croesus/core_ext/hash.rb', line 109 def recursively_stringify_key recursively_transform_keys { |key| key.to_s rescue key } end |
#recursively_symbolize_keys ⇒ Hash
Returns a new Hash, recursively converting all keys to symbols.
93 94 95 |
# File 'lib/croesus/core_ext/hash.rb', line 93 def recursively_symbolize_keys recursively_transform_keys { |key| key.to_sym rescue key } end |
#recursively_transform_keys(&block) ⇒ Hash
Returns a new hash, recursively converting all keys by the block operation.
59 60 61 |
# File 'lib/croesus/core_ext/hash.rb', line 59 def recursively_transform_keys(&block) _recursively_transform_keys_in_object(self, &block) end |
#stringify_keys ⇒ Hash
Returns a new hash with all keys converted to strings.
101 102 103 |
# File 'lib/croesus/core_ext/hash.rb', line 101 def stringify_keys transform_keys { |key| key.to_s rescue key } end |
#symbolize_keys ⇒ Hash
Returns a new hash with all keys converted to symbols.
85 86 87 |
# File 'lib/croesus/core_ext/hash.rb', line 85 def symbolize_keys transform_keys { |key| key.to_sym rescue key } end |
#transform_keys ⇒ Hash
Returns a new hash with all keys converted using the block operation.
45 46 47 48 49 50 51 52 |
# File 'lib/croesus/core_ext/hash.rb', line 45 def transform_keys enum_for(:transform_keys) unless block_given? result = self.class.new each_key do |key| result[yield(key)] = self[key] end result end |