From 747f463363eac59520d0e0d72e2fc96c48b8343d Mon Sep 17 00:00:00 2001 From: Cezary Baginski Date: Sat, 6 Dec 2014 14:51:02 +0100 Subject: [PATCH] allow requiring from Gemfile to work --- README.md | 20 +++++++++++++++++--- lib/guard/compat/plugin.rb | 11 +++++++++++ lib/guard/compat/test/helper.rb | 3 +++ 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 lib/guard/compat/plugin.rb diff --git a/README.md b/README.md index 60bab59..89e63a6 100644 --- a/README.md +++ b/README.md @@ -18,13 +18,27 @@ And then execute: ## Usage -Put the following notes in your plugin file (e.g. `lib/guard/myplugin.rb`): +Put the following in your plugin files (e.g. `lib/guard/myplugin.rb`): ```ruby -# Do NOT require "guard/plugin" - it should already required -# by Guard or by the test helper +# Don't require "guard/plugin" in hits files +require 'guard/compat/plugin' + +module Guard + class MyPlugin < Plugin + # (...) + end +end + ``` +### IMPORTANT + +1) Do not include *any* files from Guard directly (if you need something from Guard which Guard::Compat doesn't provide, file an issue) +2) include 'guard/compat/plugin' is *all* your files which use `Guard::Plugin` +3) make sure you include the `< Plugin` part in *every* file which add classes or methods to your plugin class (important if your plugin consists of multiple files/sub class) + + And in your plugin tests (e.g. `spec/lib/guard/myplugin_spec.rb`): ```ruby diff --git a/lib/guard/compat/plugin.rb b/lib/guard/compat/plugin.rb new file mode 100644 index 0000000..21be031 --- /dev/null +++ b/lib/guard/compat/plugin.rb @@ -0,0 +1,11 @@ +unless Object.const_defined?('Guard::Plugin') + # Provided empty definition so requiring the plugin without Guard won't crash + # (e.g. when added to a Gemfile without `require: false`) + module Guard + class Plugin + def initialize + fail NotImplementedError + end + end + end +end diff --git a/lib/guard/compat/test/helper.rb b/lib/guard/compat/test/helper.rb index d8dbf62..689a38a 100644 --- a/lib/guard/compat/test/helper.rb +++ b/lib/guard/compat/test/helper.rb @@ -1,4 +1,7 @@ # Minimal stub allowing a plugin to work + +require 'guard/compat/plugin' + module Guard class Plugin attr_reader :options