Configurable path to growlnotify bin #2

Merged
merged 2 commits into from Oct 15, 2011
Jump to file or symbol
Failed to load files and symbols.
+71 −42
Split
View
@@ -43,30 +43,38 @@ Ruby growlnotify 'bindings'.
notify_warning 'Merge required'
notify_error 'Failed to send email', :sticky => true
+== Custom path to growlnotify
+
+If needed you can set a path to the binary with:
+
+ Growl.bin_path = '~/my/path/to/growlnotify'
+
+right after you require the growl library.
+
== Normaized Icons
The :icon option key is automatically normalized when you use any of the Growl#notify
methods, this removes the need to explicitly use :appIcon, :image, etc.
notify 'App icon', :icon => :Safari
- notify 'Jpeg icon', :icon => :jpeg
- notify 'Image icon', :icon => 'path/to/image.png'
+ notify 'Jpeg icon', :icon => :jpeg
+ notify 'Image icon', :icon => 'path/to/image.png'
notify 'Icns icon', :icon => 'path/to/icon.icns'
- notify 'Path extname icon', :icon => 'foo.rb'
+ notify 'Path extname icon', :icon => 'foo.rb'
== Features
* Check availability with Growl.installed?
* Check dependencies with Growl.version
* Use images, icon paths, application icons, or file format icons
-* Sticky a notification making it appear until explicitly closed
-* Set notification priority
+* Sticky a notification making it appear until explicitly closed
+* Set notification priority
* Convenience methods such as notify_ok, notify_warning, notify_error, and notify_info
* Etc (consult growlnotify --help)
== Installing growlnotify
-Visit http://growl.info/documentation.php for installation help.
+Visit http://growl.info/documentation.php for installation help.
== Known Issues
View
@@ -2,18 +2,23 @@
$:.unshift File.dirname(__FILE__) + '/../lib'
require 'growl'
+Growl.bin_path = ARGV[0] unless ARGV.empty?
+
+puts "using growlnotify located at #{Growl.bin_path}" unless ARGV.empty?
+
puts "growlnotify version #{Growl.version} installed"
imagepath = File.dirname(__FILE__) + '/../spec/fixtures/image.png'
iconpath = File.dirname(__FILE__) + '/../spec/fixtures/icon.icns'
include Growl
+
notify_info 'New email received', :sticky => true, :title => 'Some app'; sleep 0.2
notify_ok 'Deployment complete' ; sleep 0.2
notify_error 'Deployment failure' ; sleep 0.2
notify 'Safari icon', :icon => :Safari ; sleep 0.2
notify 'Jpeg icon', :icon => :jpeg ; sleep 0.2
notify 'Image icon', :icon => imagepath ; sleep 0.2
notify 'Icns icon', :icon => iconpath ; sleep 0.2
-notify 'Path extname icon', :icon => 'foo.rb' ; sleep 0.2
+notify 'Path extname icon', :icon => 'foo.rb' ; sleep 0.2
View
@@ -1,16 +1,16 @@
module Growl
-
- BIN = 'growlnotify'
-
+
+ @@path = 'growlnotify'
+
#--
# Exceptions
#++
-
+
class Error < StandardError; end
-
+
##
- # Display a growl notification +message+, with +options+
+ # Display a growl notification +message+, with +options+
# documented below. Alternatively a +block+ may be passed
# which is then instance evaluated or yielded to the block.
#
@@ -20,25 +20,25 @@ class Error < StandardError; end
# and your user.
#
# === Examples
- #
+ #
# Growl.notify 'Hello'
# Growl.notify 'Hello', :title => 'TJ Says:', :sticky => true
# Growl.notify { |n| n.message = 'Hello'; n.sticky! }
# Growl.notify { self.message = 'Hello'; sticky! }
#
-
+
def notify message = nil, options = {}, &block
return unless Growl.installed?
options.merge! :message => message if message
Growl.normalize_icon! options
Growl.new(options, &block).run
end
module_function :notify
-
+
#--
# Generate notify_STATUS methods.
#++
-
+
%w( ok info warning error ).each do |type|
define_method :"notify_#{type}" do |message, *args|
options = args.first || {}
@@ -47,36 +47,52 @@ def notify message = nil, options = {}, &block
end
module_function :"notify_#{type}"
end
-
+
##
# Execute +args+ against the binary.
-
+
def self.exec *args
- Kernel.system BIN, *args
+ Kernel.system @@path, *args
end
-
+
+ ##
+ # Specify +path+ to the growlnotify binary
+ # useful if you're bundling it with your application
+ # and don't want to force your user to install
+ # growlnotify package
+
+ def bin_path= path
+ @@path = File.expand_path path
+ end
+ module_function :bin_path=
+
+ def bin_path
+ @@path
+ end
+ module_function :bin_path
+
##
# Return the version triple of the binary.
-
+
def self.version
- @version ||= `#{BIN} --version`.split[1]
+ @version ||= `#{@@path} --version`.split[1]
end
-
+
##
# Check if the binary is installed and accessable.
-
+
def self.installed?
version rescue false
end
-
+
##
# Return an instance of Growl::Base or nil when not installed.
-
+
def self.new *args, &block
return unless installed?
Base.new *args, &block
end
-
+
##
# Normalize the icon option in +options+. This performs
# the following operations in order to allow for the :icon
@@ -87,7 +103,7 @@ def self.new *args, &block
# * capitalized word sets :appIcon
# * filename uses extname as :icon
# * otherwise treated as :icon
-
+
def self.normalize_icon! options = {}
return unless options.include? :icon
icon = options.delete(:icon).to_s
@@ -107,18 +123,18 @@ def self.normalize_icon! options = {}
end
end
end
-
+
#--
# Growl base
#++
-
+
class Base
attr_reader :args
-
+
##
# Initialize with optional +block+, which is then
# instance evaled or yielded depending on the blocks arity.
-
+
def initialize options = {}, &block
@args = []
if block_given?
@@ -133,10 +149,10 @@ def initialize options = {}, &block
end
end
end
-
+
##
# Run the notification, only --message is required.
-
+
def run
raise Error, 'message required' unless message
self.class.switches.each do |switch|
@@ -147,7 +163,7 @@ def run
end
Growl.exec *args
end
-
+
##
# Define a switch +name+.
#
@@ -160,26 +176,26 @@ def run
# @growl.sticky = false # => false
# @growl.sticky? # => false
#
-
+
def self.switch name
ivar = :"@#{name}"
(@switches ||= []) << name
attr_accessor :"#{name}"
define_method(:"#{name}?") { instance_variable_get(ivar) }
define_method(:"#{name}!") { instance_variable_set(ivar, true) }
end
-
+
##
# Return array of available switch symbols.
-
+
def self.switches
@switches
end
-
+
#--
# Switches
#++
-
+
switch :title
switch :message
switch :sticky
@@ -199,4 +215,4 @@ def self.switches
end
-end
+end