From aba38ea9e82dbaf1ff12e6914fb668fe5aa84c78 Mon Sep 17 00:00:00 2001 From: John Mair Date: Fri, 2 Sep 2011 04:16:43 +1200 Subject: [PATCH] refactored plugin code: 1. should not warn when activate called twice on a gem that has already been activated (but ceases to be accessible due to bundler, robgleeson's bug). 2. calling activate on a non-existent plugin should warn not raise --- lib/pry/command_processor.rb | 1 - lib/pry/plugins.rb | 27 +++++++++++++++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/pry/command_processor.rb b/lib/pry/command_processor.rb index fa30ac7b..4e5c9c5b 100644 --- a/lib/pry/command_processor.rb +++ b/lib/pry/command_processor.rb @@ -126,7 +126,6 @@ class Pry ret = commands.run_command(context, command, *args) - # Tick, tock, im getting rid of this shit soon. options[:val].replace("") ret diff --git a/lib/pry/plugins.rb b/lib/pry/plugins.rb index 5f782e47..5151fc8c 100644 --- a/lib/pry/plugins.rb +++ b/lib/pry/plugins.rb @@ -1,9 +1,17 @@ class Pry class PluginManager PRY_PLUGIN_PREFIX = /^pry-/ - PluginNotFound = Class.new(LoadError) - MessageSink = Object.new.tap { |o| def o.method_missing(*args) end } + # Placeholder when no associated gem found, displays warning + class NoPlugin + def initialize(name) + @name = name + end + + def method_missing(*args) + $stderr.puts "Warning: The plugin '#{@name}' was not found! (no gem found)" + end + end class Plugin attr_accessor :name, :gem_name, :enabled, :spec, :active @@ -12,22 +20,25 @@ class Pry @name, @gem_name, @enabled, @spec = name, gem_name, enabled, spec end - # Disable a plugin. + # Disable a plugin. (prevents plugin from being loaded, cannot + # disable an already activated plugin) def disable! self.enabled = false end - # Enable a plugin. + # Enable a plugin. (does not load it immediately but puts on + # 'white list' to be loaded) def enable! self.enabled = true end - # Activate the plugin (require the gem). + # Activate the plugin (require the gem - enables/loads the + # plugin immediately at point of call, even if plugin is disabled) def activate! begin - require gem_name + require gem_name if !active? rescue LoadError - $stderr.puts "Warning: The plugin '#{gem_name}' was not found!" + $stderr.puts "Warning: The plugin '#{gem_name}' was not found! (gem found but could not be loaded)" end self.active = true self.enabled = true @@ -55,7 +66,7 @@ class Pry # @return [Hash] A hash with all plugin names (minus the 'pry-') as # keys and Plugin objects as values. def plugins - h = Pry.config.plugins.strict_loading ? {} : Hash.new { MessageSink } + h = Hash.new { |_, key| NoPlugin.new(key) } @plugins.each do |plugin| h[plugin.name] = plugin end