Class: Droid

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

Constant Summary collapse

FILE =
'droid.yml'
COMMANDS =
"commands"
SYNTAX =
"syntax"
DESC =
"desc"
RUN =
"run"

Instance Method Summary collapse

Instance Method Details

#handle_command(command) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/droid.rb', line 33

def handle_command(command)
  commands = load[COMMANDS]
  command = commands[command]
  if !command
    list_commands
    abort()
  end

  system("#{command[RUN]} #{ARGV[1..ARGV.count].join(' ')}")
end

#list_commandsObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/droid.rb', line 21

def list_commands
  loaded_file = load
  abort("No commands found in droid.yml") unless loaded_file

  commands = loaded_file[COMMANDS]
  puts "Usage: #{"droid".green} #{"<command>".yellow} #{"[args...] [options...]".blue}"
  commands.each do |command|
    puts "\n#{"droid".green} #{command[0].green} #{command[1][SYNTAX] ? command[1][SYNTAX] : ""}"
    puts "  #{command[1][DESC]}"
  end
end

#loadObject



13
14
15
# File 'lib/droid.rb', line 13

def load
  YAML.load_file(FILE)
end

#runObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/droid.rb', line 44

def run
  File.file?('./droid.yml')
  if !File.file?('./droid.yml')
      abort('Could not find droid.yml')
  end
  # Main runner code
  command = ARGV[0]
  if !command
    list_commands
  else
    handle_command(command)
  end
end

#save(data) ⇒ Object



17
18
19
# File 'lib/droid.rb', line 17

def save(data)
  File.open(FILE, 'w') { |f| YAML.dump(data, f) }
end