Sha256: 781b9d7887e844cb7cc420a43066ab1b4b5f467db0123be80593b8ba0c405d5d

Contents?: true

Size: 1.77 KB

Versions: 51

Compression:

Stored size: 1.77 KB

Contents

module Vedeu

  # A class responsible for managing plugins installation.
  #
  class Plugins

    # Returns a new instance of Vedeu::Plugins.
    #
    # @return [Vedeu::Plugins]
    def initialize
      @plugins = []
    end

    # Loads all plugins that are not enabled.
    #
    # @return [Array<void>]
    def load
      plugins.each { |plugin| plugin.load! unless plugin.enabled? }
    end

    # Register plugin with name in an internal array.
    #
    # @param name [String]
    # @param plugin [Vedeu::Plugin]
    # @return [Array<void>]
    def register(name, plugin = false)
      Vedeu.log(type:    :debug,
                message: "Attempting to register plugin: #{name}".freeze)

      plugins << plugin if plugin && not_loaded?(name)
    end

    # Find all installed plugins and store them.
    #
    # @return [Array<void>]
    def find
      Gem.refresh

      Gem::Specification.each do |gem|
        next unless gem.name =~ /^#{prefix}/.freeze

        plugin_name = gem.name[/^#{prefix}(.*)/, 1]

        register(plugin_name, Vedeu::Plugin.new(plugin_name, gem))
      end

      plugins
    end

    # Return a list of all plugin names as strings.
    #
    # @return [Hash]
    def names
      collection = {}
      plugins.each_with_object(collection) do |hash, plugin|
        hash[plugin.name] = plugin
        hash
      end
    end

    protected

    # @!attribute [r] input
    # @return [Array<String>]
    attr_accessor :plugins

    private

    # Returns a boolean indicating whether a plugin is already loaded.
    #
    # @param name [String]
    # @return [Boolean]
    def not_loaded?(name)
      plugins.empty? || plugins.any? { |plugin| plugin.gem_name != name }
    end

    # @return [String]
    def prefix
      'vedeu_'.freeze
    end

  end # Plugins

end # Vedeu

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
vedeu-0.8.0 lib/vedeu/plugins/plugins.rb
vedeu-0.7.4 lib/vedeu/plugins/plugins.rb
vedeu-0.7.3 lib/vedeu/plugins/plugins.rb
vedeu-0.7.2 lib/vedeu/plugins/plugins.rb
vedeu-0.7.1 lib/vedeu/plugins/plugins.rb
vedeu-0.7.0 lib/vedeu/plugins/plugins.rb
vedeu-0.6.71 lib/vedeu/plugins/plugins.rb
vedeu-0.6.70 lib/vedeu/plugins/plugins.rb
vedeu-0.6.69 lib/vedeu/plugins/plugins.rb
vedeu-0.6.68 lib/vedeu/plugins/plugins.rb
vedeu-0.6.67 lib/vedeu/plugins/plugins.rb
vedeu-0.6.66 lib/vedeu/plugins/plugins.rb
vedeu-0.6.65 lib/vedeu/plugins/plugins.rb
vedeu-0.6.64 lib/vedeu/plugins/plugins.rb
vedeu-0.6.63 lib/vedeu/plugins/plugins.rb
vedeu-0.6.62 lib/vedeu/plugins/plugins.rb
vedeu-0.6.61 lib/vedeu/plugins/plugins.rb
vedeu-0.6.60 lib/vedeu/plugins/plugins.rb
vedeu-0.6.59 lib/vedeu/plugins/plugins.rb
vedeu-0.6.58 lib/vedeu/plugins/plugins.rb