Class: Reckon::CSVParser
- Inherits:
-
Object
- Object
- Reckon::CSVParser
- Defined in:
- lib/reckon/csv_parser.rb
Overview
Parses CSV files
Instance Attribute Summary collapse
-
#csv_data ⇒ Object
Returns the value of attribute csv_data.
-
#date_column ⇒ Object
Returns the value of attribute date_column.
-
#date_column_index ⇒ Object
Returns the value of attribute date_column_index.
-
#description_column_indices ⇒ Object
Returns the value of attribute description_column_indices.
-
#money_column ⇒ Object
Returns the value of attribute money_column.
-
#money_column_indices ⇒ Object
Returns the value of attribute money_column_indices.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#columns ⇒ Object
transpose csv_data (array of rows) to an array of columns.
- #date_for(index) ⇒ Object
- #description_for(index) ⇒ Object
-
#initialize(options = {}) ⇒ CSVParser
constructor
A new instance of CSVParser.
- #money_for(index) ⇒ Object
- #pretty_date_for(index) ⇒ Object
- #pretty_money(amount, negate = false) ⇒ Object
- #pretty_money_for(index, negate = false) ⇒ Object
- #row(index) ⇒ Object
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( = {}) self. = self.[:csv_separator] = "\t" if [:csv_separator] == '\t' self.[:currency] ||= '$' # we convert to a string so we can do character encoding cleanup @csv_data = parse([:string] || File.read([:file]), [:file]) filter_csv detect_columns end |
Instance Attribute Details
#csv_data ⇒ Object
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_column ⇒ Object
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_index ⇒ Object
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_indices ⇒ Object
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_column ⇒ Object
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_indices ⇒ Object
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 |
#options ⇒ Object
Returns the value of attribute options.
8 9 10 |
# File 'lib/reckon/csv_parser.rb', line 8 def @options end |
Instance Method Details
#columns ⇒ Object
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 |