Class: Reckon::CSVParser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CSVParser

Returns a new instance of CSVParser.



7
8
9
10
11
12
13
# File 'lib/reckon/csv_parser.rb', line 7

def initialize(options = {})
  self.options = options
  self.options[:currency] ||= '$'
  @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.



5
6
7
# File 'lib/reckon/csv_parser.rb', line 5

def csv_data
  @csv_data
end

#date_columnObject

Returns the value of attribute date_column.



5
6
7
# File 'lib/reckon/csv_parser.rb', line 5

def date_column
  @date_column
end

#date_column_indexObject

Returns the value of attribute date_column_index.



5
6
7
# File 'lib/reckon/csv_parser.rb', line 5

def date_column_index
  @date_column_index
end

#description_column_indicesObject

Returns the value of attribute description_column_indices.



5
6
7
# File 'lib/reckon/csv_parser.rb', line 5

def description_column_indices
  @description_column_indices
end

#money_columnObject

Returns the value of attribute money_column.



5
6
7
# File 'lib/reckon/csv_parser.rb', line 5

def money_column
  @money_column
end

#money_column_indicesObject

Returns the value of attribute money_column_indices.



5
6
7
# File 'lib/reckon/csv_parser.rb', line 5

def money_column_indices
  @money_column_indices
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/reckon/csv_parser.rb', line 5

def options
  @options
end

Instance Method Details

#columnsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/reckon/csv_parser.rb', line 15

def columns
  @columns ||=
    begin
      last_row_length = nil
      csv_data.inject([]) do |memo, row|
        unless row.all? { |i| i.nil? || i.length == 0 }
          row.each_with_index do |entry, index|
            memo[index] ||= []
            memo[index] << (entry || '').strip
          end
          last_row_length = row.length
        end
        memo
      end
    end
end

#date_for(index) ⇒ Object



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

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

#description_for(index) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/reckon/csv_parser.rb', line 55

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



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

def money_for(index)
  @money_column[index]
end

#pretty_date_for(index) ⇒ Object



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

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

#pretty_money(amount, negate = false) ⇒ Object



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

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

#pretty_money_for(index, negate = false) ⇒ Object



48
49
50
51
52
53
# File 'lib/reckon/csv_parser.rb', line 48

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

  money.pretty(negate)
end

#row(index) ⇒ Object



64
65
66
# File 'lib/reckon/csv_parser.rb', line 64

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