Module: Rucola::Notifications::ClassMethods

Defined in:
lib/rucola/rucola_support/notifications/notifications.rb

Instance Method Summary collapse

Instance Method Details

#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

  notify_on :win_did_become_key do |notification|
    # code
  end
end

By default the shortcut { :app => :application } is registered.



47
48
49
# File 'lib/rucola/rucola_support/notifications/notifications.rb', line 47

def notification_prefix(prefixes)
  (@_notification_prefixes ||= {}).merge! prefixes
end

#notify(method_to_notify, options = {}) ⇒ Object

Register a callback 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


119
120
121
122
# File 'lib/rucola/rucola_support/notifications/notifications.rb', line 119

def notify(method_to_notify, options = {})
  @_registered_notifications ||= {}
  @_registered_notifications[options[:when]] = method_to_notify
end

#notify_on(notification, &block) ⇒ Object

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.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rucola/rucola_support/notifications/notifications.rb', line 70

def notify_on(notification, &block)
  notification_name = notification
  
  if notification_name.is_a? Symbol
    notification_name = notification_name.to_s
    
    # first check if this notification_name uses a shortcut prefix
    splitted_notification_name = notification_name.split('_')
    prefix = splitted_notification_name.first.to_sym
    if @_notification_prefixes and @_notification_prefixes.has_key? prefix
      notification_name = @_notification_prefixes[prefix].to_s << '_' << splitted_notification_name[1..-1].join('_')
    end

    begin
      # try with only Notification appended
      notification_name = notification_name.camel_case << 'Notification'
      OSX.const_get(notification_name)
    rescue NameError
      begin
        # then try with NS prepended
        notification_name = 'NS' << notification_name
        OSX.const_get(notification_name)
      rescue NameError
        raise NameError, "Unable to find the notification corresponding to :#{notification}"
      end
    end
  end
  
  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