Module: Thailand::Utils
- Defined in:
- lib/thailand/utils.rb
Class Method Summary collapse
-
.deep_hash_merge(hashes) ⇒ Object
Merge an array of hashes deeply Returns a merged hash.
-
.merge_arrays_by_keys(arrays, keys) ⇒ Object
Merge arrays of hashes using the specified keys Returns a single merges array of hashes.
Class Method Details
.deep_hash_merge(hashes) ⇒ Object
Merge an array of hashes deeply Returns a merged hash
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/thailand/utils.rb', line 5 def self.deep_hash_merge(hashes) return hashes.first if hashes.size == 1 hashes.inject do |acc, hash| acc.merge(hash) do |_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 end end |
.merge_arrays_by_keys(arrays, keys) ⇒ Object
Merge arrays of hashes using the specified keys Returns a single merges array of hashes
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/thailand/utils.rb', line 21 def self.merge_arrays_by_keys(arrays, keys) arrays.each_with_object([]) do |array, aggregate| 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 existing ? existing.merge!(new_hash) : aggregate << new_hash end end end |