Class: Amp::Hook

Inherits:
Object show all
Defined in:
lib/amp/commands/hooks.rb

Overview

Hook

The hook class allows the end-user to easily provide hooks into the code that makes Amp go. For example, one might want to easily hook into the commit command, and send an e-mail out to your team after every commit. You could use the Hook class, as well as the Kernel-level “hook” method, to do this.

Hooks are global, currently - they cannot only be applied to one repo at a time.

Constant Summary collapse

DEFAULTS =
{:throw => false}
@@all_hooks =
Hash.new {|h, k| h[k] = []}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) {|opts| ... } ⇒ Hook

Registers a hook with the system. A hook is simply a proc that takes some options. The hook has a name, which specifies which action it is hooking into. Some hooks include:

:outgoing
:prechangegroup
:changegroup

Parameters:

  • hook_type (Symbol)

    the type of hook

Yields:

  • the given block is the action to take when the hook is executed

Yield Parameters:

  • opts

    the options that are passed to the hook. See hook.rb for details on all possible hooks and their passed-in options.



52
53
54
55
56
# File 'lib/amp/commands/hooks.rb', line 52

def initialize(name, &block)
  @name  = name
  @block = block
  @@all_hooks[name] << self
end

Instance Attribute Details

#blockObject

The block to be executed when the hook is called



38
39
40
# File 'lib/amp/commands/hooks.rb', line 38

def block
  @block
end

#nameObject

The call symbol this hook is associated with



34
35
36
# File 'lib/amp/commands/hooks.rb', line 34

def name
  @name
end

Class Method Details

.all_hooksObject



13
# File 'lib/amp/commands/hooks.rb', line 13

def self.all_hooks; @@all_hooks; end

.run_hook(call, opts = {}) ⇒ Object

Call the hooks that run under call

Parameters:

  • call (Symbol)

    the location in the system where the hooks are to be called

  • opts (Hash) (defaults to: {})

    the options to pass to the hook



26
27
28
29
# File 'lib/amp/commands/hooks.rb', line 26

def run_hook(call, opts={})
  opts = DEFAULTS.merge opts
  all_hooks[call].each {|hook| hook[opts] }
end

Instance Method Details

#call(opts) ⇒ Object Also known as: run, []

Runs the hook.

Parameters:

  • opts (Hash)

    the options to pass to the hook.



62
63
64
# File 'lib/amp/commands/hooks.rb', line 62

def call(opts)
  @block.call(opts)
end