Class: Dyph::Differ

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

Class Method Summary collapse

Class Method Details

.default_diff2TwoWayDiffer

Returns:

  • (TwoWayDiffer)


64
65
66
# File 'lib/dyph/differ.rb', line 64

def self.default_diff2
  Dyph::TwoWayDiffers::HeckelDiff
end

.default_diff3ThreeWayDiffer

Returns:

  • (ThreeWayDiffer)


69
70
71
# File 'lib/dyph/differ.rb', line 69

def self.default_diff3
  Dyph::Support::Diff3
end

.default_merge_optionsHash

Returns the default options for a merge.

Returns:

  • (Hash)

    the default options for a merge



52
53
54
55
56
57
58
59
60
61
# File 'lib/dyph/differ.rb', line 52

def self.default_merge_options
  {
    split_function: identity,
    join_function: identity,
    conflict_function: identity,
    diff2: default_diff2,
    diff3: default_diff3,
    use_class_processors: true
  }
end

.identityProc

Returns helper proc for identity.

Returns:

  • (Proc)

    helper proc for identity



47
48
49
# File 'lib/dyph/differ.rb', line 47

def self.identity
  -> (x) { x }
end

.merge(left, base, right, options = {}) ⇒ MergeResult

Perform a three way diff, which attempts to merge left and right relative to a common base

Parameters:

  • left (Object)
  • base (Object)
  • right (Object)
  • options (Hash) (defaults to: {})

    custom split, join, conflict functions, can also override the diff2 and diff3 algorithm. (see default_merge_options)

Returns:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/dyph/differ.rb', line 9

def self.merge(left, base, right, options = {})
  options = default_merge_options.merge(options)

  split_function, join_function, conflict_function = set_processors(base, options)
  split_left, split_base, split_right = [left, base, right].map { |t| split_function.call(t) }
  merge_result = Dyph::Support::Merger.merge(split_left, split_base, split_right, diff2: options[:diff2], diff3: options[:diff3] )
  collated_merge_results = Dyph::Support::Collater.collate_merge(merge_result, join_function, conflict_function)

  if collated_merge_results.success?
    Dyph::Support::SanityCheck.ensure_no_lost_data(split_left, split_base, split_right, collated_merge_results.results)
  end

  collated_merge_results
end

.split_on_new_lineProc

Returns helper proc for keeping newlines on string.

Returns:

  • (Proc)

    helper proc for keeping newlines on string



37
38
39
# File 'lib/dyph/differ.rb', line 37

def self.split_on_new_line
  -> (some_string) { some_string.split(/(\n)/).each_slice(2).map { |x| x.join } }
end

.standard_joinProc

Returns helper proc for joining an array.

Returns:

  • (Proc)

    helper proc for joining an array



42
43
44
# File 'lib/dyph/differ.rb', line 42

def self.standard_join
  -> (array) { array.join }
end

.two_way_diff(left, right, options = {}) ⇒ Array

Perform a two way diff

Parameters:

  • left (Array)
  • right (Array)
  • options (Hash) (defaults to: {})

    Pass in an optional diff2 class

Returns:

  • (Array)

    array of Dyph::Action



29
30
31
32
33
34
# File 'lib/dyph/differ.rb', line 29

def self.two_way_diff(left, right, options = {})
  diff2 = options[:diff2] || default_diff2
  diff_results = diff2.execute_diff(left, right)
  raw_merge = Dyph::TwoWayDiffers::OutputConverter.merge_results(diff_results[:old_text], diff_results[:new_text],)
  Dyph::TwoWayDiffers::OutputConverter.objectify(raw_merge)
end