Class: Unparser::CLI::Differ

Inherits:
Object
  • Object
show all
Includes:
Adamantium::Flat
Defined in:
lib/unparser/cli/differ.rb

Overview

Class to create diffs from source code

Constant Summary collapse

CONTEXT_LINES =
5

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(old, new) ⇒ Differ

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return new object

Parameters:

  • old (String)
  • new (String)

Returns:



91
92
93
# File 'lib/unparser/cli/differ.rb', line 91

def self.build(old, new)
  new(lines(old), lines(new))
end

Instance Method Details

#collapsed_hunksEnumerable<Diff::LCS::Hunk>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return collapsed hunks

Returns:

  • (Enumerable<Diff::LCS::Hunk>)


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/unparser/cli/differ.rb', line 32

def collapsed_hunks
  hunks.each_with_object([]) do |hunk, output|
    last = output.last

    if last && hunk.merge(last)
      output.pop
    end

    output << hunk
  end
end

#colorized_diffString?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return colorized source diff

Returns:

  • (String)

    if there is a diff

  • (nil)

    otherwise



75
76
77
78
79
# File 'lib/unparser/cli/differ.rb', line 75

def colorized_diff
  diff.lines.map do |line|
    self.class.colorize_line(line)
  end.join
end

#diffString?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return source diff

Returns:

  • (String)

    if there is a diff

  • (nil)

    otherwise



54
55
56
57
58
59
60
61
62
# File 'lib/unparser/cli/differ.rb', line 54

def diff
  output = ''

  collapsed_hunks.each do |hunk|
    output << hunk.diff(:unified) << "\n"
  end

  output
end

#hunksArray<Diff::LCS::Hunk>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return hunks

Returns:

  • (Array<Diff::LCS::Hunk>)


17
18
19
20
21
22
23
24
# File 'lib/unparser/cli/differ.rb', line 17

def hunks
  file_length_difference = new.length - old.length
  diffs.map do |piece|
    hunk = Diff::LCS::Hunk.new(old, new, piece, CONTEXT_LINES, file_length_difference)
    file_length_difference = hunk.file_length_difference
    hunk
  end
end