Sha256: 1d7953d81d8b4a9963f2641976adbbb67a3fa0055f3064ee3667cc8ca35df107

Contents?: true

Size: 1.55 KB

Versions: 7

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

class Serega
  # Plugins are stored here
  module SeregaPlugins
    @plugins = {}

    class << self
      #
      # Registers given plugin to be able to load it using symbol name.
      #
      # @param name [Symbol] Plugin name
      # @param mod [Module] Plugin module
      #
      # @example Register plugin
      #   Serega::SeregaPlugins.register_plugin(:plugin_name, PluginModule)
      #
      # @return [void]
      #
      def register_plugin(name, mod)
        @plugins[name] = mod
      end

      #
      # Loads plugin code and returns plugin core module.
      #
      # @param name [Symbol, Module] plugin name or plugin itself
      #
      # @raise [SeregaError] Raises SeregaError when plugin was not found
      #
      # @example Find plugin when providing name
      #   Serega::SeregaPlugins.find_plugin(:presenter) # => Serega::SeregaPlugins::Presenter
      #
      # @example Find plugin when providing plugin itself
      #   Serega::SeregaPlugins.find_plugin(Presenter) # => Presenter
      #
      # @return [Class<Module>] Plugin core module
      #
      def find_plugin(name)
        return name if name.is_a?(Module)
        return @plugins[name] if @plugins.key?(name)

        require_plugin(name)

        @plugins[name] || raise(SeregaError, "Plugin '#{name}' did not register itself correctly")
      end

      private

      def require_plugin(name)
        require "serega/plugins/#{name}/#{name}"
      rescue LoadError
        raise SeregaError, "Plugin '#{name}' does not exist"
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
serega-0.10.0 lib/serega/plugins.rb
serega-0.9.0 lib/serega/plugins.rb
serega-0.8.3 lib/serega/plugins.rb
serega-0.8.2 lib/serega/plugins.rb
serega-0.8.1 lib/serega/plugins.rb
serega-0.8.0 lib/serega/plugins.rb
serega-0.7.0 lib/serega/plugins.rb