Class: Unparser::Diff

Inherits:
Object
  • Object
show all
Includes:
Adamantium
Defined in:
lib/unparser/diff.rb

Overview

Class to create diffs from source code

Constant Summary collapse

ADDITION =
'+'
DELETION =
'-'
NEWLINE =
"\n"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(old, new) ⇒ Diff

Build new object from source strings

Parameters:

  • old (String)
  • new (String)

Returns:



46
47
48
# File 'lib/unparser/diff.rb', line 46

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

Instance Method Details

#colorized_diffString?

Colorized unified source diff between old and new

Returns:

  • (String)

    if there is a diff

  • (nil)

    otherwise



33
34
35
36
37
# File 'lib/unparser/diff.rb', line 33

def colorized_diff
  return unless diff

  diff.lines.map(&self.class.method(:colorize_line)).join
end

#diffString?

Unified source diff between old and new

Returns:

  • (String)

    if there is exactly one diff

  • (nil)

    otherwise



19
20
21
22
23
# File 'lib/unparser/diff.rb', line 19

def diff
  return if diffs.empty?

  minimized_hunk.diff(:unified) + NEWLINE
end