Class: Subtrigger::Rule
- Inherits:
-
Object
- Object
- Subtrigger::Rule
- Defined in:
- lib/subtrigger/rule.rb
Overview
A Rule
object knows when to fire some kind of action for some kind of revision. When the Subversion hook is fired, a Rule can inspect it and choose whether or not to fire its trigger (a piece code defined by the user).
In the first example, the rule will output fired
whenever a Revision
comes along with a message containing foo
.
In the second example, we find all applicable rules for a given Revision
object. We can then run each of them.
Constant Summary collapse
- CannotCompare =
Exception for when trying to apply a rule to something other than an instance of Revision.
Class.new(Exception)
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
The callback to run on a match.
-
#criteria ⇒ Object
readonly
A hash of Revision attributes and regular expressions to match against.
Class Method Summary collapse
-
.matching(revision) ⇒ Array<Rule>
Return an array of all existing Rule objects that match the given revision.
-
.reset ⇒ Object
Reset the list of known rules, deleting all currently known rules.
-
.rules ⇒ Array<Rule>
Return an array of all rules currently defined.
Instance Method Summary collapse
-
#===(other) ⇒ Boolean
Use #matches? to see if this
Rule
matches the givenRevision
. -
#initialize(pattern_or_options, &block) ⇒ Rule
constructor
Create a new Rule object with criteria for different properties of a Revision.
-
#matches?(revision) ⇒ Boolean
See if the current rule matches a given subversion revision.
-
#run(rev) ⇒ nil
Call this Rule’s callback method with the give Revision object.
Constructor Details
#initialize(pattern, &block) ⇒ Rule #initialize(options, &block) ⇒ Rule
Create a new Rule object with criteria for different properties of a Revision. The required block defines the callback to run. It will have the current Revision object yielded to it.
Criteria are Ruby objects that should match (‘===`) a Revision’s attributes. These would usually be regular expressions, but they can be strings or custom objects if you want to.
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/subtrigger/rule.rb', line 90 def initialize(, &block) raise ArgumentError, 'a Rule requires a block' unless block_given? # If not given a hash, we build a hash defaulting on message unless .is_a?(Hash) = { :message => } end @criteria, @block = , block @criteria.inspect self.class.register self end |
Instance Attribute Details
#block ⇒ Object (readonly)
The callback to run on a match
32 33 34 |
# File 'lib/subtrigger/rule.rb', line 32 def block @block end |
#criteria ⇒ Object (readonly)
A hash of Revision attributes and regular expressions to match against
29 30 31 |
# File 'lib/subtrigger/rule.rb', line 29 def criteria @criteria end |
Class Method Details
.matching(revision) ⇒ Array<Rule>
Return an array of all existing Rule objects that match the given revision.
67 68 69 |
# File 'lib/subtrigger/rule.rb', line 67 def self.matching(revision) @rules.select { |child| child === revision } end |
.reset ⇒ Object
Reset the list of known rules, deleting all currently known rules.
58 59 60 |
# File 'lib/subtrigger/rule.rb', line 58 def self.reset @rules = [] end |
.rules ⇒ Array<Rule>
Return an array of all rules currently defined.
51 52 53 |
# File 'lib/subtrigger/rule.rb', line 51 def self.rules @rules end |
Instance Method Details
#===(other) ⇒ Boolean
Use #matches? to see if this Rule
matches the given Revision
.
116 117 118 119 120 |
# File 'lib/subtrigger/rule.rb', line 116 def ===(other) matches?(other) rescue CannotCompare super end |
#matches?(revision) ⇒ Boolean
See if the current rule matches a given subversion revision.
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/subtrigger/rule.rb', line 129 def matches?(revision) raise CannotCompare unless @criteria.keys.all? { |k| k == :all || revision.respond_to?(k) } match = @criteria.any? @criteria.each_pair do |key, value| if key == :all match = (value === revision) else match &= (value === revision.send(key.to_sym)) end end match end |
#run(rev) ⇒ nil
Call this Rule’s callback method with the give Revision object.
105 106 107 108 |
# File 'lib/subtrigger/rule.rb', line 105 def run(rev) @rev = rev block.call(@rev, collect_captures) end |