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 ``` bash
$ guard --notify false $ 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` 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 Guard
module Notifier module Notifier
@enabled = false
def self.turn_off def self.turn_off
ENV["GUARD_NOTIFY"] = 'false' @enabled = false
end end
def self.turn_on def self.turn_on
ENV["GUARD_NOTIFY"] = 'true' @enabled = true
case Config::CONFIG['target_os'] case Config::CONFIG['target_os']
when /darwin/i when /darwin/i
require_growl require_growl
@ -19,7 +20,7 @@ module Guard
end end
def self.notify(message, options = {}) def self.notify(message, options = {})
unless disabled? if enabled?
image = options[:image] || :success image = options[:image] || :success
title = options[:title] || "Guard" title = options[:title] || "Guard"
case Config::CONFIG['target_os'] case Config::CONFIG['target_os']
@ -31,8 +32,8 @@ module Guard
end end
end end
def self.disabled? def self.enabled?
ENV["GUARD_NOTIFY"] == 'false' @enabled
end end
private private

View File

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

View File

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

View File

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