Class: Banco::Reader

Inherits:
Object
  • Object
show all
Includes:
Viewable
Defined in:
lib/banco/reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_csvObject (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_nameObject (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_nameObject (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.message.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_dataObject



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