Class: HAL9000::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/hal9000/command.rb

Class Method Summary collapse

Class Method Details

.create(thought, action) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/hal9000/command.rb', line 39

def create(thought, action)
  if manager.create_action(thought, action)
    puts "hal - done! i will keep this in mind for you."
  else
    puts "hal - clash of thoughts! you already have this thought."
  end
end

.delete(thought) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/hal9000/command.rb', line 47

def delete(thought)
  if manager.delete_action(thought)
    puts "hal - gone! keep me posted if you think on this again."
  else
    puts "hal - maybe you are confused, i don't have this thought."
  end
end

.execute(args) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hal9000/command.rb', line 11

def execute(args)
  case args.first when '--help', 'help', '-h', '--usage', nil
    help
  when '--version'
    version
  when '--list'
    list
  when '--create'
    thought, action = args[1], args[2]
    return help unless thought && action
    create(thought, action)
  when '--delete'
    thought = args[1]
    return help unless thought
    delete(thought)
  else
    thought = args.join(' ')
    return help unless thought
    execute_action(thought)
  end
end

.execute_action(thought) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/hal9000/command.rb', line 55

def execute_action(thought)
  action_item = manager.find_action(thought)
  unless action_item
    puts "you - #{thought}"
    puts "hal - i don't know what you mean. maybe this will help."
    help
  else
    begin
      puts "you - #{thought}"
      puts "hal - right way sir! running: '#{action_item.action}'"
      system(action_item.action)
    rescue Exception => e 
      puts "hal - oops! i can't perform this action. here, maybe this will help."
      puts e
    end
  end
end

.helpObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hal9000/command.rb', line 73

def help
  help_text = <<-EOS
    example usage:
      hal --list                          lists all actions
      hal --create <thought> <action>     create a new action
      hal --delete <thought>              delete an action

      hal <thought>                       execute an action

      hal --version                       show hal version
      hal --help                          show this shiny help
    EOS
  puts help_text.gsub(/^ {10}/, '')
end

.listObject



33
34
35
36
37
# File 'lib/hal9000/command.rb', line 33

def list
  puts "hal - here are the thoughts that we've shared."
  puts "----------------------------------------------"
  manager.list_actions
end

.managerObject



7
8
9
# File 'lib/hal9000/command.rb', line 7

def manager 
  HAL9000.manager
end

.versionObject



88
89
90
# File 'lib/hal9000/command.rb', line 88

def version
  puts "hal - i'm currently running the version #{HAL9000::VERSION}"
end