Class: Hub::Runner

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

Overview

The Hub runner expects to be initialized with ‘ARGV` and primarily exists to run a git command.

The actual functionality, that is, the code it runs when it needs to augment a git command, is kept in the ‘Hub::Commands` module.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Runner

Returns a new instance of Runner.



10
11
12
13
14
15
16
17
18
19
# File 'lib/hub/runner.rb', line 10

def initialize(*args)
  @args = Args.new(args)

  # Hack to emulate git-style
  @args.unshift 'help' if @args.grep(/^[^-]|version/).empty?

  if Commands.respond_to?(@args[0])
    Commands.send(@args[0], @args)
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



8
9
10
# File 'lib/hub/runner.rb', line 8

def args
  @args
end

Class Method Details

.execute(*args) ⇒ Object

Shortcut



22
23
24
# File 'lib/hub/runner.rb', line 22

def self.execute(*args)
  new(*args).execute
end

Instance Method Details

#afterObject

Returns the current after callback, which (if set) is run after the target git command.

See the ‘Hub::Args` class for more information on the `after` callback.



31
32
33
# File 'lib/hub/runner.rb', line 31

def after
  args.after.to_s
end

#commandObject

A string representation of the git command we would run if #execute were called.



37
38
39
# File 'lib/hub/runner.rb', line 37

def command
  args.to_exec.join(' ')
end

#executeObject

Runs the target git command with an optional callback. Replaces the current process.



43
44
45
46
47
48
49
# File 'lib/hub/runner.rb', line 43

def execute
  if args.after?
    execute_with_after_callback
  else
    exec(*args.to_exec)
  end
end

#execute_with_after_callbackObject

Runs the target git command then executes the ‘after` callback.

See the ‘Hub::Args` class for more information on the `after` callback.



55
56
57
58
59
60
61
62
63
# File 'lib/hub/runner.rb', line 55

def execute_with_after_callback
  after = args.after
  if system(*args.to_exec)
    after.respond_to?(:call) ? after.call : exec(after)
    exit
  else
    exit 1
  end
end