Class: Glossarist::ConceptComparator

Inherits:
Object
  • Object
show all
Defined in:
lib/glossarist/concept_comparator.rb

Instance Method Summary collapse

Constructor Details

#initialize(new_concepts:, old_concepts:) ⇒ ConceptComparator

Returns a new instance of ConceptComparator.



5
6
7
8
# File 'lib/glossarist/concept_comparator.rb', line 5

def initialize(new_concepts:, old_concepts:)
  @new_concepts = new_concepts
  @old_concepts = old_concepts
end

Instance Method Details

#compare(show_diffs: true) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/glossarist/concept_comparator.rb', line 10

def compare(show_diffs: true)
  new_index = build_index(@new_concepts)
  old_index = build_index(@old_concepts)

  matched_ids = new_index.keys & old_index.keys
  new_only_ids = new_index.keys - old_index.keys
  old_only_ids = old_index.keys - new_index.keys

  diffs = if show_diffs
            compute_diffs(matched_ids, new_index, old_index)
          else
            []
          end

  ComparisonResult.new(
    new_count: @new_concepts.length,
    old_count: @old_concepts.length,
    matched: matched_ids.sort,
    new_only: new_only_ids.sort,
    old_only: old_only_ids.sort,
    diffs: diffs,
  )
end