Class: Diffy::Diff

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

Constant Summary collapse

ORIGINAL_DEFAULT_OPTIONS =
{
  :diff => '-U 10000',
  :source => 'strings',
  :include_diff_info => false,
  :include_plus_and_minus_in_html => false,
  :context => nil,
  :allow_empty_diff => true,
}

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.



35
36
37
38
39
40
41
# File 'lib/diffy/diff.rb', line 35

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



14
15
16
# File 'lib/diffy/diff.rb', line 14

def default_format
  @default_format || :text
end

.default_optionsObject



20
21
22
# File 'lib/diffy/diff.rb', line 20

def default_options
  @default_options ||= ORIGINAL_DEFAULT_OPTIONS.dup
end

Instance Attribute Details

#diffObject (readonly)

Returns the value of attribute diff.



26
27
28
# File 'lib/diffy/diff.rb', line 26

def diff
  @diff
end

#optionsObject (readonly)

Returns the value of attribute options.



26
27
28
# File 'lib/diffy/diff.rb', line 26

def options
  @options
end

#string1Object (readonly)

Returns the value of attribute string1.



26
27
28
# File 'lib/diffy/diff.rb', line 26

def string1
  @string1
end

#string2Object (readonly)

Returns the value of attribute string2.



26
27
28
# File 'lib/diffy/diff.rb', line 26

def string2
  @string2
end

Instance Method Details

#eachObject



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/diffy/diff.rb', line 83

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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/diffy/diff.rb', line 95

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



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/diffy/diff.rb', line 115

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



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/diffy/diff.rb', line 127

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