Class: Ecu::LabelListComparison

Inherits:
Object
  • Object
show all
Defined in:
lib/ecu/labels/label_list_comparison.rb

Instance Method Summary collapse

Constructor Details

#initialize(left, right) ⇒ LabelListComparison

Returns a new instance of LabelListComparison.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/ecu/labels/label_list_comparison.rb', line 4

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

Instance Method Details

#differencesObject



36
37
38
# File 'lib/ecu/labels/label_list_comparison.rb', line 36

def differences
  names(:nonequal).map { [ left.fetch(_1), right.fetch(_1) ] }
end

#identical?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
# File 'lib/ecu/labels/label_list_comparison.rb', line 40

def identical?
  names(:left_exclusive).empty? &&
    names(:right_exclusive).empty? &&
    names(:nonequal).empty?
end

#left_exclusiveObject



28
29
30
# File 'lib/ecu/labels/label_list_comparison.rb', line 28

def left_exclusive
  names(:left_exclusive).map { left.fetch(_1) }
end

#merge(priority: :right) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ecu/labels/label_list_comparison.rb', line 46

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

#names(criterion) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ecu/labels/label_list_comparison.rb', line 16

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

#right_exclusiveObject



32
33
34
# File 'lib/ecu/labels/label_list_comparison.rb', line 32

def right_exclusive
  names(:right_exclusive).map { right.fetch(_1) }
end