Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#&(*keys) ⇒ Object

Special intersection method allowing us to intersect a hash with an array of keys. Matches string and symbol keys (like stringify_keys)

For example => 1, :b => :two, ‘c’ => ‘3’ & [:a, :c]

> => 1, ‘c’ => ‘3’



8
9
10
11
12
# File 'lib/ndr_support/hash.rb', line 8

def &(*keys)
  h = {}
  each { |k, v| h[k] = v if keys.flatten.map(&:to_s).include?(k.to_s) }
  h
end

#rawtext_merge(hash2, prevent_overwrite = true) ⇒ Object

This method merges this hash with another, but also merges the :rawtext (rather than replacing the current hashes rawtext with the second). Additionally it can raise a RuntimeError to prevent the second hash overwriting the value for a key from the first.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ndr_support/hash.rb', line 33

def rawtext_merge(hash2, prevent_overwrite = true)
  hash1_rawtext = self[:rawtext] || {}
  hash2_rawtext = hash2[:rawtext] || {}

  if prevent_overwrite
    non_unique_rawtext_keys = hash1_rawtext.keys & hash2_rawtext.keys
    unless non_unique_rawtext_keys.empty?
      fail("Non-unique rawtext keys: #{non_unique_rawtext_keys.inspect}")
    end
    non_unique_non_rawtext_keys = (keys & hash2.keys) - [:rawtext]
    unless non_unique_non_rawtext_keys.empty?
      fail("Non-unique non-rawtext keys: #{non_unique_non_rawtext_keys.inspect}")
    end
  end

  merge(hash2).merge(
    :rawtext => hash1_rawtext.merge(hash2_rawtext)
  )
end

#value_by_path(first_key, *descendant_keys) ⇒ Object

This method allows us to walk the path of nested hashes to reference a value

For example, my_hash = { ‘one’ => ‘1’, ‘two’ => { ‘twopointone’ => ‘2.1’, ‘twopointtwo’ => ‘2.2’ } } my_hash becomes my_hash.value_by_path(‘one’) my_hash[‘twopointone’] becomes my_hash.value_by_path(‘two’, ‘twopointone’)



21
22
23
24
25
26
27
# File 'lib/ndr_support/hash.rb', line 21

def value_by_path(first_key, *descendant_keys)
  result = self[first_key]
  descendant_keys.each do |key|
    result = result[key]
  end
  result
end