Module: Larynx::Observable

Included in:
CallHandler
Defined in:
lib/larynx/observable.rb

Instance Method Summary collapse

Instance Method Details

#add_observer(object) ⇒ Object



4
5
6
7
# File 'lib/larynx/observable.rb', line 4

def add_observer(object)
  @observers ||= []
  @observers << object
end

#clear_observers!Object



13
14
15
# File 'lib/larynx/observable.rb', line 13

def clear_observers!
  @observers = []
end

#notify_current_observer(event, data = nil) ⇒ Object

Like an observer stack which only notifies top observer



18
19
20
21
22
23
24
# File 'lib/larynx/observable.rb', line 18

def notify_current_observer(event, data=nil)
  return unless @observers
  obs = @observers.last
  if obs.respond_to?(event)
    data ? obs.send(event, data) : obs.send(event)
  end
end

#notify_observers(event, data = nil) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/larynx/observable.rb', line 26

def notify_observers(event, data=nil)
  return unless @observers
  @observers.each do |obs|
    next unless obs.respond_to?(event)
    data ? obs.send(event, data) : obs.send(event)
  end
end

#remove_observer(object) ⇒ Object



9
10
11
# File 'lib/larynx/observable.rb', line 9

def remove_observer(object)
  @observers && @observers.delete(object)
end