class Ecu class LabelListComparison def initialize(left, right) unless left.is_a?(LabelList) && right.is_a?(LabelList) fail "Can only merge LabelLists (left: #{left.class}, right: #{right.class})!" end @left = left.map(&:name).zip(left).to_h @right = right.map(&:name).zip(right).to_h @unified_headers = left.headers + right.headers @unified_subheaders = left.subheaders + right.subheaders @names = Hash.new end def names(criterion) @names[criterion] ||= \ case criterion when :left_exclusive then left.keys - right.keys when :right_exclusive then right.keys - left.keys when :common then left.keys & right.keys when :nonequal then names(:common).select { |n| left.fetch(n) != right.fetch(n) } when :equal then names(:common).select { |n| left.fetch(n) == right.fetch(n) } else fail "Unknown criterion #{criterion}" end end def left_exclusive names(:left_exclusive).map { |name| left.fetch(name) } end def right_exclusive names(:right_exclusive).map { |name| right.fetch(name) } end def differences names(:nonequal).map { |name| [ left.fetch(name), right.fetch(name) ] } end def merge(priority: :right) labels = [] left.each do |name, label| if names(:common).include?(name) && priority == :right labels << right.fetch(name) else labels << left.fetch(name) end end right.each do |name, label| next if labels.map(&:name).include?(name) if names(:common).include?(name) && priority == :left labels << left.fetch(name) else labels << right.fetch(name) end end LabelList.new(labels, @unified_headers, @unified_subheaders) end private attr_reader :left, :right end end