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

This commit is contained in:
John Mair 2011-09-02 04:16:43 +12:00
parent be77470bd9
commit aba38ea9e8
2 changed files with 19 additions and 9 deletions

View File

@ -126,7 +126,6 @@ class Pry
ret = commands.run_command(context, command, *args) ret = commands.run_command(context, command, *args)
# Tick, tock, im getting rid of this shit soon.
options[:val].replace("") options[:val].replace("")
ret ret

View File

@ -1,9 +1,17 @@
class Pry class Pry
class PluginManager class PluginManager
PRY_PLUGIN_PREFIX = /^pry-/ 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 class Plugin
attr_accessor :name, :gem_name, :enabled, :spec, :active attr_accessor :name, :gem_name, :enabled, :spec, :active
@ -12,22 +20,25 @@ class Pry
@name, @gem_name, @enabled, @spec = name, gem_name, enabled, spec @name, @gem_name, @enabled, @spec = name, gem_name, enabled, spec
end end
# Disable a plugin. # Disable a plugin. (prevents plugin from being loaded, cannot
# disable an already activated plugin)
def disable! def disable!
self.enabled = false self.enabled = false
end end
# Enable a plugin. # Enable a plugin. (does not load it immediately but puts on
# 'white list' to be loaded)
def enable! def enable!
self.enabled = true self.enabled = true
end 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! def activate!
begin begin
require gem_name require gem_name if !active?
rescue LoadError 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 end
self.active = true self.active = true
self.enabled = true self.enabled = true
@ -55,7 +66,7 @@ class Pry
# @return [Hash] A hash with all plugin names (minus the 'pry-') as # @return [Hash] A hash with all plugin names (minus the 'pry-') as
# keys and Plugin objects as values. # keys and Plugin objects as values.
def plugins def plugins
h = Pry.config.plugins.strict_loading ? {} : Hash.new { MessageSink } h = Hash.new { |_, key| NoPlugin.new(key) }
@plugins.each do |plugin| @plugins.each do |plugin|
h[plugin.name] = plugin h[plugin.name] = plugin
end end