-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.rb
34 lines (30 loc) · 914 Bytes
/
event.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
class Neo::Event
@events = {}
class << self
# registers an event named <label>
# if you want to unregister an event later, define an <identifier>
def register(label, identifier, &block)
@events[label] = {} if @events[label].nil?
@events[label][identifier] = block
end
# unregister an event named <label> and identified by <identifier>
def unregister(label, identifier)
@events[label].delete(identifier) unless @events[label].nil? && @events[label][identifier].nil?
end
# deletes an entire event named <label>
def delete(label)
@events.delete(label) unless @events[label].nil?
end
# trigger an event named <label>
def trigger(label)
@events[label] = {} if @events[label].nil?
@events[label].each_value do |block|
block.call
end
end
# clear all events
def clear
@events = {}
end
end
end