Module: CSVR::Parse

Included in:
App
Defined in:
lib/csvr/parse.rb

Class Method Summary collapse

Class Method Details

.filter(hash, filters) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/csvr/parse.rb', line 63

def filter(hash, filters)
	hash.each do |key, value|
		filters.each do |filter|
			if filter.class == String
				match = value.match(filter) if value.respond_to? :match
				return hash if match
			elsif filter.class == Fixnum || filter.class == Float
				return hash if hash.value?(filter)
			end
		end
	end
	return nil
end

.headers(file) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/csvr/parse.rb', line 9

def headers(file)

	File.open(file) do |fi|
		1.times do
			return CSV.parse_line(fi.readline)
		end
	end
end

.index(file, columns) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/csvr/parse.rb', line 18

def index(file, columns)

	array = headers(file)
	hash = {}
	array.each{|value| hash[array.index(value)] = value if columns.include?(value) }

	return hash
end

.rows(file, headers, filters = nil) ⇒ Object



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
# File 'lib/csvr/parse.rb', line 27

def rows(file, headers, filters = nil)

	index = index(file, headers)

	File.open(file) do |fi|

		#Skip the first line
		lines = fi.each_line
		lines.next

		values = []
		counter = 0

		lines.each do |line|

			counter += 1
			puts "Parsing #{counter}"

			row = CSV.parse_line(line, :converters => :all)

			hash = {}

			#Remove ' punctation - this causes problems with the "INSERT INTO lines"
			index.each do |k, v|
				row[k].gsub!(/[']/, '') if row[k].respond_to? :gsub
				hash[v] = row[k]
			end

			filters ? values << filter(hash, filters) : values << hash

		end

		return values.compact
	end
end

.type(headers, row) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/csvr/parse.rb', line 77

def type(headers, row)
	types = []
	headers.each do |header|
		hash = {}
		row.each do |k, v|
			hash[k] = v.class if header.include?(k)
			types << hash
		end
	end
	#Remove duplicates and merge array of hashes into single hash
	return types.uniq.inject(:merge)
end