Class: Reyes::Diff

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

Instance Method Summary collapse

Constructor Details

#initializeDiff

Returns a new instance of Diff.



3
4
5
6
# File 'lib/reyes/diff.rb', line 3

def initialize
  @old = Tempfile.new('diff.old.')
  @new = Tempfile.new('diff.new.')
end

Instance Method Details

#diffString?

Consumes the Diff, returning the full unified diff of @old and @new

Returns:

  • (String, nil)

    String diff or nil if the files do not differ



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/reyes/diff.rb', line 10

def diff
  raise "Diff has already been created" unless @old && @new

  [@old, @new].each do |f|
    f.flush
  end

  output = nil
  cmd = ["diff", '-u', @old.path, @new.path]
  status = Subprocess.call(cmd, :stdout => Subprocess::PIPE) do |c|
    output, _ = c.communicate
  end

  case status.exitstatus
  when 0
    # no diff
    return nil
  when 1
    # files differ
    return output
  else
    # trouble
    raise Subprocess::NonZeroExit.new(cmd, status)
  end
ensure
  @old.unlink
  @old = nil
  @new.unlink
  @new = nil
end

#newFile

Returns a handle to the underlying File for new, allowing the caller to populate it’s contents

Returns:

  • (File)


51
52
53
# File 'lib/reyes/diff.rb', line 51

def new
  @new
end

#oldFile

Returns a handle to the underlying File for old, allowing the caller to populate it’s contents

Returns:

  • (File)


44
45
46
# File 'lib/reyes/diff.rb', line 44

def old
  @old
end