Module: Shirka::Eventalk

Included in:
App, Controller, View
Defined in:
lib/shirka/eventalk.rb

Instance Method Summary collapse

Instance Method Details

#add_watcher(obj) ⇒ Object



32
33
34
35
# File 'lib/shirka/eventalk.rb', line 32

def add_watcher(obj)
  @watchers ||= []
  @watchers << obj
end

#fire(signal, msg = nil, receivers = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/shirka/eventalk.rb', line 5

def fire(signal, msg=nil, receivers=nil)
  if receivers.nil?
    receivers = watchers
  else
    receivers = Array(receivers)
  end
  
  receivers.each do |w|
    method = "on_#{signal}".to_sym
    if w.respond_to? method
      e = OpenStruct.new
      e.signal =  signal
      e.msg = msg
      e.sender = self
      w.send method, e
    end
  end
end

#forward(event, receivers = nil) ⇒ Object



24
25
26
# File 'lib/shirka/eventalk.rb', line 24

def forward(event, receivers=nil)
  fire event.signal, event.msg, receivers
end

#watch(obj) ⇒ Object



37
38
39
# File 'lib/shirka/eventalk.rb', line 37

def watch(obj)
  obj.add_watcher self
end

#watchersObject



28
29
30
# File 'lib/shirka/eventalk.rb', line 28

def watchers
  @watchers ||= []
end