Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrubycsv.rb
More file actions
Latest commit
executable file
·104 lines (83 loc) · 2.49 KB
/
Copy pathrubycsv.rb
File metadata and controls
executable file
·104 lines (83 loc) · 2.49 KB
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
#!/usr/bin/env ruby
require'csv'
require'erb'
require'date'
# globals
$template_file =ARGV[0]
$csv_filename =ARGV[1]
$template_text =""
$header_row =[]
# evaluate the template in order to get the templates and other variables out
eval(File.read($template_file))
$template_text = $erb_template
# class to bind hash into erb template
classCSVRow
attr_accessor:csvrow
definitialize(row)
@csvrow=Hash.new
index=0
$header_row.size.timesdo
@csvrow[$header_row[index]]=row[index]
index +=1;
end
end
defget_binding()
returnbinding()
end
end
# helper functions
defclean_date(text_date,date_format="%Y-%m-%d")# reformat date in to big endian format
returnDate.strptime(text_date,date_format).strftime("%Y-%m-%d")
end
defclean_money(text_money,positive=FALSE)# nuke all but digits, negation, decimal place
iftext_money.nil?
return""
end
ifpositive
returntext_money.gsub(/[^\d\.]/,'')
else
returntext_money.gsub(/[^-\d\.]/,'')
end
end
defclean_num(text_num)# nuke all but digits, negation
iftext_num.nil?
return""
end
returntext_num.gsub(/[^-\d]/,'')
end
defclean_text(text)# get rid of anything that's not a word or space, make multiple spaces single space
iftext.nil?
return""
end
returntext.gsub(/[^\w\]\[\@\.\,\'\"]/,'').gsub(/[ ]+/,' ').strip;
end
# lookup values in arrays. Table is a 2D array,
# where the first value is the string returned,
# and the following are substrings to attempt to match against the value
deftablematch(table,value)
default=""
table.eachdo |row|
rowfirst=row.first
row.slice(1,row.length).eachdo |item|
ifitem == "DEFAULT"# set the default value
default=rowfirst
elsifvalue.match(/^#{item}/i)
#print " val: #{value} matches #{item}, returning #{rowfirst}\n"
returnrowfirst
end
end
end
returndefault
end
# parse CSV file
csv=CSV.open($csv_filename,'r')
csv.each()do |row|
if(csv.lineno == 1)#this is the header row, store to assign as keys on later rows, stripping whitespace
$header_row =row.collect{|val| val.to_s.strip}
else
thisrow=CSVRow.new(row)
#print "line: #{thisrow.csvrow.inspect}\n"
erbrender=ERB.new($erb_template,nil,'<>')
putserbrender.result(thisrow.get_binding)
end
end