Class: Hash

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

Overview

Stole this from Rails; rather copy the source than introduce ActiveSupport as a dependency

Instance Method Summary collapse

Instance Method Details

#deep_compactObject

Returns a hash, removing all elements that respond to and return true from :empty? Iterates recursively over nested hashes Will continue to call itself until the second run does not differ from the first (kind of gross) TODO: Try to make this less gross



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rulix/core_ext/hash.rb', line 68

def deep_compact
  result = dup.deep_compact!
  result2 = result.dup.deep_compact!

  if result != result2
    result = result2

    result.deep_compact
  end

  result
end

#deep_compact!Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rulix/core_ext/hash.rb', line 81

def deep_compact!
  each_pair do |current_key, value|
    this_value = self[current_key]

    if this_value.respond_to?(:empty?)
      if this_value.empty?
        self.delete current_key
      elsif this_value.is_a?(Hash)
        self[current_key] = this_value.deep_compact
      end
    else
      self.delete current_key if this_value.nil?
    end
  end
end

#deep_merge(other_hash, &block) ⇒ Object

Returns a new hash with self and other_hash merged recursively.

h1 = { a: true, b: { c: [1, 2, 3] } }
h2 = { a: false, b: { x: [3, 4, 5] } }

h1.deep_merge(h2) # => { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } }

Like with Hash#merge in the standard library, a block can be provided to merge values:

h1 = { a: 100, b: 200, c: { c1: 100 } }
h2 = { b: 250, c: { c1: 200 } }
h1.deep_merge(h2) { |key, this_val, other_val| this_val + other_val }
# => { a: 100, b: 450, c: { c1: 300 } }


18
19
20
# File 'lib/rulix/core_ext/hash.rb', line 18

def deep_merge(other_hash, &block)
  dup.deep_merge!(other_hash, &block)
end

#deep_merge!(other_hash, &block) ⇒ Object

Same as deep_merge, but modifies self.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rulix/core_ext/hash.rb', line 23

def deep_merge!(other_hash, &block)
  other_hash.each_pair do |current_key, other_value|
    this_value = self[current_key]

    self[current_key] = if this_value.is_a?(Hash) && other_value.is_a?(Hash)
      this_value.deep_merge(other_value, &block)
    else
      if block_given? && key?(current_key)
        block.call(current_key, this_value, other_value)
      else
        other_value
      end
    end
  end

  self
end

#deep_reject(&block) ⇒ Object

Returns a hash, removing all values that cause the block to evaluate to true Iterates recursively over nested hashes



43
44
45
# File 'lib/rulix/core_ext/hash.rb', line 43

def deep_reject &block
  dup.deep_reject! &block
end

#deep_reject!(&block) ⇒ Object

Same as deep_reject, but modifies self.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rulix/core_ext/hash.rb', line 48

def deep_reject! &block
  each_pair do |current_key, value|
    this_value = self[current_key]

    if this_value.is_a?(Hash)
      self[current_key] = this_value.deep_reject &block
    else
      if block_given? && key?(current_key)
        self.delete current_key if block.call current_key, this_value
      end
    end
  end
end