Class: DiffViewer

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

Overview

指定されたソースの差分を作成する

書式はなんちゃってunified contextなので、パッチ作成には使えません

Defined Under Namespace

Classes: InvalidSourceType

Instance Method Summary collapse

Constructor Details

#initialize(old, new, source = :file) ⇒ DiffViewer

Returns a new instance of DiffViewer.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/diffviewer.rb', line 16

def initialize(old, new, source = :file)
  case source
  when :file
    old_strings = File.read(old, :encoding => Encoding::UTF_8)
    new_strings = File.read(new, :encoding => Encoding::UTF_8)
  when :string
    old_strings = old
    new_strings = new
  else
    raise InvalidSourceType, "source supported options are ':file' and ':string'"
  end
  old_strings.gsub!("\r", "")
  new_strings.gsub!("\r", "")
  @builded_buffer = ""
  @events = Diff::LCS.sdiff(old_strings.split("\n"), new_strings.split("\n"))
  build
end

Instance Method Details

#to_sObject



34
35
36
# File 'lib/diffviewer.rb', line 34

def to_s
  @builded_buffer
end

#viewObject



38
39
40
# File 'lib/diffviewer.rb', line 38

def view
  puts @builded_buffer
end