Module: Protos::Mix

Defined in:
lib/protos/mix.rb

Constant Summary collapse

MERGEABLE_ATTRIBUTES =
Set.new(i[class data]).freeze

Class Method Summary collapse

Class Method Details

.call(old_hash, *hashes) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/protos/mix.rb', line 14

def call(old_hash, *hashes)
  hashes
    .compact
    .each_with_object(old_hash) do |new_hash, result|
      merge(result, new_hash, top_level: true)
    end
end

.merge(old_hash, new_hash, top_level: false) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/protos/mix.rb', line 22

def merge(old_hash, new_hash, top_level: false) # rubocop:disable Metrics/PerceivedComplexity
  old_hash.merge!(new_hash) do |key, old, new|
    next old unless new
    next old if old == new
    next new if top_level && !MERGEABLE_ATTRIBUTES.include?(key)

    case [old, new]
    in String, String then "#{old} #{new}"
    in Array, Array then old + new
    in Hash, Hash then merge(old, new)
    else new
    end
  end
end