Module: Footing::Hash

Defined in:
lib/footing/extensions/hash.rb

Instance Method Summary collapse

Instance Method Details

#adjust_values! {|value| ... } ⇒ Object

Recursively adjusts the values of the Hash in place.

Examples:

dict = {:a => 1, :b => 2, :c => 3}
dict.adjust_values! { |v| v.to_s }
dict # => {:a => "1", :b => "2", :c => "3"}

Yields:

  • (value)

    Yields the current value to the block. The result of the block is then assigned to the corresponding key.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/footing/extensions/hash.rb', line 54

def adjust_values!(&block)
  each do |key, value|
    if value.respond_to?(:adjust_values!)
      value.adjust_values!(&block)
    elsif value.is_a?(Enumerable)
      value.each do |val|
        next unless val.respond_to?(:adjust_values!)
        val.adjust_values!(&block)
      end
    else
      self[key] = yield(value)
    end
  end
  self
end

#cast_values!Object

Recursively casts all string values in this Hash. See Footing::String#cast



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/footing/extensions/hash.rb', line 72

def cast_values!
  each do |key, value|
    if value.respond_to?(:cast_values!)
      value.cast_values!
    elsif value.is_a?(Enumerable)
      value.each do |val|
        next unless val.respond_to?(:cast_values!)
        val.cast_values!
      end
    elsif value.respond_to?(:cast)
      self[key] = value.cast
    end
  end
  self
end

#copyObject



118
119
120
# File 'lib/footing/extensions/hash.rb', line 118

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

#filter!(keys, replacement = "[FILTERED]") ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/footing/extensions/hash.rb', line 88

def filter!(keys, replacement="[FILTERED]")
  should_replace = lambda do |key|
    replace = false
    keys.each do |k|
      break if replace
      replace = k.is_a?(Regexp) ? key.to_s =~ k : key.to_s == k.to_s
    end
    replace
  end

  each do |key, value|
    if value.respond_to?(:filter!)
      value.filter!(keys, replacement)
    elsif value.is_a?(Enumerable)
      value.each do |val|
        next unless val.respond_to?(:filter!)
        val.filter!(keys, replacement)
      end
    else
      value = replacement if should_replace.call(key)
      self[key] = value
    end
  end
  self
end

#force_encoding!(encoding) {|value| ... } ⇒ Object

Recursively forces all String values to the specified encoding.

Parameters:

  • []

    encoding The encoding to use.

Yields:

  • (value)

    Yields the value after the encoding has been applied.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/footing/extensions/hash.rb', line 25

def force_encoding!(encoding, &block)
  each do |key, value|
    if value.respond_to?(:force_encoding!)
      value.force_encoding!(encoding, &block)
    elsif value.is_a?(Enumerable)
      value.each do |val|
        next unless val.respond_to?(:force_encoding!)
        val.force_encoding!(encoding, &block)
      end
    elsif value.is_a?(String)
      # force encoding then strip all non ascii chars
      if block_given?
        self[key] = yield(value.force_encoding(encoding))
      else
        self[key] = value.force_encoding(encoding)
      end
    end
  end
end

#rekey(method_name) ⇒ Hash

Rekeys the Hash by invoking a method on the existing keys and uses the return value as the new key.

NOTE: Creates and returns a new Hash.

Example:

h = { [1] => "short", [1,2] => "medium", [1,2,3] => "long" }
h.rekey(:length) # => { 1 => "short", 2 => "medium", 3 => "long" }

Parameters:

  • name (Symbol)

    The method name to invoke on the existing keys.

Returns:

  • (Hash)

    A new Hash that has been re-keyed.



15
16
17
18
19
20
# File 'lib/footing/extensions/hash.rb', line 15

def rekey(method_name)
  reduce({}) do |new_hash, (key, value)|
    new_hash[key.public_send(method_name)] = value
    new_hash
  end
end

#silence!(keys) ⇒ Object



114
115
116
# File 'lib/footing/extensions/hash.rb', line 114

def silence!(keys)
  filter! keys, nil
end