-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_data.rb
167 lines (159 loc) · 3.52 KB
/
custom_data.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
require 'csv'
# Note: these set of classes are for reading in CSV such as below with the 4 columns:
# * First
# * Last
# * Age
# * Occupation
#
# Example:
# First,Last,Age,Occupation
# John,Smith,60,Engineer
# David,Smith,20,Doctor
class Key
attr_accessor :first, :last
def initialize(first, last)
@first = first
@last = last
end
def eql?(other)
return false if self.first != other.first
return false if self.last != other.last
return true
end
def hash
[@first, @last].hash
end
def to_s
"first: #{@first}, last: #{@last}"
end
end
class Value
attr_accessor :age, :occupation
def initialize(age, occupation)
@age = age
@occupation = occupation
end
def eql?(other)
return false if self.age != other.age
return false if self.occupation != other.occupation
return true
end
def hash
[@age, @occupation].hash
end
def to_s
"age: #{@age}, occupation: #{@occupation}"
end
end
class CustomCompareResult
attr_accessor :num_records_same
def initialize(old_data, new_data)
@old_data = old_data
@new_data = new_data
compare_num_records
end
def compare_num_records
if @old_data.size == @new_data.size
@num_records_same = true
else
@num_records_same = false
end
end
def new_records
result = Array.new
puts "old_data: #{@old_data}" if $DEBUG
@new_data.each do |new_item|
puts "old_data.find(#{new_item.key})" if $DEBUG
old_item = @old_data.find_by_key(new_item.key)
puts "old_item: #{old_item}" if $DEBUG
unless old_item
result << new_item
end
end
return result
end
def updated_records
result = Array.new
puts "old_data: #{@old_data}" if $DEBUG
@old_data.each do |old_item|
puts "new_data.find(#{old_item.key})" if $DEBUG
new_item = @new_data.find_by_key(old_item.key)
puts "new_item: #{new_item}" if $DEBUG
if new_item
unless new_item.value.eql?(old_item.value)
result << new_item
end
end
end
return result
end
def deleted_records
result = Array.new
puts "old_data: #{@old_data}" if $DEBUG
@old_data.each do |old_item|
puts "new_data.find(#{old_item.key})" if $DEBUG
new_item = @new_data.find_by_key(old_item.key)
puts "new_item: #{new_item}" if $DEBUG
unless new_item
result << old_item
end
end
return result
end
end
class CustomData
attr_accessor :key, :value
def initialize(first, last, age, occupation)
@key = Key.new(first, last)
@value = Value.new(age, occupation)
end
def to_s
"#{@key} => value: #{@value}"
end
def self.to_data(row)
first = row['First']
last = row['Last']
age = row['Age']
occupation = row['Occupation']
data = CustomData.new(first, last, age, occupation)
end
def self.to_list(file_table)
list = CustomDataList.new
file_table.each do |row|
data = to_data(row)
list.add(data)
end
return list
end
def self.compare(old_data, new_data)
result = CustomCompareResult.new(old_data, new_data)
return result
end
end
class CustomDataList
include Enumerable
def initialize
@hash = Hash.new
@hash_indexed = Hash.new
@index = 0
end
def find_by_key(key)
@hash[key]
end
def add(data)
@hash[data.key] = data
@hash_indexed[@index] = data
@index = @index + 1
end
def each
@hash.each do |key,value|
yield value
end
end
def size
@hash.size
end
def [](index)
@hash_indexed[index]
end
end