Class: Dyph::TwoWayDiffers::HeckelDiff

Inherits:
Object
  • Object
show all
Defined in:
lib/dyph/two_way_differs/heckel_diff.rb

Defined Under Namespace

Classes: ChangeData

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(left, right) ⇒ HeckelDiff

Returns a new instance of HeckelDiff.



22
23
24
25
# File 'lib/dyph/two_way_differs/heckel_diff.rb', line 22

def initialize(left, right)
  @left = left
  @right = right
end

Class Method Details

.diff(left, right) ⇒ Object

Two-way diff based on the algorithm by P. Heckel.

Parameters:

  • left (in)

    Array of anything implementing hash and equals

  • right (in)

    Array of anything implementing hash and equals



17
18
19
20
# File 'lib/dyph/two_way_differs/heckel_diff.rb', line 17

def self.diff(left, right)
  differ = HeckelDiff.new(left,right)
  differ.perform_diff
end

.execute_diff(old_text_array, new_text_array) ⇒ Object

Raises:

  • (ArgumentError)


7
8
9
10
11
12
# File 'lib/dyph/two_way_differs/heckel_diff.rb', line 7

def self.execute_diff(old_text_array, new_text_array)
  raise ArgumentError, "Argument is not an array." unless old_text_array.is_a?(Array) && new_text_array.is_a?(Array)
  diff_result = diff(old_text_array, new_text_array)
  # convert to typed differ's output (wrapped with change types eg. Add, Delete, Change)
  HeckelDiffWrapper.new(old_text_array, new_text_array, diff_result).convert_to_typed_ouput
end

Instance Method Details

#perform_diffObject



27
28
29
30
31
32
33
34
# File 'lib/dyph/two_way_differs/heckel_diff.rb', line 27

def perform_diff
  unique_positions = identify_unique_postions
  unique_positions.sort!{ |a, b| a[0] <=> b[0] }    # sort by the line in which the line was found in a
  left_change_pos, right_change_pos = find_next_change
  init_changes = ChangeData.new(left_change_pos, right_change_pos, [])
  final_changes = unique_positions.reduce(init_changes, &method(:get_differences))
  final_changes.change_ranges
end