Module: ConfigPlus::Merger

Defined in:
lib/config_plus/merger.rb

Constant Summary collapse

MERGER =
->(key, h1, h2) do
  if h1.is_a?(Hash) and h2.is_a?(Hash)
    h1.merge(h2, &MERGER)
  else
    h2
  end
end

Class Method Summary collapse

Class Method Details

.merge(collection1, collection2) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/config_plus/merger.rb', line 11

def self.merge(collection1, collection2)
  return collection2 unless collection1
  return collection1 unless collection2

  if collection1.is_a?(Array) and
      collection2.is_a?(Array)
    collection1.concat(collection2)
  elsif collection2.is_a?(::ConfigPlus::Node)
    collection1.merge(collection2.__send__(:node), &MERGER)
  else
    object = collection2.__send__(:node) if collection2.is_a?(::ConfigPlus::Node)
    object ||= collection2
    collection1.merge(object, &MERGER)
  end
end