Module: Wdiff

Defined in:
lib/wdiff.rb

Defined Under Namespace

Modules: Helper

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bin_pathObject



13
14
15
# File 'lib/wdiff.rb', line 13

def bin_path
  'wdiff'
end

.diff(old_str, new_str, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/wdiff.rb', line 37

def diff(old_str, new_str, options={})
  f1, f2 = Tempfile.new('f1'), Tempfile.new('f2')
  f1.write(old_str); f1.flush
  f2.write(new_str); f2.flush
  opt_str = options_string_from_hash(options)
  raw = %x{#{bin_path} #{opt_str} #{f1.path} #{f2.path}}
  f1.close; f2.close
  raw
end

.included(target) ⇒ Object



4
5
6
# File 'lib/wdiff.rb', line 4

def included(target)
  verify_wdiff_in_path
end

.options_string_from_hash(options = {}) ⇒ Object

TODO - this will fail if option strings include quotes, need to escape quotes automagically



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/wdiff.rb', line 18

def options_string_from_hash(options={})
  parts = []

  options[:inserts].each_with_index do |token, index|
    token.gsub!(/^\"$/,'\"') if token == '"'
    parts << ( index.even? ?  %Q(--start-insert="#{token}")  :  %Q(--end-insert="#{token}") )
    break if index >= 1
  end if options[:inserts]

  
  options[:deletes].each_with_index do |token, index|
    token.gsub!(/^\"$/,'\"') if token == '"'
    parts << ( index.even? ?  %Q(--start-delete="#{token}")  :  %Q(--end-delete="#{token}") )
    break if index >= 1
  end if options[:deletes]
  
  parts.join(" ")
end

.verify_wdiff_in_pathObject



8
9
10
11
# File 'lib/wdiff.rb', line 8

def verify_wdiff_in_path
  path = %x{which #{bin_path}}
  raise "GNU wdiff (http://www.gnu.org/software/wdiff/) not found in $PATH" if path.empty?
end

Instance Method Details

#wdiff(str_new, options = {}) ⇒ Object



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

def wdiff(str_new, options={})
  Wdiff::diff(self, str_new, options)
end