Sha256: c75a75901a7782343564d153316398e8e9ec4288963c218fc4036fbddb0b1176

Contents?: true

Size: 1.77 KB

Versions: 8

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

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.
    #
    # @macro param_name
    # @param plugin [Vedeu::Plugin]
    # @return [Array<void>]
    def register(name, plugin = false)
      Vedeu.log(type:    :debug,
                message: "Attempting to register plugin: #{name}")

      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}/

        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.
    #
    # @macro param_name
    # @return [Boolean]
    def not_loaded?(name)
      plugins.empty? || plugins.any? { |plugin| plugin.gem_name != name }
    end

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

  end # Plugins

end # Vedeu

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
vedeu-0.8.23 lib/vedeu/plugins/plugins.rb
vedeu-0.8.22 lib/vedeu/plugins/plugins.rb
vedeu-0.8.21 lib/vedeu/plugins/plugins.rb
vedeu-0.8.20 lib/vedeu/plugins/plugins.rb
vedeu-0.8.19 lib/vedeu/plugins/plugins.rb
vedeu-0.8.18 lib/vedeu/plugins/plugins.rb
vedeu-0.8.17 lib/vedeu/plugins/plugins.rb
vedeu-0.8.16 lib/vedeu/plugins/plugins.rb