Class: HashCompare

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

Overview

Class to compare two different hashes and to report similarities and differences

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(old_hash = nil, new_hash = nil) ⇒ HashCompare

The new method takes two hashes as arguments for comparison



12
13
14
# File 'lib/hash_compare.rb', line 12

def initialize old_hash = nil, new_hash = nil
	@old_hash, @new_hash = old_hash, new_hash
end

Instance Attribute Details

#new_hashObject

The current or new hash that needs to be compares with old hash



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

def new_hash
  @new_hash
end

#old_hashObject

The old hash or the previous hash



6
7
8
# File 'lib/hash_compare.rb', line 6

def old_hash
  @old_hash
end

Class Method Details

.csv_to_hash(file_name, split_string = ',', id_column_number = 0) ⇒ Object

This function converts an CSV to hash, the lines inis file are converted to values and key is determined by id_column_number

file_name , the name of the file that needs to scanned and converted to hash

split_string , the field seprator

id_column_number , for an hash you need to have a key, the key will be taken from the CSV file, it will be split and corresponding colum will be made as key



70
71
72
73
74
75
76
77
78
# File 'lib/hash_compare.rb', line 70

def self.csv_to_hash file_name, split_string = ',', id_column_number = 0
	f = File.open file_name, "r"
	hash = {}
	while line = f.gets
		hash.store(line.split(split_string)[id_column_number], line)
	end
	f.close
	hash
end

Instance Method Details

#changedObject

Gets stuff in new hash who’s value has been changed from the old hash



36
37
38
39
40
41
42
# File 'lib/hash_compare.rb', line 36

def changed
	hash = {}
	@new_hash.each do |k,v|
		hash.store(k,v) if @old_hash.key?(k) and @old_hash[k] != v
	end
	hash
end

#deletedObject

Gets stuff in old_hash thats not in new_hash



26
27
28
29
30
31
32
# File 'lib/hash_compare.rb', line 26

def deleted
	hash = {}
	@old_hash.each do |k,v|
		hash.store(k,v) unless @new_hash.key?(k)
	end
	hash
end

#from_csv(old_csv, new_csv, split_string = ',', id_column_number = 0) ⇒ Object

Takes two CSV’s, converts it to hash, then you can use methods in HashCompare class to check difference between them



82
83
84
85
# File 'lib/hash_compare.rb', line 82

def from_csv old_csv, new_csv, split_string = ',', id_column_number = 0
	@old_hash = HashCompare::csv_to_hash old_csv, split_string, id_column_number
	@new_hash = HashCompare::csv_to_hash new_csv, split_string, id_column_number
end

#newly_addedObject

Gets stuff in new_hash thats not in old_hash



17
18
19
20
21
22
23
# File 'lib/hash_compare.rb', line 17

def newly_added
	hash = {}
	@new_hash.each do |k,v|
		hash.store(k,v) unless @old_hash.key?(k)
	end
	hash
end

#same?Boolean

Returns true if both hashes are same

Returns:

  • (Boolean)


55
56
57
# File 'lib/hash_compare.rb', line 55

def same?
	@old_hash == @new_hash
end

#unchangedObject

Gets stuff in new hash who’s value has been unchanged from the old hash



46
47
48
49
50
51
52
# File 'lib/hash_compare.rb', line 46

def unchanged
	hash = {}
	@new_hash.each do |k,v|
		hash.store(k,v) if @old_hash.key?(k) and @old_hash[k] == v
	end
	hash
end