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

Public Instance Methods

add_notifier(name, opts = {}) click to toggle source

Add a notification library to be used.

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

# File lib/guard/notifier.rb, line 148
def add_notifier(name, opts = {})
  return turn_off if name == :off

  notifier_class = _get_notifier_module(name)

  if notifier_class && notifier_class.available?(opts)
    self.notifiers = notifiers << { name: name, options: opts }
    true
  else
    false
  end
end
clear_notifiers() click to toggle source

Clear available notifications.

# File lib/guard/notifier.rb, line 86
def clear_notifiers
  ENV['GUARD_NOTIFIERS'] = 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 137
def enabled?
  ENV['GUARD_NOTIFY'] == 'true'
end
notifiers() click to toggle source
# File lib/guard/notifier.rb, line 76
def notifiers
  ENV['GUARD_NOTIFIERS'] ? YAML::load(ENV['GUARD_NOTIFIERS']) : []
end
notifiers=(notifiers) click to toggle source
# File lib/guard/notifier.rb, line 80
def notifiers=(notifiers)
  ENV['GUARD_NOTIFIERS'] = YAML::dump(notifiers)
end
notify(message, opts = {}) click to toggle source

Show a system notification with all configured notifiers.

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

# File lib/guard/notifier.rb, line 167
def notify(message, opts = {})
  return unless enabled?

  notifiers.each do |notifier|
    notifier = _get_notifier_module(notifier[:name]).new(notifier[:options])

    begin
      notifier.notify(message, opts)
    rescue Exception => e
      ::Guard::UI.error "Error sending notification with #{ notifier.name }: #{ e.message }"
    end
  end
end
toggle() click to toggle source

Toggle the system notifications on/off

# File lib/guard/notifier.rb, line 124
def toggle
  if enabled?
    ::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 112
def turn_off
  notifiers.each do |notifier|
    notifier_class = _get_notifier_module(notifier[:name])

    notifier_class.turn_off if notifier_class.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 93
def turn_on
  _auto_detect_notification if notifiers.empty? && (!::Guard.options || ::Guard.options.notify)

  if notifiers.empty?
    turn_off
  else
    notifiers.each do |notifier|
      notifier_class = _get_notifier_module(notifier[:name])
      ::Guard::UI.info "Guard is using #{ notifier_class.title } to send notifications."

      notifier_class.turn_on if notifier_class.respond_to?(:turn_on)
    end

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.