Module: Percolate::Util

Defined in:
lib/percolate/util.rb

Overview

Contains utility methods.

Class Method Summary collapse

Class Method Details

.merge_attributes(lhs, rhs) ⇒ Object

Merges the given attributes, which can take the form of nested ‘Hash`es, `Array`s, and `String`s. If there is a conflict, the right hand side wins.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/percolate/util.rb', line 24

def self.merge_attributes(lhs, rhs)
  if lhs.is_a?(Hash) && rhs.is_a?(Hash)
    res = {}

    lhs.each_pair do |key, value|
      if rhs.include?(key)
        res[key] = merge_attributes(lhs[key], rhs[key])
      else
        res[key] = value
      end
    end

    rhs.each_pair do |key, value|
      res[key] = value if !res.include?(key)
    end
  elsif lhs.is_a?(Array) && rhs.is_a?(Array)
    res = lhs + rhs
  else
    res = rhs
  end

  res
end