Module: Hookify::Command

Extended by:
Helpers
Defined in:
lib/hookify/command.rb,
lib/hookify/commands/base.rb,
lib/hookify/commands/pre_commit.rb,
lib/hookify/commands/pre_release.rb

Overview

Defined Under Namespace

Classes: Base, CommandFailed, InvalidCommand, PreCommit, PreRelease

Class Method Summary collapse

Methods included from Helpers

display, error, executable, home_directory, hooks_directory, running_on_a_mac?, running_on_windows?

Class Method Details

.parse(command) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/hookify/command.rb', line 30

def parse(command)
  parts = command.split(':').collect {|i| i.gsub("-", "_")}
  case parts.size
    when 1
      begin
        return "Hookify::Command::#{parts[0].camelize}".constantize, :create
      rescue NameError, NoMethodError
        return Hookify::Command::Base, parts[0]
      end
    when 2
      begin
        return "Hookify::Command::#{parts[0].camelize}".constantize, parts[1]
      rescue NameError
        raise InvalidCommand
      end
    else
      raise InvalidCommand
  end
end

.run(command, args) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/hookify/command.rb', line 11

def run(command, args)
  begin
    run_command(command, args.dup)
  rescue InvalidCommand
    error "Unknown command '#{command.to_s}'. Run 'hookify help' for usage information."
  rescue CommandFailed => e
    error e.message
  rescue Interrupt => e
    error "\n[canceled]"
  end
end

.run_command(command, args) ⇒ Object

Raises:



23
24
25
26
27
28
# File 'lib/hookify/command.rb', line 23

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