Class: BTCompare::Comparison

Inherits:
Object
  • Object
show all
Defined in:
lib/btcompare/comparison.rb

Overview

Actor that runs a comparison on two different TorrentFiles.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tf1, tf2) ⇒ Comparison

Creates a new comparison

Parameters:

  • tf1 (TorrentFile, String)

    First file for comparison

  • tf2 (TorrentFile, String)

    Second file for comparison

Raises:

  • (RuntimeError)

    if argument class is wrong


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/btcompare/comparison.rb', line 20

def initialize tf1, tf2

	@file_1 = nil
	@file_2 = nil
	@result = nil


	case tf1
	when String
		@file_1 = TorrentFile.new tf1

	when TorrentFile
		@file_1 = tf1

	else
		raise "tf1 is wrong type of file"
	end

	
	case tf2
	when String
		@file_2 = TorrentFile.new tf2

	when TorrentFile
		@file_2 = tf2

	else
		raise "tf2 is wrong type of file"
	end
end

Instance Attribute Details

#file_1Object (readonly)

TorrentFile

File 1 of the comparison


9
10
11
# File 'lib/btcompare/comparison.rb', line 9

def file_1
  @file_1
end

#file_2Object (readonly)

TorrentFile

File 2 of the comparison


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

def file_2
  @file_2
end

Instance Method Details

#invalidate_cacheObject

Invalidates cached data


79
80
81
# File 'lib/btcompare/comparison.rb', line 79

def invalidate_cache
	@result = nil
end

#resultResult

Runs the comparison

Returns:

  • (Result)

    The result of the comparison


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/btcompare/comparison.rb', line 54

def result
	unless @result == nil then
		return @result
	end

	@result = Result.new self

	@result.same_piece_count = ( @file_1.piece_count == @file_2.piece_count )


	difference_ids = []
	shortest = [ @file_1.piece_count, @file_2.piece_count ].sort.first
	shortest.times do |i|
		unless @file_1.individual_pieces[i] == @file_2.individual_pieces[i] then
			difference_ids.push i
		end
	end
	@result.difference_ids = difference_ids

	@result.lock
	return @result
end