Module: Rucola::Notifications::ClassMethods
- Defined in:
- lib/rucola/rucola_support/notifications/notifications.rb
Overview
This Notifications module will add a class method called notify_on
, which registers your object for the given notification and executes the given block when the notification is posted to the OSX::NSNotificationCenter.defaultCenter.
class FooController < OSX::NSObject
notify_on OSX::NSApplicationDidFinishLaunchingNotification do |notification|
puts "Application did finish launching."
p notification
end
# code
end
In addition to notify_on, you also get a method called notify which allows you to specify methods
to be invoked when a notification is posted.
class FooController < OSX::NSObject
notify :some_method, :when => :application_did_finish_launching
def some_method(notification)
puts "Application finished launching"
end
end
Instance Method Summary collapse
-
#fire_notification(notification, obj) ⇒ Object
(also: #post_notification)
Creates a notification and posts it to the reciever.
-
#notification_prefix(prefixes) ⇒ Object
Add prefix shortcuts as a hash.
-
#notify(method_to_notify, options = {}) ⇒ Object
Register a callback when a notification is posted.
-
#notify_on(notification, &block) ⇒ Object
(also: #once)
Registers the object for the given notification and executes the given block when the notification is posted to the OSX::NSNotificationCenter.defaultCenter.
Instance Method Details
#fire_notification(notification, obj) ⇒ Object Also known as: post_notification
Creates a notification and posts it to the reciever
50 51 52 53 |
# File 'lib/rucola/rucola_support/notifications/notifications.rb', line 50 def fire_notification(notification, obj) notification_name = resolve_notification_name(notification) OSX::NSNotificationCenter.defaultCenter.postNotificationName_object(notification_name, obj) end |
#notification_prefix(prefixes) ⇒ Object
Add prefix shortcuts as a hash.
class FooController < OSX::NSObject
acts_as_notifiable
# This will make sure that :win_ is expanded to :window_ in the notifications that you register.
notification_prefix :win => :window
when :win_did_become_key do |notification|
# code
end
end
By default the shortcut { :app => :application }
is registered.
45 46 47 |
# File 'lib/rucola/rucola_support/notifications/notifications.rb', line 45 def notification_prefix(prefixes) (@_notification_prefixes ||= {}).merge! prefixes end |
#notify(method_to_notify, options = {}) ⇒ Object
122 123 124 125 |
# File 'lib/rucola/rucola_support/notifications/notifications.rb', line 122 def notify(method_to_notify, = {}) @_registered_notifications ||= {} @_registered_notifications[[:when]] = method_to_notify end |
#notify_on(notification, &block) ⇒ Object Also known as: once
Registers the object for the given notification and executes the given block when the notification is posted to the OSX::NSNotificationCenter.defaultCenter.
class FooController < OSX::NSObject
notify_on OSX::NSApplicationDidFinishLaunchingNotification do |notification|
puts "Application did finish launching."
p notification
end
# code
end
You can also pass it a symbol as notification
in which case it will be exapnded. It will first check if the name + ‘Notification’ exists, if not it will prepend ‘NS’. So :application_did_finish_launching becomes ‘NSApplicationDidFinishLaunchingNotification’.
You can even register shortcut prefixes. See notification_prefix
.
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/rucola/rucola_support/notifications/notifications.rb', line 75 def notify_on(notification, &block) notification_name = resolve_notification_name(notification) method_name = "_handle_#{notification_name.snake_case}".to_sym # define the handle method class_eval do define_method(method_name, &block) end @_registered_notifications ||= {} @_registered_notifications[notification_name.to_s] = method_name end |