Browse files

Adds the ability to specify path to growlnotify binary

  • Loading branch information...
1 parent c2b8a18 commit cf125fd887980c374df12fa2c16538e1c49120e2 @lukaszkorecki committed Nov 10, 2010
Showing with 57 additions and 36 deletions.
  1. +6 −1 examples/growl.rb
  2. +51 −35 lib/growl/growl.rb
View
7 examples/growl.rb
@@ -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
86 lib/growl/growl.rb
@@ -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

0 comments on commit cf125fd

Please sign in to comment.