Class: Reckon::CSVParser

Inherits:
Object
  • Object
show all
Defined in:
lib/reckon/csv_parser.rb

Overview

Parses CSV files

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CSVParser

Returns a new instance of CSVParser.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/reckon/csv_parser.rb', line 11

def initialize(options = {})
  self.options = options

  self.options[:csv_separator] = "\t" if options[:csv_separator] == '\t'
  self.options[:currency] ||= '$'

  # we convert to a string so we can do character encoding cleanup
  @csv_data = parse(options[:string] || File.read(options[:file]), options[:file])
  filter_csv
  detect_columns
end

Instance Attribute Details

#csv_dataObject

Returns the value of attribute csv_data.



8
9
10
# File 'lib/reckon/csv_parser.rb', line 8

def csv_data
  @csv_data
end

#date_columnObject

Returns the value of attribute date_column.



8
9
10
# File 'lib/reckon/csv_parser.rb', line 8

def date_column
  @date_column
end

#date_column_indexObject

Returns the value of attribute date_column_index.



8
9
10
# File 'lib/reckon/csv_parser.rb', line 8

def date_column_index
  @date_column_index
end

#description_column_indicesObject

Returns the value of attribute description_column_indices.



8
9
10
# File 'lib/reckon/csv_parser.rb', line 8

def description_column_indices
  @description_column_indices
end

#money_columnObject

Returns the value of attribute money_column.



8
9
10
# File 'lib/reckon/csv_parser.rb', line 8

def money_column
  @money_column
end

#money_column_indicesObject

Returns the value of attribute money_column_indices.



8
9
10
# File 'lib/reckon/csv_parser.rb', line 8

def money_column_indices
  @money_column_indices
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/reckon/csv_parser.rb', line 8

def options
  @options
end

Instance Method Details

#columnsObject

transpose csv_data (array of rows) to an array of columns



24
25
26
# File 'lib/reckon/csv_parser.rb', line 24

def columns
  @columns ||= @csv_data[0].zip(*@csv_data[1..])
end

#date_for(index) ⇒ Object



28
29
30
# File 'lib/reckon/csv_parser.rb', line 28

def date_for(index)
  @date_column.for(index)
end

#description_for(index) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/reckon/csv_parser.rb', line 51

def description_for(index)
  description_column_indices.map { |i| columns[i][index].to_s.strip }
                            .reject(&:empty?)
                            .join("; ")
                            .squeeze(" ")
                            .gsub(/(;\s+){2,}/, '')
                            .strip
end

#money_for(index) ⇒ Object



36
37
38
# File 'lib/reckon/csv_parser.rb', line 36

def money_for(index)
  @money_column[index]
end

#pretty_date_for(index) ⇒ Object



32
33
34
# File 'lib/reckon/csv_parser.rb', line 32

def pretty_date_for(index)
  @date_column.pretty_for(index)
end

#pretty_money(amount, negate = false) ⇒ Object



40
41
42
# File 'lib/reckon/csv_parser.rb', line 40

def pretty_money(amount, negate = false)
  Money.new(amount, @options).pretty(negate)
end

#pretty_money_for(index, negate = false) ⇒ Object



44
45
46
47
48
49
# File 'lib/reckon/csv_parser.rb', line 44

def pretty_money_for(index, negate = false)
  money = money_for(index)
  return 0 if money.nil?

  money.pretty(negate)
end

#row(index) ⇒ Object



60
61
62
# File 'lib/reckon/csv_parser.rb', line 60

def row(index)
  csv_data[index].join(", ")
end