lib/analdiffist/diff_set.rb in analdiffist-0.3.0 vs lib/analdiffist/diff_set.rb in analdiffist-0.4.0
- old
+ new
@@ -4,27 +4,50 @@
@before = before
@after = after
end
def added_problems
- compare(@after, @before)
+ differences.select {|difference| difference.score > 0 }
end
def removed_problems
- compare(@before, @after)
+ differences.select {|difference| difference.score < 0 }
end
private
- def compare(a,b)
- #TODO: move this comparison into the class?
- a.map do |problem|
- matching_problem = b.detect {|problem2| [problem.type, problem.context] == [problem2.type, problem2.context] }
+ def differences
+ @differences ||= calculate_differences
+ end
- problem.diff(matching_problem)
+ def calculate_differences
+ diffs = []
+ before_by_context = @before.group_by {|b| [b.type, b.context]}
+ after_by_context = @after.group_by {|b| [b.type, b.context]}
+ all_contexts = (before_by_context.keys + after_by_context.keys).uniq
- end.reject {|x| x.nil?}
+ all_contexts.each do |context|
+
+ before = before_by_context[context] || []
+ after = after_by_context[context] || []
+
+ diffs.concat(calculate_diffs_for_a_context(before, after))
+ end
+ diffs.reject(&:nil?)
end
- end
- class ScoreDiffSet
+ def calculate_diffs_for_a_context before, after
+ diffs = []
+ while before.any? || after.any? do
+ diff = get_diff(before.pop, after.pop)
+ diffs << diff unless diff.nil?
+ end
+ diffs
+ end
+
+ def get_diff before, after
+ return after.diff(before) unless after.nil?
+ diff = before.diff(after)
+ return nil if diff.nil?
+ InvertedDiff.new(diff)
+ end
end
end