Class: WCC::Differ

Inherits:
Object
  • Object
show all
Defined in:
lib/wcc/diff.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dstring) ⇒ Differ

Returns a new instance of Differ.



102
103
104
105
# File 'lib/wcc/diff.rb', line 102

def initialize(dstring)
	@di = dstring.lines.map { |line| DiffItem.new(line) }
	compute_hilite
end

Instance Attribute Details

#diObject (readonly)

Returns the value of attribute di.



100
101
102
# File 'lib/wcc/diff.rb', line 100

def di
  @di
end

Instance Method Details

#compute_hiliteObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/wcc/diff.rb', line 107

def compute_hilite
	# get representional string for the whole diff
	s = rchar
	#puts s
	mds = []
	md = s.match(/(@|_)di(@|_)/)
	while not md.nil?
		mds << md
		s = s.substring(md.begin(2))
		md = s.match(/(@|_)di(@|_)/)
	end
	
	offset = 0
	mds.each do |md|
		i = offset+md.begin(1)+1
		offset = md.begin(2)+1
		# found a single insertion/deletion pair
		InLineDiffer.new(@di[i], @di[i+1]).compute_hilite
	end
end

#ncharscObject



144
145
146
# File 'lib/wcc/diff.rb', line 144

def ncharsc
	@di.inject(0) { |sum,o| sum += (o.hilite.nil? ? 0 : o.hilite.select { |e| not e.nil? }.size) }
end

#ndeletionsObject



136
137
138
# File 'lib/wcc/diff.rb', line 136

def ndeletions
	@di.inject(0) { |sum,o| sum += (o.status == :del ? 1 : 0) }
end

#nhunksObject



128
129
130
# File 'lib/wcc/diff.rb', line 128

def nhunks
	@di.inject(0) { |sum,o| sum += (o.status == :range ? 1 : 0) }
end

#ninsertionsObject



132
133
134
# File 'lib/wcc/diff.rb', line 132

def ninsertions
	@di.inject(0) { |sum,o| sum += (o.status == :ins ? 1 : 0) }
end

#nlinescObject



140
141
142
# File 'lib/wcc/diff.rb', line 140

def nlinesc
	ninsertions + ndeletions
end

#rcharObject



148
149
150
# File 'lib/wcc/diff.rb', line 148

def rchar
	@di.map { |o| o.rchar }.join
end

#to_sObject



152
153
154
# File 'lib/wcc/diff.rb', line 152

def to_s
	@di.map { |o| o.to_s }.join("\n")
end