Class: Hash
Overview
Extension class for Hash
Instance Method Summary collapse
-
#apply_defaults(other_hash) ⇒ Object
Apply other hash values if current value is blank.
-
#apply_defaults!(other_hash) ⇒ Object
Apply in-place other hash values if current value is blank.
-
#cleanup ⇒ Object
Removes all hash entries for which value.empty? is true.
-
#key_strings_to_symbols(options = {}) ⇒ Object
Return new Hash with all keys converted to symbols.
-
#key_strings_to_symbols!(options = {}) ⇒ Object
Convert all keys to symbols.
-
#key_symbols_to_strings(options = {}) ⇒ Object
Return new Hash with all keys converted to strings.
-
#key_symbols_to_strings!(options = {}) ⇒ Object
Convert all keys to strings.
-
#recursive_cleanup ⇒ Object
Removes all hash entries for which value.empty? is true.
-
#recursive_merge(other_hash) ⇒ Object
Merges two hashes, but does so recursively.
-
#recursive_merge!(other_hash) ⇒ Object
Merges two hashes in-place, but does so recursively.
-
#reverse_merge(other_hash) ⇒ Object
Merges two hashes with priority for the first hash.
-
#reverse_merge!(other_hash) ⇒ Object
Merges two hashes in-place with priority for the first hash.
Instance Method Details
#apply_defaults(other_hash) ⇒ Object
Apply other hash values if current value is blank
50 51 52 |
# File 'lib/libis/tools/extend/hash.rb', line 50 def apply_defaults(other_hash) self.merge(other_hash) {|_,v, w| v.blank? ? w : v} end |
#apply_defaults!(other_hash) ⇒ Object
Apply in-place other hash values if current value is blank
55 56 57 |
# File 'lib/libis/tools/extend/hash.rb', line 55 def apply_defaults!(other_hash) self.merge!(other_hash) {|_,v, w| v.blank? ? w : v} end |
#cleanup ⇒ Object
Removes all hash entries for which value.empty? is true
7 8 9 |
# File 'lib/libis/tools/extend/hash.rb', line 7 def cleanup self.delete_if { |_,v| v.nil? || (v.respond_to?(:empty?) ? v.empty? : false) } end |
#key_strings_to_symbols(options = {}) ⇒ Object
Return new Hash with all keys converted to symbols.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/libis/tools/extend/hash.rb', line 71 def key_strings_to_symbols( = {}) = {resursive: false, upcase: false, downcase: false}.merge r = Hash.new self.each_pair do |k,v| k = k.to_s if k.kind_of? Symbol if k.kind_of? String k = k.downcase if [:downcase] k = k.upcase if [:upcase] k = k.to_sym end if [:recursive] case v when Hash v = v.key_strings_to_symbols when Array # noinspection RubyResolve v = v.collect { |a| (a.kind_of? Hash) ? a.key_strings_to_symbols() : Marshal.load(Marshal.dump(a)) } else # noinspection RubyResolve v = Marshal.load(Marshal.dump(v)) end end r[k] = v end r end |
#key_strings_to_symbols!(options = {}) ⇒ Object
Convert all keys to symbols. In-place operation.
61 62 63 |
# File 'lib/libis/tools/extend/hash.rb', line 61 def key_strings_to_symbols!( = {}) self.replace self.key_strings_to_symbols end |
#key_symbols_to_strings(options = {}) ⇒ Object
Return new Hash with all keys converted to strings. (see #key_strings_to_symbols)
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/libis/tools/extend/hash.rb', line 114 def key_symbols_to_strings( = {}) = {resursive: false, upcase: false, downcase: false}.merge r = Hash.new self.each_pair do |k,v| k = k.to_sym if k.kind_of? String if k.kind_of? Symbol k = k.to_s k = k.downcase if [:downcase] k = k.upcase if [:upcase] end if [:recursive] case v when Hash v = v.key_symbols_to_strings() when Array # noinspection RubyResolve v = v.collect { |a| (a.kind_of? Hash) ? a.key_symbols_to_strings() : Marshal.load(Marshal.dump(a)) } else # noinspection RubyResolve v = Marshal.load(Marshal.dump(v)) end end r[k] = v end r end |
#key_symbols_to_strings!(options = {}) ⇒ Object
Convert all keys to strings. In-place operation. (@see #key_symbols_to_strings)
107 108 109 |
# File 'lib/libis/tools/extend/hash.rb', line 107 def key_symbols_to_strings!( = {}) self.replace self.key_symbols_to_strings end |
#recursive_cleanup ⇒ Object
Removes all hash entries for which value.empty? is true. Performed recursively.
12 13 14 15 |
# File 'lib/libis/tools/extend/hash.rb', line 12 def recursive_cleanup each { |_, v| v.recursive_cleanup if Array === v || Hash === v } cleanup end |
#recursive_merge(other_hash) ⇒ Object
Merges two hashes, but does so recursively.
18 19 20 21 22 23 24 25 26 |
# File 'lib/libis/tools/extend/hash.rb', line 18 def recursive_merge(other_hash) self.merge(other_hash) do |_, old_val, new_val| if old_val.is_a? Hash old_val.recursive_merge new_val else new_val end end end |
#recursive_merge!(other_hash) ⇒ Object
Merges two hashes in-place, but does so recursively.
29 30 31 32 33 34 35 36 37 |
# File 'lib/libis/tools/extend/hash.rb', line 29 def recursive_merge!(other_hash) self.merge!(other_hash) do |_, old_val, new_val| if old_val.is_a? Hash old_val.recursive_merge new_val else new_val end end end |
#reverse_merge(other_hash) ⇒ Object
Merges two hashes with priority for the first hash
40 41 42 |
# File 'lib/libis/tools/extend/hash.rb', line 40 def reverse_merge(other_hash) self.merge(other_hash) {|_,v, _| v} end |
#reverse_merge!(other_hash) ⇒ Object
Merges two hashes in-place with priority for the first hash
45 46 47 |
# File 'lib/libis/tools/extend/hash.rb', line 45 def reverse_merge!(other_hash) self.merge!(other_hash) {|_,v, _| v} end |