class Diffy::Diff
Attributes
default_format[W]
default_options[W]
diff[R]
options[R]
string1[R]
string2[R]
Public Class Methods
default_format()
click to toggle source
# File lib/diffy/diff.rb, line 5 def default_format @default_format || :text end
default_options()
click to toggle source
default options passed to new Diff objects
# File lib/diffy/diff.rb, line 11 def default_options @default_options ||= { :diff => '-U 10000', :source => 'strings', :include_diff_info => false, :include_plus_and_minus_in_html => false, :context => nil, :allow_empty_diff => true, } end
new(string1, string2, options = {})
click to toggle source
supported options
:diff
-
A cli options string passed to diff
:source
-
Either strings or files. Determines whether string1 and string2 should be interpreted as strings or file paths.
:include_diff_info
-
Include diff header info
:include_plus_and_minus_in_html
-
Show the +, -, ' ' at the beginning of lines in html output.
# File lib/diffy/diff.rb, line 33 def initialize(string1, string2, options = {}) @options = self.class.default_options.merge(options) if ! ['strings', 'files'].include?(@options[:source]) raise ArgumentError, "Invalid :source option #{@options[:source].inspect}. Supported options are 'strings' and 'files'." end @string1, @string2 = string1, string2 end
Public Instance Methods
each() { |line| ... }
click to toggle source
# File lib/diffy/diff.rb, line 68 def each lines = case @options[:include_diff_info] when false then diff.split("\n").reject{|x| x =~ /^(---|\+\+\+|@@|\\\\)/ }.map {|line| line + "\n" } when true then diff.split("\n").map {|line| line + "\n" } end if block_given? lines.each{|line| yield line} else lines.to_enum end end
each_chunk() { |chunk| ... }
click to toggle source
# File lib/diffy/diff.rb, line 80 def each_chunk old_state = nil chunks = inject([]) do |cc, line| state = line.each_char.first if state == old_state cc.last << line else cc.push line.dup end old_state = state cc end if block_given? chunks.each{|chunk| yield chunk } else chunks.to_enum end end
tempfile(string)
click to toggle source
# File lib/diffy/diff.rb, line 100 def tempfile(string) t = Tempfile.new('diffy') # ensure tempfiles aren't unlinked when GC runs by maintaining a # reference to them. @tempfiles ||=[] @tempfiles.push(t) t.print(string) t.flush t.close t.path end
to_s(format = nil)
click to toggle source
# File lib/diffy/diff.rb, line 112 def to_s(format = nil) format ||= self.class.default_format formats = Format.instance_methods(false).map{|x| x.to_s} if formats.include? format.to_s enum = self enum.extend Format enum.send format else raise ArgumentError, "Format #{format.inspect} not found in #{formats.inspect}" end end
Private Instance Methods
diff_bin()
click to toggle source
# File lib/diffy/diff.rb, line 127 def diff_bin return @@bin if @@bin @@bin ||= "" if WINDOWS @@bin = `which diff.exe`.chomp if @@bin.empty? end @@bin = `which diff`.chomp if @@bin.empty? if @@bin.empty? raise "Can't find a diff executable in PATH #{ENV['PATH']}" end @@bin end
diff_options()
click to toggle source
options pass to diff program
# File lib/diffy/diff.rb, line 143 def diff_options Array(options[:context] ? "-U #{options[:context]}" : options[:diff]) end