-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_class_creator.rb
47 lines (43 loc) · 1.01 KB
/
csv_class_creator.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
class CsvClassCreator
def initialize(classname)
@klass = Class.new do
@array_object = []
class << self
attr_accessor :array_object
end
attr_accessor :store_values
define_method :initialize do
self.store_values = {}
self.class.array_object << self
end
end
Object.const_set(classname, @klass)
end
def create_read_values_method
@klass.class_eval do
define_method :read_values_from_csv do |*row|
row.each do |key,value|
store_values[key] = value
end
end
end
end
def create_method(data_array)
data_array.headers.each do |method_name|
@klass.class_eval do
define_method(method_name) { store_values[method_name] }
end
end
end
def array_object
@klass.array_object
end
def create_method_object(data_array)
create_read_values_method
data_array.each do |row|
obj = @klass.new
obj.read_values_from_csv(*row)
end
create_method(data_array)
end
end