Class: Wutang::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/wutang/cli.rb

Constant Summary collapse

PUBLIC_COMMANDS =
[:create, :update, :search, :list]

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#argsObject (readonly)

Returns the value of attribute args.



4
5
6
# File 'lib/wutang/cli.rb', line 4

def args
  @args
end

#commandObject (readonly)

Returns the value of attribute command.



4
5
6
# File 'lib/wutang/cli.rb', line 4

def command
  @command
end

#wutangObject (readonly)

Returns the value of attribute wutang.



4
5
6
# File 'lib/wutang/cli.rb', line 4

def wutang
  @wutang
end

Instance Method Details

#attributesObject



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

#createObject



20
21
22
23
# File 'lib/wutang/cli.rb', line 20

def create
  entry = wutang.create attributes
  puts "Created #{entry}"
end

#handleObject



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

#listObject



39
40
41
# File 'lib/wutang/cli.rb', line 39

def list
  puts wutang.entries
end

#searchObject



34
35
36
37
# File 'lib/wutang/cli.rb', line 34

def search
  criteria = args.shift
  puts wutang.search criteria
end

#updateObject



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