Class: Guard::Notifier::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/guard/notifiers/base.rb

Overview

Base class for all notifiers.

Constant Summary

HOSTS =
{
  darwin:  'Mac OS X',
  linux:   'Linux',
  freebsd: 'FreeBSD',
  openbsd: 'OpenBSD',
  sunos:   'SunOS',
  solaris: 'Solaris',
  mswin:   'Windows',
  mingw:   'Windows',
  cygwin:  'Windows'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Base

Returns a new instance of Base



25
26
27
# File 'lib/guard/notifiers/base.rb', line 25

def initialize(opts = {})
  @options = opts
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options



23
24
25
# File 'lib/guard/notifiers/base.rb', line 23

def options
  @options
end

Class Method Details

._supported_host?Boolean

Checks if the current OS is supported by the notifier.

Returns:

  • (Boolean)

See Also:



157
158
159
160
# File 'lib/guard/notifiers/base.rb', line 157

def self._supported_host?
  supported_hosts == :all ||
  RbConfig::CONFIG['host_os'] =~ /#{supported_hosts.join('|')}/
end

.available?(opts = {}) ⇒ Boolean

Test if the notifier can be used.

Parameters:

  • opts (Hash) (defaults to: {})

    notifier options

Options Hash (opts):

  • silent (Boolean)

    true if no error messages should be shown

Returns:

  • (Boolean)

    the availability status



45
46
47
48
49
50
51
52
53
# File 'lib/guard/notifiers/base.rb', line 45

def self.available?(opts = {})
  if _supported_host?
    true
  else
    hosts = supported_hosts.map { |host| HOSTS[host.to_sym] }.join(', ')
    ::Guard::UI.error "The :#{name} notifier runs only on #{hosts}." unless opts.fetch(:silent) { false }
    false
  end
end

.gem_nameString

Returns the name of the notifier's gem. By default it returns the notifier name. This method can be overriden by subclasses.

Examples:

Un-modulize, underscorize and downcase the class name

Guard::Notifier::FileNotifier.gem_name
#=> 'file_notifier'

Returns:

  • (String)

    the name of the notifier's gem



96
97
98
# File 'lib/guard/notifiers/base.rb', line 96

def self.gem_name
  name
end

.nameString

Returns the name of the notifier.

Examples:

Un-modulize, underscorize and downcase the class name

Guard::Notifier::FileNotifier.name
#=> 'file_notifier'

Returns:

  • (String)

    the name of the notifier



83
84
85
# File 'lib/guard/notifiers/base.rb', line 83

def self.name
  title.gsub(/([a-z])([A-Z])/, '\1_\2').downcase
end

.require_gem_safely(opts = {}) ⇒ Boolean

This method tries to require the gem whose name is returned by `.gem_name`. If a LoadError or NameError occurs, it displays an error message (unless opts is true) and returns false.

Parameters:

  • opts (Hash) (defaults to: {})

    some options

Options Hash (opts):

  • silent (Boolean)

    true if no error messages should be shown

Returns:

  • (Boolean)

    whether or not the gem is loaded



109
110
111
112
113
114
115
116
117
# File 'lib/guard/notifiers/base.rb', line 109

def self.require_gem_safely(opts = {})
  require gem_name
  true
rescue LoadError, NameError
  unless opts[:silent]
    ::Guard::UI.error "Please add \"gem '#{gem_name}'\" to your Gemfile and run Guard with \"bundle exec\"."
  end
  false
end

.supported_hostsObject

This method should be overriden by subclasses and return an array of OSes the notifier supports. By default, it returns :all which mean there's no check against the current OS.



35
36
37
# File 'lib/guard/notifiers/base.rb', line 35

def self.supported_hosts
  :all
end

.titleString

Returns the title of the notifier.

Examples:

Un-modulize the class name

Guard::Notifier::FileNotifier.title
#=> 'FileNotifier'

Returns:

  • (String)

    the title of the notifier



71
72
73
# File 'lib/guard/notifiers/base.rb', line 71

def self.title
  self.to_s.sub(/.+::(\w+)$/, '\1')
end

Instance Method Details

#images_pathPathname

Paths where all Guard images are located

Returns:

  • (Pathname)

    the path to the images directory



147
148
149
# File 'lib/guard/notifiers/base.rb', line 147

def images_path
  @images_path ||= Pathname.new(File.dirname(__FILE__)).join('../../../images')
end

#nameString

Returns the name of the notifier.

Examples:

Un-modulize, underscorize and downcase the class name

Guard::Notifier::FileNotifier.new.name
#=> 'file_notifier'

Returns:

  • (String)

    the name of the notifier



139
140
141
# File 'lib/guard/notifiers/base.rb', line 139

def name
  self.class.name
end

#normalize_standard_options!(opts) ⇒ Object

Set or modify the `:title`, `:type` and `:image` options for a notification. Should be used in `#notify`.

Parameters:

  • opts (Hash)

    additional notification library options

Options Hash (opts):

  • type (String)

    the notification type. Either 'success', 'pending', 'failed' or 'notify'

  • title (String)

    the notification title

  • image (String)

    the path to the notification image



171
172
173
174
175
# File 'lib/guard/notifiers/base.rb', line 171

def normalize_standard_options!(opts)
  opts[:title] ||= 'Guard'
  opts[:type]  ||= _notification_type(opts.fetch(:image, :success))
  opts[:image]   = _image_path(opts.delete(:image) { :success })
end

#notify(message, opts = {}) ⇒ Object

This method must be overriden.



57
58
59
60
61
# File 'lib/guard/notifiers/base.rb', line 57

def notify(message, opts = {})
  options.delete(:silent)
  opts.replace(options.merge(opts))
  normalize_standard_options!(opts)
end

#titleString

Returns the title of the notifier.

Examples:

Un-modulize the class name

Guard::Notifier::FileNotifier.new.title
#=> 'FileNotifier'

Returns:

  • (String)

    the title of the notifier



127
128
129
# File 'lib/guard/notifiers/base.rb', line 127

def title
  self.class.title
end