Module: GitRunner::Hooks

Extended by:
Hooks
Included in:
Hooks
Defined in:
lib/git-runner/hooks.rb,
lib/git-runner/hooks.rb

Defined Under Namespace

Classes: InvalidCallable

Instance Method Summary collapse

Instance Method Details

#fire(name, data = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/git-runner/hooks.rb', line 22

def fire(name, data=nil)
  return unless registrations.keys.include?(name)

  registrations[name].each do |callable|
    if callable.arity == 1
      callable.call(data)
    else
      callable.call
    end
  end
end

#register(name, callable) ⇒ Object



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

def register(name, callable)
  unless callable.respond_to?(:call) && callable.respond_to?(:arity)
    raise InvalidCallable.new("Supplied callable is not callable, should respond to #call and #arity")
  end

  if callable.arity > 1
    raise InvalidCallable.new("Supplied callable takes #{callable.arity} argument(s), should accept either 1 or 0.")
  end

  registrations[name] << callable
end

#registrationsObject



6
7
8
# File 'lib/git-runner/hooks.rb', line 6

def registrations
  @registrations ||= Hash.new { |hash, key| hash[key] = [] }
end