Class: Black::Diff

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/black/diff.rb

Defined Under Namespace

Modules: OutputParser Classes: Line

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(older, newer, filename = nil) ⇒ Diff

Returns a new instance of Diff.



13
14
15
16
17
# File 'lib/black/diff.rb', line 13

def initialize(older, newer, filename=nil)
  @older = older
  @newer = newer
  @filename = filename
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



11
12
13
# File 'lib/black/diff.rb', line 11

def filename
  @filename
end

#newerObject (readonly)

Returns the value of attribute newer.



11
12
13
# File 'lib/black/diff.rb', line 11

def newer
  @newer
end

#olderObject (readonly)

Returns the value of attribute older.



11
12
13
# File 'lib/black/diff.rb', line 11

def older
  @older
end

Class Method Details

.execute(*command_args) ⇒ Object

wrapper to execute ‘diff` in the shell. Reimplement this to suit your own platform needs



5
6
7
# File 'lib/black/diff.rb', line 5

def self.execute(*command_args)
  `diff #{ command_args.join(' ') }`
end

Instance Method Details

#diff_parsedObject



48
49
50
# File 'lib/black/diff.rb', line 48

def diff_parsed
  OutputParser.parse(execute.enum_for(:lines))
end

#eachObject



19
20
21
22
23
24
25
26
27
# File 'lib/black/diff.rb', line 19

def each
  if block_given?
    diff_parsed.each do |line|
      yield line
    end
  else
    diff_parsed.enum_for(:each) 
  end
end

#executeObject

diff two strings using unix ‘diff’ command



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/black/diff.rb', line 30

def execute
  files = [Tempfile.new('black'), Tempfile.new('black')]
  files.first.write(@older)
  files.last.write(@newer)
  files.each { |a| a.read }

  args = [files.first.path, files.last.path] + OutputParser.diff_options

  Diff.execute(args)
ensure
  files.each do |file|
    if file && File.exist?(file.path)
      file.close
      file.unlink
    end
  end
end