Class: Diffy::Diff

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string1, string2, options = {}) ⇒ Diff

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.



32
33
34
35
36
37
38
# File 'lib/diffy/diff.rb', line 32

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

Class Attribute Details

.default_formatObject



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

def default_format
  @default_format || :text
end

.default_optionsObject

default options passed to new Diff objects



11
12
13
14
15
16
17
18
19
# 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
  }
end

Instance Attribute Details

#diffObject (readonly)

Returns the value of attribute diff.



23
24
25
# File 'lib/diffy/diff.rb', line 23

def diff
  @diff
end

#optionsObject (readonly)

Returns the value of attribute options.



23
24
25
# File 'lib/diffy/diff.rb', line 23

def options
  @options
end

#string1Object (readonly)

Returns the value of attribute string1.



23
24
25
# File 'lib/diffy/diff.rb', line 23

def string1
  @string1
end

#string2Object (readonly)

Returns the value of attribute string2.



23
24
25
# File 'lib/diffy/diff.rb', line 23

def string2
  @string2
end

Instance Method Details

#eachObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/diffy/diff.rb', line 67

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_chunkObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/diffy/diff.rb', line 79

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) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/diffy/diff.rb', line 99

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) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/diffy/diff.rb', line 111

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