Class: AnixeCsv::Reader
- Inherits:
-
Object
- Object
- AnixeCsv::Reader
- 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
Class Method Summary collapse
- .open(path) ⇒ Object
-
.parse_fields(header_line, options = Hash.new) ⇒ Object
parses a header_line and returns a hash of fields and their index.
-
.read(path, options = Hash.new, &block) ⇒ Object
Class Methods Yields a oww to the block provided.
- .split_line(line, options = {:col_sep => ','}) ⇒ Object
Instance Method Summary collapse
- #close ⇒ Object
-
#each_line(options, &block) ⇒ Object
reads all lines from the open file, and yields block for each file.
-
#initialize ⇒ Reader
constructor
Instance Methods.
- #open(path) ⇒ Object
-
#read_fields(options = Hash.new) ⇒ Object
reads the first line of a csv file and returns a collection with its field names.
Constructor Details
#initialize ⇒ Reader
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, =Hash.new) header = [:split] ? Reader.split_line(header_line, ) : CSV.parse_line(header_line, ) 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
16 17 18 19 20 21 22 |
# File 'lib/anixe_csv/reader.rb', line 16 def self.read(path, =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(, &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, ={:col_sep => ','}) line.split([:col_sep]).collect { |field| field.empty? ? nil : field } end |
Instance Method Details
#close ⇒ Object
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(, &block) fields = read_fields() 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 = [:split] ? Reader.split_line(valid_line, ) : CSV.parse_line(valid_line, ) yield(Row.new(row, fields)) rescue CSV::MalformedCSVError raise CSV::MalformedCSVError, "#{$!.to_s.gsub(/\w+.$/, line_no.to_s)}" if [: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(=Hash.new) header_line = [:header_line] || @file.readline.chomp Reader.parse_fields(header_line, ) end |