Class: PPCurses::NotificationCentre

Inherits:
Object
  • Object
show all
Defined in:
lib/ppcurses/notification_centre.rb

Constant Summary collapse

@@default_centre =
PPCurses::NotificationCentre.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNotificationCentre

Returns a new instance of NotificationCentre.



5
6
7
# File 'lib/ppcurses/notification_centre.rb', line 5

def initialize
  @listeners = Hash.new
end

Class Method Details

.default_centreObject



11
12
13
# File 'lib/ppcurses/notification_centre.rb', line 11

def NotificationCentre.default_centre
 @@default_centre
end

Instance Method Details

#add_observer(observer, selector, name, sender) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ppcurses/notification_centre.rb', line 16

def add_observer( observer, selector, name, sender)

   if @listeners.has_key?(name)
    sender_hash = @listeners.fetch(name)
   else
    sender_hash = Hash.new
   @listeners.store(name, sender_hash)
  end

   sender_hash.store(sender, [observer, selector])

end

#post_notification(name, sender) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ppcurses/notification_centre.rb', line 30

def post_notification ( name, sender )

  if @listeners.has_key?(name) == false then return end

  sender_hash =  @listeners.fetch(name)

  if sender_hash.has_key?(sender) == false then return end

  listener_info = sender_hash.fetch(sender)
  # observer = listener_info[0]  # Currently unused
  callback = listener_info[1] 

  notification = Notification.new(name, sender)

  callback.call ( notification )
end