Class: Wutang::Cli
- Inherits:
-
Object
- Object
- Wutang::Cli
- Defined in:
- lib/wutang/cli.rb
Constant Summary collapse
- PUBLIC_COMMANDS =
[:create, :update, :search, :list]
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#wutang ⇒ Object
readonly
Returns the value of attribute wutang.
Instance Method Summary collapse
- #attributes ⇒ Object
- #create ⇒ Object
- #handle ⇒ Object
-
#initialize(wutang) ⇒ Cli
constructor
A new instance of Cli.
- #list ⇒ Object
- #search ⇒ Object
- #update ⇒ Object
Constructor Details
#initialize(wutang) ⇒ Cli
Returns a new instance of Cli.
6 7 8 9 10 |
# File 'lib/wutang/cli.rb', line 6 def initialize(wutang) @command = ARGV.shift.to_s.to_sym @args = ARGV @wutang = wutang end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
4 5 6 |
# File 'lib/wutang/cli.rb', line 4 def args @args end |
#command ⇒ Object (readonly)
Returns the value of attribute command.
4 5 6 |
# File 'lib/wutang/cli.rb', line 4 def command @command end |
#wutang ⇒ Object (readonly)
Returns the value of attribute wutang.
4 5 6 |
# File 'lib/wutang/cli.rb', line 4 def wutang @wutang end |
Instance Method Details
#attributes ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/wutang/cli.rb', line 43 def attributes {}.tap do|hash| args.each do |arg| key, value = arg.split(':') hash[key.to_sym] = value end end end |
#create ⇒ Object
20 21 22 23 |
# File 'lib/wutang/cli.rb', line 20 def create entry = wutang.create attributes puts "Created #{entry}" end |
#handle ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/wutang/cli.rb', line 12 def handle if PUBLIC_COMMANDS.include?(command) send(command) else puts "Unknown command #{command}" end end |
#list ⇒ Object
39 40 41 |
# File 'lib/wutang/cli.rb', line 39 def list puts wutang.entries end |
#search ⇒ Object
34 35 36 37 |
# File 'lib/wutang/cli.rb', line 34 def search criteria = args.shift puts wutang.search criteria end |
#update ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/wutang/cli.rb', line 25 def update if entry = wutang.find(args.shift) wutang.update entry, attributes puts "Entry updated with the following changes: #{attributes}" else puts "Entry not found" end end |