Class: Subtrigger::Trigger

Inherits:
Object
  • Object
show all
Defined in:
lib/subtrigger/trigger.rb

Overview

Call blocks on a matching pattern

This is a framework for combining pairs of matchers and callbacks to run on a commit message. You can define a trigger which this class will apply to a log message.

Example usage

Trigger.define(/foo/) do |matches, repo|
  puts "Someone used 'foo' in his commit message"
end

When the above trigger is defined and somebody makes a commit message containing foo the block will be called. matches contains any captured regular expression groups, repo is a Repository object for the current repository revision.

You can define as many triggers as you like. When no triggers are found an exception will be raised. When no trigger applies, it will quit silently.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, repo, &block) ⇒ Trigger

Returns a new instance of Trigger.



48
49
50
51
# File 'lib/subtrigger/trigger.rb', line 48

def initialize(pattern, repo, &block)
  @pattern, @repo, @callback = pattern, repo, block
  parse
end

Class Method Details

.define(pattern, &block) ⇒ Object

Create a new Trigger object and add it to the stack.



43
44
45
# File 'lib/subtrigger/trigger.rb', line 43

def define(pattern, &block)
  (@triggers ||= {})[pattern] = block;
end

.resetObject



38
39
40
# File 'lib/subtrigger/trigger.rb', line 38

def reset
  @triggers = {}
end

.run(repo) ⇒ Object

Run all available triggers on the given Repository object.



27
28
29
30
31
32
# File 'lib/subtrigger/trigger.rb', line 27

def run(repo)
  raise 'No suitable triggers found.' if @triggers.nil?
  @triggers.each_pair do |pattern, block|
    new(pattern, repo, &block)
  end
end

.triggersObject



34
35
36
# File 'lib/subtrigger/trigger.rb', line 34

def triggers
  @triggers ||= {}
end