Class: Hash

Inherits:
Object show all
Defined in:
lib/doing/hash.rb

Overview

Hash helpers

Instance Method Summary collapse

Instance Method Details

#cloneObject



37
38
39
# File 'lib/doing/hash.rb', line 37

def clone
  Marshal.load(Marshal.dump(self))
end

#deep_freezeObject

Freeze all values in a hash

Returns:

  • Hash with all values frozen



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

Examples:

{}.deep_set(['one', 'two'], 'value')
# => { 'one' => { 'two' => 'value' } }

Parameters:

  • path (Array)

    key path

  • value

    The value



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/doing/hash.rb', line 100

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_thawObject



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

Parameters:

  • key

    The key to verify

  • to_delete (Array)

    the keys to delete if key doesn't exist



195
196
197
198
199
# File 'lib/doing/hash.rb', line 195

def delete_unless_key(key, to_delete)
  unless key?(key)
    to_delete.each { |k| delete(k) }
  end
end

#remove_emptyObject

Remove keys with empty values



155
156
157
# File 'lib/doing/hash.rb', line 155

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

Parameters:

  • old_key

    The original key

  • new_key

    The new key

  • keep (Boolean) (defaults to: false)

    if true, keep old key in addition to new key



135
136
137
138
139
140
141
# File 'lib/doing/hash.rb', line 135

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

Parameters:

  • pairs (Array)

    pairs of old and new keys



148
149
150
# File 'lib/doing/hash.rb', line 148

def rename_keys(*pairs)
  pairs.each { |p| rename_key(p[0], p[1]) }
end

#stringify_keysHash

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

Returns:

  • (Hash)

    a copy of the hash where all its keys are strings



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_valuesHash

Turn all non-numeric values into strings

Returns:

  • (Hash)

    a copy of the hash where all non-numeric values are strings



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/doing/hash.rb', line 79

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_keysHash

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

Returns:

  • (Hash)

    a copy of the hash where all its keys are symbols



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_optionsObject



159
160
161
162
163
164
165
166
167
# File 'lib/doing/hash.rb', line 159

def tag_filter_to_options
  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_viewHash

Convert an options hash to a view config

Returns:

  • (Hash)

    View representation of the object.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/doing/hash.rb', line 174

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.tag_filter_to_options

  hsh = hsh.remove_empty.stringify_keys.stringify_values
  hsh.keys.sort.each_with_object({}) { |k, out| out[k] = hsh[k] }
end