Class: Banco::Reader
Instance Attribute Summary collapse
-
#all_from_csv ⇒ Object
readonly
Returns the value of attribute all_from_csv.
-
#csv_file_name ⇒ Object
readonly
Returns the value of attribute csv_file_name.
-
#report_name ⇒ Object
readonly
Returns the value of attribute report_name.
Instance Method Summary collapse
-
#initialize(csv_file_name, report_name) ⇒ Reader
constructor
A new instance of Reader.
- #non_numeric_in_csv(exception, row_number) ⇒ Object
- #read_in_csv_data ⇒ Object
Methods included from Viewable
#bottom_line, #dashes, #date_range, #facts, farewell, hello, #menu, #money_in_summary, #money_out_summary, #print_summary, prompt, #save_summary_to_file, #save_transactions_to_file, #to_pounds, #transactions_all, #transactions_in, #transactions_out
Constructor Details
#initialize(csv_file_name, report_name) ⇒ Reader
Returns a new instance of Reader.
12 13 14 15 16 |
# File 'lib/banco/reader.rb', line 12 def initialize(csv_file_name, report_name) @csv_file_name = csv_file_name @report_name = report_name @all_from_csv = [] end |
Instance Attribute Details
#all_from_csv ⇒ Object (readonly)
Returns the value of attribute all_from_csv.
10 11 12 |
# File 'lib/banco/reader.rb', line 10 def all_from_csv @all_from_csv end |
#csv_file_name ⇒ Object (readonly)
Returns the value of attribute csv_file_name.
10 11 12 |
# File 'lib/banco/reader.rb', line 10 def csv_file_name @csv_file_name end |
#report_name ⇒ Object (readonly)
Returns the value of attribute report_name.
10 11 12 |
# File 'lib/banco/reader.rb', line 10 def report_name @report_name end |
Instance Method Details
#non_numeric_in_csv(exception, row_number) ⇒ Object
49 50 51 52 53 |
# File 'lib/banco/reader.rb', line 49 def non_numeric_in_csv(exception, row_number) reason = exception..split(':') puts "Please check row #{row_number} of #{@csv_file_name}," puts "#{reason.last} should be a numeric value".rjust(54) end |
#read_in_csv_data ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/banco/reader.rb', line 18 def read_in_csv_data @csv_row_count = 1 CSV.foreach(csv_file_name) do |row| all_from_csv << Transaction.new(row[0], row[1], row[2], row[3], row[4], @csv_row_count) @csv_row_count += 1 end rescue ArgumentError => e non_numeric_in_csv(e, @csv_row_count) puts "sorted ? ('y' to continue or 'q' to exit)".rjust(54) input = gets.chomp.downcase @all_from_csv = [] input == 'y' ? retry : Viewable.farewell rescue Errno::ENOENT => e puts "Can't find file #{@csv_file_name} - have another go or (q) quit\n\n" loop do input = gets.chomp.downcase case input when 'q' Viewable.farewell exit when /([^\s]+(\.csv)$)/ @csv_file_name = input break else puts "\ninvalid file - try again or 'q' to quit\n\n" end end retry end |