Class: CcManager::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/cc_manager/cli.rb

Instance Method Summary collapse

Instance Method Details

#processObject



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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cc_manager/cli.rb', line 22

def process
  file = options['file']
  @accounts, @errors = [], []
  counter = 1
  if File.exist?(file)
    File.open(file, "r") do |infile|
      while (line = infile.gets)
        args = line.split(" ")
        action = args.first.downcase
        case(action)
        when "add"  # Adds new account if credit card is valid and all the arguments are valid
          add(args, line, counter)
        when "charge", "credit" # Updates the account 
          update(action, args, line, counter)
        else
          add_errors(counter, line, "Invalid Action Specified!!!")
        end
        counter = counter + 1
      end
    end
  else
    say("Invalid File location. Please Enter Valid path of File!!!", :red)
  end

  # Displaying final output
  say("\n\nOUTPUT: \n\n", :yellow)
  @accounts.sort! { |a,b| a.name <=> b.name }
  @accounts.each do |a|
    if a.valid?
      say("#{a.name}: #{a.balance}", :green)
    else
      say("#{a.name}: Error", :red)
    end
  end
  
  if options['errors']
    # Displaying Errors in tabular form
    display_errors
  end
end

#versionObject



15
16
17
# File 'lib/cc_manager/cli.rb', line 15

def version
  say("Version: #{CcManager::VERSION}", :green)
end