Class: Kiq::CLI::Run
- Inherits:
-
Object
- Object
- Kiq::CLI::Run
- Defined in:
- lib/kiq/run.rb
Overview
Runs the program when the user types ‘kiq’
Instance Method Summary collapse
-
#add(project) ⇒ Object
Returns successful message if project added Returns help if add is unsuccessful.
-
#back(type, backer_name, project_name, credit_card, amount) ⇒ String
Successful message if user input is valid, help if back is unsuccessful.
-
#backer(backer_name) ⇒ String
Lists projects backed by a certain backer.
-
#handle(input) ⇒ Object
Handles valid input Runs relevant method for command Returns help if user input indicates help is needed.
-
#initialize ⇒ Run
constructor
Initialize the Run with a projects hash and load the file.
-
#list(project_name) ⇒ String
If Project instance exists, lists all backers for a certain project.
-
#perform ⇒ String
Uses user input.
Constructor Details
#initialize ⇒ Run
Initialize the Run with a projects hash and load the file
13 14 15 16 17 |
# File 'lib/kiq/run.rb', line 13 def initialize @projects = {} load_file load_projects end |
Instance Method Details
#add(project) ⇒ Object
Returns successful message if project added Returns help if add is unsuccessful
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/kiq/run.rb', line 64 def add(project) type = project[0] name = project[1] amount = project[2] if Project.validate_project(project, @projects) @projects[name] = Project.new(name, amount) save puts "Added Project: #{name}!" else Display.command_help(type) end end |
#back(type, backer_name, project_name, credit_card, amount) ⇒ String
Returns successful message if user input is valid, help if back is unsuccessful.
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/kiq/run.rb', line 84 def back(type, backer_name, project_name, credit_card, amount) project = @projects[project_name] if Backer.validate_project_exists(project) && Backer.validate_card(project, credit_card) && Backer.validate_backer_name(backer_name) && Backer.check_amount_dollar_sign(amount) backer = Backer.new(backer_name, credit_card, amount) project.backers[credit_card] = backer update_goal(project, amount) save puts "#{backer.name.capitalize} backed #{project_name} for $#{backer.amount}." else Display.command_help(type) end end |
#backer(backer_name) ⇒ String
Lists projects backed by a certain backer
119 120 121 122 123 124 125 126 127 |
# File 'lib/kiq/run.rb', line 119 def backer(backer_name) @projects.each do |project| project[1].backers.each do |backer| if backer[1].name == backer_name puts "Backed #{project[1].name} for $#{backer[1].amount} dollars" end end end end |
#handle(input) ⇒ Object
Handles valid input Runs relevant method for command Returns help if user input indicates help is needed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/kiq/run.rb', line 44 def handle(input) if input[-1] == '--help' return Display.command_help(input[0]) end case input[0] when "project" add(input) when "back" back(input[0], input[1], input[2], input[3], input[4]) when "list" list(input[1]) when "backer" backer(input[1]) end end |
#list(project_name) ⇒ String
If Project instance exists, lists all backers for a certain project
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/kiq/run.rb', line 100 def list(project_name) if !Project.project_does_not_exist?(['project', project_name], @projects) project = Project.all_offspring.find { |p| p.name == project_name } puts "Project Name: #{project.name}" puts "Amount Remaining: $#{project.amount}" check_goal(project) puts "BACKERS:" project.backers.each do |backer| puts "Backer #{backer[1].name}, Amount: $#{backer[1].amount}" end else puts "ERROR: Project does not exist.\n\n" Display.help end end |
#perform ⇒ String
Uses user input
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/kiq/run.rb', line 21 def perform user_input = ARGV primary_command = user_input[0] valid_commands = ['project', 'list', 'back', 'backer'] case primary_command when *valid_commands handle(user_input) when '-v' puts Kiq::VERSION when nil Display.help when '--help' Display.help else puts "Unknown command: '#{user_input}'." unless user_input == '-h' Display.help end end |