Sha256: 285aa2f3dbc33a67779b2308a92bb1365e5b93bc36be2d531347b61fb0dce0f2

Contents?: true

Size: 1.45 KB

Versions: 8

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

class Serega
  # Module in which all Serega plugins should be stored
  module SeregaPlugins
    @plugins = {}

    class << self
      #
      # Registers given plugin to be able to load it using symbol name.
      #
      # @example Register plugin
      #   Serega::SeregaPlugins.register_plugin(:plugin_name, PluginModule)
      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

8 entries across 8 versions & 1 rubygems

Version Path
serega-0.6.1 lib/serega/plugins.rb
serega-0.6.0 lib/serega/plugins.rb
serega-0.5.2 lib/serega/plugins.rb
serega-0.5.1 lib/serega/plugins.rb
serega-0.5.0 lib/serega/plugins.rb
serega-0.4.0 lib/serega/plugins.rb
serega-0.3.0 lib/serega/plugins.rb
serega-0.2.0 lib/serega/plugins.rb