Module: Carmen::Utils

Defined in:
lib/carmen/utils.rb

Class Method Summary collapse

Class Method Details

.deep_hash_merge(hashes) ⇒ Object

Merge an array of hashes deeply.

When a conflict occurs:

- if both the old value and the new value respond_to? :merge, they
are recursively passed to deep_merge_hash and the result used.
- if either doesn't respond_to? :merge, then the new value is used if
it is not nil. If the new value is nil, the old value is used.

Returns a merged hash.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/carmen/utils.rb', line 12

def self.deep_hash_merge(hashes)
  return hashes.first if hashes.size == 1

  hashes.inject { |acc, hash|
    acc.merge(hash) { |key, old_value, new_value|
      if old_value.respond_to?(:merge) && new_value.respond_to?(:merge)
        deep_hash_merge([old_value, new_value])
      else
        new_value || old_value
      end
    }
  }
end

.merge_arrays_by_keys(arrays, keys) ⇒ Object

Merge arrays of hashes using the specified keys.

If two hashes have the same value for a key, they are merged together. Otherwise, a new hash is appended to the array.

Matching arrays uses the keys in the order they are provided.

Returns a single merges array of hashes.



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

def self.merge_arrays_by_keys(arrays, keys)
  arrays.inject do |aggregate, array|

    array.each do |new_hash|
      # Find the matching element in the agregate array
      existing = aggregate.find do |hash|
        keys.any? {|key| hash[key] && hash[key] == new_hash[key] }
      end

      # Merge the new hash to an existing one, or append it if new
      if existing
        existing.merge!(new_hash)
      else
        aggregate << new_hash
      end
    end

    aggregate
  end

end