-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.rb
37 lines (33 loc) · 879 Bytes
/
events.rb
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
class Hook
def self.hooks
@hooks ||= []
end
def initialize(event, conditions=false, &callback)
# Add the hook to the list of hooks
Hook.hooks << { :event => event, :conditions => conditions, :callback => callback }
end
end
# Syntactic sugar - preferred to Hook.new
def on(event, conditions=false, &callback)
return Hook.new(event, conditions, &callback)
end
# Syntactic sugar - preferred to on(:command)
def command(command, conditions={}, &callback)
return Hook.new(:command, conditions.merge({:command => command.to_s}), &callback)
end
def fireevent(event, arguments)
Hook.hooks.each { |hook|
if hook[:event] == event
if !hook[:conditions]
hook[:callback].call(arguments)
else
fire = true
hook[:conditions].each do
|k,v|
fire = false unless arguments[k] == v
end
hook[:callback].call(arguments) if fire
end
end
}
end