File: trigger.rb

package info (click to toggle)
ruby-god 0.12.1-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 752 kB
  • sloc: ruby: 5,913; ansic: 217; makefile: 3
file content (43 lines) | stat: -rw-r--r-- 900 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module God

  class Trigger

    class << self
      attr_accessor :triggers # {task.name => condition}
    end

    # init
    self.triggers = {}
    @mutex = Mutex.new

    def self.register(condition)
      @mutex.synchronize do
        self.triggers[condition.watch.name] ||= []
        self.triggers[condition.watch.name] << condition
      end
    end

    def self.deregister(condition)
      @mutex.synchronize do
        self.triggers[condition.watch.name].delete(condition)
        self.triggers.delete(condition.watch.name) if self.triggers[condition.watch.name].empty?
      end
    end

    def self.broadcast(task, message, payload)
      return unless self.triggers[task.name]

      @mutex.synchronize do
        self.triggers[task.name].each do |t|
          t.process(message, payload)
        end
      end
    end

    def self.reset
      self.triggers.clear
    end

  end

end