Module: Scorm::Command

Defined in:
lib/scorm/command.rb,
lib/scorm/commands/base.rb,
lib/scorm/commands/help.rb,
lib/scorm/commands/check.rb,
lib/scorm/commands/bundle.rb,
lib/scorm/commands/create.rb,
lib/scorm/commands/extract.rb,
lib/scorm/commands/version.rb

Defined Under Namespace

Classes: Base, Bundle, Check, CommandFailed, Create, Extract, Help, InvalidCommand, Version

Class Method Summary collapse

Class Method Details

.error(msg) ⇒ Object


12
13
14
15
# File 'lib/scorm/command.rb', line 12

def error(msg)
  STDERR.puts(msg)
  exit 1
end

.parse(command) ⇒ Object


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/scorm/command.rb', line 42

def parse(command)
  parts = command.split(':')
  case parts.size
    when 1
      begin
        return eval("Scorm::Command::#{command.capitalize}"), :index
      rescue NameError, NoMethodError
        raise InvalidCommand
      end
    when 2
      begin
        return Scorm::Command.const_get(parts[0].capitalize), parts[1]
      rescue NameError
        raise InvalidCommand
      end
    else
      raise InvalidCommand
  end
end

.run(command, args) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/scorm/command.rb', line 17

def run(command, args)
  begin
    run_internal(command, args.dup)
  rescue Zip::ZipError => e
    error e.message
  rescue InvalidPackage => e
    error e.message
  rescue InvalidManifest => e
    error e.message
  rescue InvalidCommand
    error "Unknown command. Run 'scorm help' for usage information."
  rescue CommandFailed => e
    error e.message
  rescue Interrupt => e
    error "\n[canceled]"
  end
end

.run_internal(command, args) ⇒ Object

Raises:


35
36
37
38
39
40
# File 'lib/scorm/command.rb', line 35

def run_internal(command, args)
  klass, method = parse(command)
  runner = klass.new(args)
  raise InvalidCommand unless runner.respond_to?(method)
  runner.send(method)
end