Class: AnixeCsv::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/anixe_csv/reader.rb

Overview

Reads a csv file using the ruby csv class Arguments:

path - full path to file
options - see FasterCsv class

Direct Known Subclasses

GzipReader

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReader

Instance Methods



48
49
50
# File 'lib/anixe_csv/reader.rb', line 48

def initialize
	@ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
end

Class Method Details

.open(path) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/anixe_csv/reader.rb', line 24

def self.open(path)
	reader = self.new
	reader.open(path)

	if block_given?
		yield reader
		reader.close
	else
		reader
	end
end

.parse_fields(header_line, options = Hash.new) ⇒ Object

parses a header_line and returns a hash of fields and their index



37
38
39
40
# File 'lib/anixe_csv/reader.rb', line 37

def self.parse_fields(header_line, options=Hash.new)
	header = options[:split] ? Reader.split_line(header_line, options) : CSV.parse_line(header_line, options)
	Hash[ header.collect {|v| [v.delete("\"").to_sym, header.index(v)] } ]
end

.read(path, options = Hash.new, &block) ⇒ Object

Class Methods Yields a oww to the block provided

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
# File 'lib/anixe_csv/reader.rb', line 16

def self.read(path, options=Hash.new, &block)
	raise ArgumentError, "Must provide a block to iterate over csv file" unless block_given?

	self.open(path) do |reader|
		reader.each_line(options, &block)
	end
end

.split_line(line, options = {:col_sep => ','}) ⇒ Object



42
43
44
# File 'lib/anixe_csv/reader.rb', line 42

def self.split_line(line, options={:col_sep => ','})
	line.split(options[:col_sep]).collect { |field| field.empty? ? nil : field }
end

Instance Method Details

#closeObject



56
57
58
# File 'lib/anixe_csv/reader.rb', line 56

def close
	@file.close unless @file.nil?
end

#each_line(options, &block) ⇒ Object

reads all lines from the open file, and yields block for each file



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/anixe_csv/reader.rb', line 61

def each_line(options, &block)

	fields = read_fields(options)

	line_no = 0
	@file.each_line do |line|
		begin
			line_no += 1
			valid_line = @ic.iconv(line.chomp + ' ')[0..-2]    # convert to valid utf-8
			row = options[:split] ? Reader.split_line(valid_line, options) : CSV.parse_line(valid_line, options)
			yield(Row.new(row, fields))
		rescue CSV::MalformedCSVError
			raise CSV::MalformedCSVError, "#{$!.to_s.gsub(/\w+.$/, line_no.to_s)}" if options[:ignore_errors] == true
		end
	end
end

#open(path) ⇒ Object



52
53
54
# File 'lib/anixe_csv/reader.rb', line 52

def open(path)
	@file = File.open(path, "r")
end

#read_fields(options = Hash.new) ⇒ Object

reads the first line of a csv file and returns a collection with its field names



79
80
81
82
# File 'lib/anixe_csv/reader.rb', line 79

def read_fields(options=Hash.new)
	header_line = options[:header_line] || @file.readline.chomp
	Reader.parse_fields(header_line, options)
end