Sha256: e73cc16c9df08eb4e6e017ff2e1027b6bcc660fc495f36cfa8069c9294b6d89a

Contents?: true

Size: 1.43 KB

Versions: 11

Compression:

Stored size: 1.43 KB

Contents

# encoding: utf-8

module TTY
  # A class responsible for managing plugins installation
  class Plugins
    PLUGIN_PREFIX = 'tty'

    attr_accessor :plugins
    private :plugins

    # Initialize the Plugins
    #
    # @api public
    def initialize
      @plugins = []
    end

    # Load all plugins that are not enabled
    #
    # @api public
    def load
      plugins.each do |plugin|
        plugin.load! unless plugin.enabled?
      end
    end

    # Register plugin with name in internal array
    #
    # @param [String] name
    #
    # @param [TTY::Plugin] plugin
    #
    # @api public
    def register(name, plugin = false)
      if plugin && !loaded?(name)
        plugins << plugin
      end
    end

    # Find all installed TTY plugins and store them
    #
    # @api private
    def find
      Gem.refresh
      Gem::Specification.each do |gem|
        next unless gem.name =~ /^#{PLUGIN_PREFIX}/
        plugin_name = gem.name[/^#{PLUGIN_PREFIX}-(.*)/, 1]
        register(plugin_name, Plugin.new(plugin_name, gem))
      end
      plugins
    end

    # Return a list of all plugin names as strings
    #
    # @api public
    def names
      plugins.reduce({}) do |hash, plugin|
        hash[plugin.name] = plugin
        hash
      end
    end

    private

    # Check if plugin is already loaded
    #
    # @api private
    def loaded?(name)
      plugins.any? { |plugin| plugin.gem_name == name }
    end
  end # PluginManager
end # TTY

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
tty-0.5.0 lib/tty/plugins.rb
tty-0.4.0 lib/tty/plugins.rb
tty-0.3.2 lib/tty/plugins.rb
tty-0.3.1 lib/tty/plugins.rb
tty-0.3.0 lib/tty/plugins.rb
tty-0.2.1 lib/tty/plugins.rb
tty-0.2.0 lib/tty/plugins.rb
tty-0.1.3 lib/tty/plugins.rb
tty-0.1.2 lib/tty/plugins.rb
tty-0.1.1 lib/tty/plugins.rb
tty-0.1.0 lib/tty/plugins.rb