Class: Hash
Overview
Hash helpers
Instance Method Summary collapse
- #clone ⇒ Object
-
#deep_freeze ⇒ Object
Freeze all values in a hash.
- #deep_freeze! ⇒ Object
-
#deep_set(path, value) ⇒ Object
Set a nested hash value using an array.
- #deep_thaw ⇒ Object
- #deep_thaw! ⇒ Object
-
#delete_unless_key(key, to_delete) ⇒ Object
Delete array of keys unless key exists.
-
#remove_empty ⇒ Object
Remove keys with empty values.
-
#rename_key(old_key, new_key, keep: false) ⇒ Object
Rename a key, deleting old key.
-
#rename_keys(*pairs) ⇒ Object
Rename keys in batch.
-
#stringify_keys ⇒ Hash
Turn all keys into string.
- #stringify_values ⇒ Object
-
#symbolize_keys ⇒ Hash
Turn all keys into symbols.
- #tag_filter_to_options ⇒ Object
-
#to_view ⇒ Hash
Convert an options hash to a view config.
Instance Method Details
#clone ⇒ Object
37 38 39 |
# File 'lib/doing/hash.rb', line 37 def clone Marshal.load(Marshal.dump(self)) end |
#deep_freeze ⇒ Object
Freeze all values in a hash
11 12 13 14 15 16 17 18 |
# File 'lib/doing/hash.rb', line 11 def deep_freeze chilled = {} each do |k, v| chilled[k] = v.is_a?(Hash) ? v.deep_freeze : v.freeze end chilled.freeze end |
#deep_freeze! ⇒ Object
20 21 22 |
# File 'lib/doing/hash.rb', line 20 def deep_freeze! replace deep_thaw.deep_freeze end |
#deep_set(path, value) ⇒ Object
Set a nested hash value using an array
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/doing/hash.rb', line 94 def deep_set(path, value) if path.count == 1 if value.nil? || value =~ /^ *$/ delete(path[0]) else self[path[0]] = value end elsif value self.default_proc = ->(h, k) { h[k] = Hash.new(&h.default_proc) } dig(*path[0..-2])[path.fetch(-1)] = value else return self unless dig(*path) dig(*path[0..-2]).delete(path.fetch(-1)) path.pop cleaned = self path.each do |key| if cleaned[key].empty? cleaned.delete(key) break end cleaned = cleaned[key] end empty? ? nil : self end end |
#deep_thaw ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/doing/hash.rb', line 24 def deep_thaw chilled = {} each do |k, v| chilled[k] = v.is_a?(Hash) ? v.deep_thaw : v.dup end chilled.dup end |
#deep_thaw! ⇒ Object
33 34 35 |
# File 'lib/doing/hash.rb', line 33 def deep_thaw! replace deep_thaw end |
#delete_unless_key(key, to_delete) ⇒ Object
Delete array of keys unless key exists
189 190 191 192 193 |
# File 'lib/doing/hash.rb', line 189 def delete_unless_key(key, to_delete) unless key?(key) to_delete.each { |k| delete(k) } end end |
#remove_empty ⇒ Object
Remove keys with empty values
149 150 151 |
# File 'lib/doing/hash.rb', line 149 def remove_empty delete_if { |k, v| !v.is_a?(FalseClass) && !v.good? } end |
#rename_key(old_key, new_key, keep: false) ⇒ Object
Rename a key, deleting old key
129 130 131 132 133 134 135 |
# File 'lib/doing/hash.rb', line 129 def rename_key(old_key, new_key, keep: false) return unless key?(old_key) self[new_key] = self[old_key] self[new_key.to_s] = self[old_key] if key?(new_key.to_s) delete(old_key) unless keep end |
#rename_keys(*pairs) ⇒ Object
Rename keys in batch
142 143 144 |
# File 'lib/doing/hash.rb', line 142 def rename_keys(*pairs) pairs.each { |p| rename_key(p[0], p[1]) } end |
#stringify_keys ⇒ Hash
Turn all keys into string
If the hash has both a string and a symbol for key, keep the string value, discarding the symnbol value
49 50 51 52 53 54 55 |
# File 'lib/doing/hash.rb', line 49 def stringify_keys each_with_object({}) do |(k, v), hsh| next if k.is_a?(Symbol) && key?(k.to_s) hsh[k.to_s] = v.is_a?(Hash) ? v.stringify_keys : v end end |
#stringify_values ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/doing/hash.rb', line 73 def stringify_values transform_values do |v| if v.is_a?(Hash) v.stringify_values elsif v.is_a?(Symbol) v.to_s else v end end end |
#symbolize_keys ⇒ Hash
Turn all keys into symbols
If the hash has both a string and a symbol for a key, keep the symbol value and discard the string value
65 66 67 68 69 70 71 |
# File 'lib/doing/hash.rb', line 65 def symbolize_keys each_with_object({}) do |(k, v), hsh| next if k.is_a?(String) && key?(k.to_sym) hsh[k.to_sym] = v.is_a?(Hash) ? v.symbolize_keys : v end end |
#tag_filter_to_options ⇒ Object
153 154 155 156 157 158 159 160 161 |
# File 'lib/doing/hash.rb', line 153 def hsh = dup if hsh.key?(:tag_filter) hsh[:tags] = hsh[:tag_filter][:tags] hsh[:bool] = hsh[:tag_filter][:bool] hsh.delete(:tag_filter) end replace hsh end |
#to_view ⇒ Hash
Convert an options hash to a view config
168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/doing/hash.rb', line 168 def to_view hsh = symbolize_keys %w[x save c a s o h e editor m menu i interactive d delete t fuzzy time_filter sort_tags].each do |key| hsh.delete(key.to_sym) if hsh.key?(key.to_sym) end hsh.delete_unless_key(:tag, %i[bool]) hsh.delete_unless_key(:search, %i[exact case]) hsh.rename_keys(%i[not negate], %i[tag tags]) hsh. hsh = hsh.remove_empty.stringify_keys.stringify_values hsh.keys.sort.each_with_object({}) { |k, out| out[k] = hsh[k] } end |