Refactorized notifier enabling/disabling

This commit is contained in:
Thibaud Guillaume-Gentil 2011-05-10 21:22:25 +02:00
parent 0dcf13d77c
commit f424854e61
5 changed files with 12 additions and 12 deletions

View File

@ -117,7 +117,7 @@ Notifications (growl/libnotify) can be disabled with:
``` bash
$ guard --notify false
$ guard -n false # shortcut
$ guard -n f # shortcut
```
Notifications can also be disabled by setting a `GUARD_NOTIFY` environment variable to `false`

View File

@ -3,13 +3,14 @@ require 'pathname'
module Guard
module Notifier
@enabled = false
def self.turn_off
ENV["GUARD_NOTIFY"] = 'false'
@enabled = false
end
def self.turn_on
ENV["GUARD_NOTIFY"] = 'true'
@enabled = true
case Config::CONFIG['target_os']
when /darwin/i
require_growl
@ -19,7 +20,7 @@ module Guard
end
def self.notify(message, options = {})
unless disabled?
if enabled?
image = options[:image] || :success
title = options[:title] || "Guard"
case Config::CONFIG['target_os']
@ -31,8 +32,8 @@ module Guard
end
end
def self.disabled?
ENV["GUARD_NOTIFY"] == 'false'
def self.enabled?
@enabled
end
private

View File

@ -5,6 +5,7 @@ describe Guard::Notifier do
describe ".notify" do
before(:each) { subject.turn_on }
after(:each) { subject.turn_off }
if mac?
if growl_installed?
@ -17,7 +18,7 @@ describe Guard::Notifier do
subject.notify 'great', :title => 'Guard'
end
else
it { should be_disabled }
it { should_not be_enabled }
end
end
@ -32,14 +33,12 @@ describe Guard::Notifier do
subject.notify 'great', :title => 'Guard'
end
else
it { should be_disabled }
it { should_not be_enabled }
end
end
end
describe ".turn_off" do
before(:each) { subject.turn_off }
if mac? && growl_installed?
it "does nothing" do
Growl.should_not_receive(:notify)
@ -52,7 +51,7 @@ describe Guard::Notifier do
end
end
it { should be_disabled }
it { should_not be_enabled }
end
end

View File

@ -35,6 +35,7 @@ describe Guard do
end
it "should turn off notifier if env[GUARD_NOTIFY] is false" do
ENV["GUARD_NOTIFY"] = 'false'
::Guard::Notifier.should_receive(:turn_off)
::Guard.setup(:notify => true)
end

View File

@ -15,7 +15,6 @@ RSpec.configure do |config|
config.run_all_when_everything_filtered = true
config.before(:each) do
Guard::Notifier.turn_off
@fixture_path = Pathname.new(File.expand_path('../fixtures/', __FILE__))
end