Sha256: 0fac331a2d10594a6a30d8d18490e5e7158a878b86d3f28cb54f31426b473f2c

Contents?: true

Size: 1.94 KB

Versions: 12

Compression:

Stored size: 1.94 KB

Contents

class Pry
  class PluginManager
    PRY_PLUGIN_PREFIX = /^pry-/
    PluginNotFound = Class.new(LoadError)

    MessageSink = Object.new.tap { |o| def o.method_missing(*args) end }

    class Plugin
      attr_accessor :name, :gem_name, :enabled, :active

      def initialize(name, gem_name, enabled)
        @name, @gem_name, @enabled = name, gem_name, enabled
      end

      # Disable a plugin.
      def disable!
        self.enabled = false
      end

      # Enable a plugin.
      def enable!
        self.enabled = true
      end

      # Activate the plugin (require the gem).
      def activate!
        begin
          require gem_name
        rescue LoadError
          raise PluginNotFound, "The plugin '#{gem_name}' was not found!"
        end
        self.active = true
        self.enabled = true
      end

      alias active? active
      alias enabled? enabled
    end

    def initialize
      @plugins = []
    end

    # Find all installed Pry plugins and store them in an internal array.
    def locate_plugins
      Gem.refresh
      (Gem::Specification.respond_to?(:each) ? Gem::Specification : Gem.source_index.find_name('')).each do |gem|
        next if gem.name !~ PRY_PLUGIN_PREFIX
        plugin_name = gem.name.split('-', 2).last
        @plugins << Plugin.new(plugin_name, gem.name, true) if !gem_located?(gem.name)
      end
      @plugins
    end

    # @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 }
      @plugins.each do |plugin|
        h[plugin.name] = plugin
      end
      h
    end

    # Require all enabled plugins, disabled plugins are skipped.
    def load_plugins
      @plugins.each do |plugin|
        plugin.activate! if plugin.enabled?
      end
    end

    private
    def gem_located?(gem_name)
      @plugins.any? { |plugin| plugin.gem_name == gem_name }
    end
  end

end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
pry-0.9.0pre3 lib/pry/plugins.rb
pry-0.9.0pre3-i386-mswin32 lib/pry/plugins.rb
pry-0.9.0pre3-i386-mingw32 lib/pry/plugins.rb
pry-0.9.0pre3-java lib/pry/plugins.rb
pry-0.9.0pre2 lib/pry/plugins.rb
pry-0.9.0pre2-i386-mswin32 lib/pry/plugins.rb
pry-0.9.0pre2-i386-mingw32 lib/pry/plugins.rb
pry-0.9.0pre2-java lib/pry/plugins.rb
pry-0.9.0pre1 lib/pry/plugins.rb
pry-0.9.0pre1-i386-mswin32 lib/pry/plugins.rb
pry-0.9.0pre1-i386-mingw32 lib/pry/plugins.rb
pry-0.9.0pre1-java lib/pry/plugins.rb