hash_compare 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/hash_compare.rb +90 -0
  2. metadata +45 -0
@@ -0,0 +1,90 @@
1
+ # Class to compare two different hashes and to report similarities and
2
+ # differences
3
+ class HashCompare
4
+
5
+ # The old hash or the previous hash
6
+ attr_accessor :old_hash
7
+
8
+ # The current or new hash that needs to be compares with old hash
9
+ attr_accessor :new_hash
10
+
11
+ # The new method takes two hashes as arguments for comparison
12
+ def initialize old_hash = nil, new_hash = nil
13
+ @old_hash, @new_hash = old_hash, new_hash
14
+ end
15
+
16
+ # Gets stuff in new_hash thats not in old_hash
17
+ def newly_added
18
+ hash = {}
19
+ @new_hash.each do |k,v|
20
+ hash.store(k,v) unless @old_hash.key?(k)
21
+ end
22
+ hash
23
+ end
24
+
25
+ # Gets stuff in old_hash thats not in new_hash
26
+ def deleted
27
+ hash = {}
28
+ @old_hash.each do |k,v|
29
+ hash.store(k,v) unless @new_hash.key?(k)
30
+ end
31
+ hash
32
+ end
33
+
34
+ # Gets stuff in new hash who's value has been changed from the
35
+ # old hash
36
+ def changed
37
+ hash = {}
38
+ @new_hash.each do |k,v|
39
+ hash.store(k,v) if @old_hash.key?(k) and @old_hash[k] != v
40
+ end
41
+ hash
42
+ end
43
+
44
+ # Gets stuff in new hash who's value has been unchanged from the
45
+ # old hash
46
+ def unchanged
47
+ hash = {}
48
+ @new_hash.each do |k,v|
49
+ hash.store(k,v) if @old_hash.key?(k) and @old_hash[k] == v
50
+ end
51
+ hash
52
+ end
53
+
54
+ # Returns true if both hashes are same
55
+ def same?
56
+ @old_hash == @new_hash
57
+ end
58
+
59
+ # This function converts an CSV to hash, the lines inis file are
60
+ # converted to values and key is determined by id_column_number
61
+ #
62
+ # file_name , the name of the file that needs to scanned and converted
63
+ # to hash
64
+ #
65
+ # split_string , the field seprator
66
+ #
67
+ # id_column_number , for an hash you need to have a key, the key will be
68
+ # taken from the CSV file, it will be split and corresponding colum will
69
+ # be made as key
70
+ def self.csv_to_hash file_name, split_string = ',', id_column_number = 0
71
+ f = File.open file_name, "r"
72
+ hash = {}
73
+ while line = f.gets
74
+ hash.store(line.split(split_string)[id_column_number], line)
75
+ end
76
+ f.close
77
+ hash
78
+ end
79
+
80
+ # Takes two CSV's, converts it to hash, then you can use methods in
81
+ # HashCompare class to check difference between them
82
+ def from_csv old_csv, new_csv, split_string = ',', id_column_number = 0
83
+ @old_hash = HashCompare::csv_to_hash old_csv, split_string, id_column_number
84
+ @new_hash = HashCompare::csv_to_hash new_csv, split_string, id_column_number
85
+ end
86
+ end
87
+
88
+
89
+
90
+
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash_compare
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Karthikeyan A K
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-29 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: A gem to compare Hashes
15
+ email: mindaslab@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/hash_compare.rb
21
+ homepage: http://github.com/mindaslab/hash_compare
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.10
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: Hash Compare
45
+ test_files: []