-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact_application.rb
45 lines (41 loc) · 1.54 KB
/
contact_application.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
require_relative 'contact_commands'
class ContactApplication
def initialize(arguments)
@arguments = arguments
end
def run
recognized_commands = { :new => CreateNewContactCommand.command_description,
:update => UpdateContactCommand.command_description,
:remove => DeleteContactCommand.command_description,
:list => ListAllContactsCommand.command_description,
:show => ShowContactCommand.command_description,
:find => FindContactsCommand.command_description,
:help => ShowHelpCommand.command_description }
# Look at the arguments passed into this application and then
# take the appropriate action
case @arguments.shift.to_sym
when :new
CreateNewContactCommand.new(*@arguments).run
when :update
UpdateContactCommand.new(*@arguments).run
when :remove
DeleteContactCommand.new(*@arguments).run
when :list
ListAllContactsCommand.new(*@arguments).run
when :show
ShowContactCommand.new(*@arguments).run
when :find
FindContactsCommand.new(*@arguments).run
when :help
ShowHelpCommand.new(recognized_commands).run
else
raise(ArgumentError, "Invalid argument not recognized")
end
rescue ArgumentError => error
# Show a help message if arguments are malformed
puts "\n#{error.message}"
puts error.backtrace.inspect
puts "\n"
ShowHelpCommand.new(recognized_commands).run
end
end