Class: KtCommon::CsvLoader

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

Overview

CsvLoader is used to parse CSV data. It generates symbols to use as hash keys based on the header row of data. This means that a header row is REQUIRED.

  • All loaded data can be retrieved from the .rows attribute.

  • .rows is an array of Hashes, each hash being data for one row,

indexed by the symbols mentioned above.

Example Rows Data Layout:

rows = Hash=> “row1Col1 Data”, :colHeaderItem2 => “row1Col2 Data” rows = Hash=> “row2Col1 Data”, :colHeaderItem2 => “row2Col2 Data” rows = Hash=> “row3Col1 Data”, :colHeaderItem2 => “row3Col2 Data”

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCsvLoader

CsvLoader constructor



44
45
46
47
48
# File 'lib/ktcommon/csvloader.rb', line 44

def initialize()
	$LOG.debug "KtCommon::CsvLoader::initialize"
	@verbose 	= false
	@rows = Array.new
end

Instance Attribute Details

#rowsObject (readonly)

Returns the value of attribute rows.



38
39
40
# File 'lib/ktcommon/csvloader.rb', line 38

def rows
  @rows
end

#verbose(arg) ⇒ Object (readonly)

Turn on verbose messaging.

arg

true to turn on verbose messaging



56
57
58
# File 'lib/ktcommon/csvloader.rb', line 56

def verbose
  @verbose
end

Instance Method Details

#createSymbolsFromHeader(hdr) ⇒ Object

Create a symbol to be used for hash keys (column) indexes for each row. The symbol is created by removing all whitespace. All capitalization will be kept as-is.

hdr

Header data row from loaded CSV file



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ktcommon/csvloader.rb', line 70

def createSymbolsFromHeader(hdr)
	i = 0
	@header = Array.new
	hdr.each do |colHdr|
		#puts "colHdr (before): " + colHdr
		val = colHdr.gsub(/\s/, "").to_s
		#puts "colHdr (after):  " + val
		raise "Unrecognized header value: #{colHdr} in #{__FILE__}:#{__LINE__}" if !colHdr.nil? && val.nil?
		unless(colHdr.nil? || colHdr.empty?)
			#@header[i] = colHdr.gsub!(/s/, "").to_s.intern if(!colHdr.nil? && !colHdr.empty?)
			@header[i] = val.intern
		end
		i += 1
	end # each colHdr
	
	#puts "header[#{i.to_s}]: #{@header[i]}"
	
	return true
end

#generateTemplate(filepath, hdr, rows = nil) ⇒ Object

Generate an example CSV file.

filepath

path and name of file to generate

hdr

array of text to create headers from

rows

an array of arrays of row data ( [ [“row1Col1”, “row1Col2”, “row1Col3”], [“row2Col1”, “row2Col2”, “row2Col3”] ] )



147
148
149
150
151
152
153
154
155
156
# File 'lib/ktcommon/csvloader.rb', line 147

def generateTemplate(filepath, hdr, rows=nil)
	$LOG.debug "KtCommon::CsvLoader::generateTemplate( #{filepath}, hdr )"

	writer = CSV.open(filepath, "w")
	writer << hdr
	unless(rows.nil?)
		rows.each {|r| writer << r}
	end
	writer.close
end

#load(filepath) ⇒ Object

Load a CSV file and process it into an array of hashes.

filepath

path to file to process



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
# File 'lib/ktcommon/csvloader.rb', line 112

def load(filepath)
	$LOG.debug "KtCommon::CsvLoader::load( #{filepath} )"
	
	if(!File.exist?( filepath ))
		raise IOError.new("File not found: #{filepath}")
		return false
	end
	
	reader = CSV.open(filepath, "r")
	header = reader.shift
	
	if(@verbose)
		puts "CSV Column Headers:"
		header.each {|h| puts "\t#{h}" }
		puts
	end
	
	if(!createSymbolsFromHeader( header ))
		return false
	end
	
	reader.each do |row|
		process(row)
	end
	
	return true
end