Class: Footing::Hash
Instance Attribute Summary
Attributes inherited from Object
#copied_object, #original_object
Class Method Summary collapse
Instance Method Summary collapse
-
#filter!(keys, replacement: "[FILTERED]") ⇒ Footing::Hash
Recursively filters the values for the specified keys in place.
-
#initialize(o = {}) ⇒ Hash
constructor
A new instance of Hash.
-
#to_h ⇒ Hash
(also: #to_hash)
Returns a standard ruby Hash representation of the wrapped Hash.
-
#update_keys! {|key| ... } ⇒ Footing::Hash
Recursively updates keys in place by passing them though the given block.
Methods inherited from Object
match?, #method_missing, #respond_to?, target_name
Constructor Details
#initialize(o = {}) ⇒ Hash
Returns a new instance of Hash.
11 12 13 14 |
# File 'lib/footing/hash.rb', line 11 def initialize(o={}) super initialize_nested end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Footing::Object
Class Method Details
.new(o = {}) ⇒ Object
5 6 7 8 |
# File 'lib/footing/hash.rb', line 5 def new(o={}) return o if o.is_a?(self) super end |
Instance Method Details
#filter!(keys, replacement: "[FILTERED]") ⇒ Footing::Hash
Recursively filters the values for the specified keys in place. IMPORTANT: This mutates the copied_object.
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 |
# File 'lib/footing/hash.rb', line 73 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 copied_object.each do |key, value| if value.is_a?(Footing::Hash) value.filter!(keys, replacement: replacement) elsif value.is_a?(::Array) value.each do |val| if val.is_a?(Footing::Hash) val.filter!(keys, replacement: replacement) end end else value = replacement if should_replace.call(key) self[key] = value end end self end |
#to_h ⇒ Hash Also known as: to_hash
Returns a standard ruby Hash representation of the wrapped Hash.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/footing/hash.rb', line 18 def to_h copied_object.each_with_object({}) do |pair, memo| value = pair.last if value.is_a?(Footing::Hash) memo[pair.first] = value.to_h elsif value.is_a?(::Array) memo[pair.first] = value.map do |val| if val.is_a?(Footing::Hash) val.to_h else val end end else memo[pair.first] = value end end end |
#update_keys! {|key| ... } ⇒ Footing::Hash
Recursively updates keys in place by passing them though the given block. IMPORTANT: This mutates the copied_object.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/footing/hash.rb', line 44 def update_keys!(&block) @copied_object = copied_object.each_with_object({}) do |pair, memo| key = pair.first new_key = block.call(key) value = pair.last if value.is_a?(Footing::Hash) memo[new_key] = value.update_keys!(&block) elsif value.is_a?(::Array) memo[new_key] = value.map do |val| if val.is_a?(Footing::Hash) val.update_keys!(&block) else val end end else memo[new_key] = value end end self end |