Files

Guard::Notifier

The notifier handles sending messages to different notifiers. Currently the following libraries are supported:

Please see the documentation of each notifier for more information about the requirements and configuration possibilities.

Guard knows four different notification types:

The notification type selection is based on the image option that is sent to {notify}. Each image type has its own notification type, and notifications with custom images goes all sent as type `notify`. The `gntp` and `growl_notify` notifiers are able to register these types at Growl and allows customization of each notification type.

Guard can be configured to make use of more than one notifier at once, @see Guard::Dsl

Constants

NOTIFIERS

List of available notifiers, grouped by functionality. It needs to be a nested hash instead of a simpler Hash, because it maintains its order on Ruby 1.8.7 also.

Public Instance Methods

add_notification(name, options = { }, silent = false) click to toggle source

Add a notification library to be used.

@param [Symbol] name the name of the notifier to use @param [Boolean] silent disable any error message @param [Hash] options the notifier options @return [Boolean] if the notification could be added

# File lib/guard/notifier.rb, line 153
def add_notification(name, options = { }, silent = false)
  return turn_off if name == :off

  notifier = get_notifier_module(name)

  if notifier && notifier.available?(silent, options)
    self.notifications = notifications << { :name => name, :options => options }
    true
  else
    false
  end
end
clear_notifications() click to toggle source

Clear available notifications.

# File lib/guard/notifier.rb, line 92
def clear_notifications
  ENV['GUARD_NOTIFICATIONS'] = nil
end
enabled?() click to toggle source

Test if the notifications are on.

@return [Boolean] whether the notifications are on

# File lib/guard/notifier.rb, line 142
def enabled?
  ENV['GUARD_NOTIFY'] == 'true'
end
notifications() click to toggle source

Get the available notifications.

@return [Hash] the notifications

# File lib/guard/notifier.rb, line 78
def notifications
  ENV['GUARD_NOTIFICATIONS'] ? YAML::load(ENV['GUARD_NOTIFICATIONS']) : []
end
notifications=(notifications) click to toggle source

Set the available notifications.

@param [Array<Hash>] notifications the notifications

# File lib/guard/notifier.rb, line 86
def notifications=(notifications)
  ENV['GUARD_NOTIFICATIONS'] = YAML::dump(notifications)
end
notify(message, options = { }) click to toggle source

Show a system notification with all configured notifiers.

@param [String] message the message to show @option options [Symbol, String] image the image symbol or path to an image @option options [String] title the notification title

# File lib/guard/notifier.rb, line 172
def notify(message, options = { })
  if enabled?
    type  = notification_type(options[:image] || :success)
    image = image_path(options.delete(:image) || :success)
    title = options.delete(:title) || 'Guard'

    notifications.each do |notification|
      begin
        get_notifier_module(notification[:name]).notify(type, title, message, image, options.merge(notification[:options]))
      rescue Exception => e
        ::Guard::UI.error "Error sending notification with #{ notification[:name] }: #{ e.message }"
      end
    end
  end
end
toggle() click to toggle source

Toggle the system notifications on/off

# File lib/guard/notifier.rb, line 129
def toggle
  if ENV['GUARD_NOTIFY'] == 'true'
    ::Guard::UI.info 'Turn off notifications'
    turn_off
  else
    turn_on
  end
end
turn_off() click to toggle source

Turn notifications off.

# File lib/guard/notifier.rb, line 118
def turn_off
  notifications.each do |notification|
    notifier = get_notifier_module(notification[:name])
    notifier.turn_off(notification[:options]) if notifier.respond_to?(:turn_off)
  end

  ENV['GUARD_NOTIFY'] = 'false'
end
turn_on() click to toggle source

Turn notifications on. If no notifications are defined in the `Guardfile` Guard auto detects the first available library.

# File lib/guard/notifier.rb, line 100
def turn_on
  auto_detect_notification if notifications.empty? && (!::Guard.options || ::Guard.options[:notify])

  if notifications.empty?
    ENV['GUARD_NOTIFY'] = 'false'
  else
    notifications.each do |notification|
      ::Guard::UI.info "Guard uses #{ get_notifier_module(notification[:name]).to_s.split('::').last } to send notifications."
      notifier = get_notifier_module(notification[:name])
      notifier.turn_on(notification[:options]) if notifier.respond_to?(:turn_on)
    end

    ENV['GUARD_NOTIFY'] = 'true'
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.