Class: Potluck::Nginx::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/potluck/nginx/util.rb

Overview

Utility methods for Nginx class.

Class Method Summary collapse

Class Method Details

.deep_merge(*hashes, arrays: false) ⇒ Object

Merges N hashes by merging nested hashes rather than overwriting them as is the case with Hash#merge.

  • hashes - Hashes to deep merge.

  • arrays - True if arrays should be merged rather than overwritten (optional, default: false).

Example:

h1 = {hello: {item1: 'world'}}
h2 = {hello: {item2: 'friend'}}

Util.deep_merge(h1, h2)
# => {hello: {item1: 'world', item2: 'friend'}}

By default only hashes are merged and arrays are still overwritten as they are with Hash#merge. Passing arrays: true will result in arrays being merged similarly to hashes. Example:

h1 = {hello: {item1: ['world']}}
h2 = {hello: {item1: ['friend']}}

Util.deep_merge(h1, h2, arrays: true)
# => {hello: {item1: ['world', 'friend']}}


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/potluck/nginx/util.rb', line 34

def self.deep_merge(*hashes, arrays: false)
  hash = hashes[0].dup

  hashes[1..-1].each do |other_hash|
    other_hash.each do |key, other_value|
      this_value = hash[key]

      if this_value.kind_of?(Hash) && other_value.kind_of?(Hash)
        hash[key] = deep_merge(this_value, other_value, arrays: arrays)
      elsif arrays && this_value.kind_of?(Array)
        hash[key] |= Array(other_value)
      else
        hash[key] = other_value
      end
    end
  end

  hash
end