Sha256: 11b60054cd5c25109e038e13789fa9796d6fe9399e1136564defd71a44a751ec

Contents?: true

Size: 1002 Bytes

Versions: 6

Compression:

Stored size: 1002 Bytes

Contents

# typed: false
module Kuby
  class PluginRegistry
    include Enumerable

    ANY = 'any'.freeze

    def register(plugin_name, plugin_klass, environment: ANY)
      plugins[plugin_name] ||= {}
      plugins[plugin_name][environment] ||= plugin_klass
    end

    def find(plugin_name, environment: Kuby.env)
      plugins_by_env = plugins[plugin_name]

      unless plugins_by_env
        raise Kubernetes::MissingPluginError, "no plugin registered with name #{plugin_name}, "\
          'do you need to add a gem to your Gemfile?'
      end

      plugins_by_env[environment] || plugins_by_env[ANY]
    end

    def each(&block)
      return to_enum(__method__) unless block

      @plugins.each_pair do |plugin_name, plugins_by_env|
        plugins_by_env.each_pair do |env, plugin_klass|
          case env
            when ANY, Kuby.env
              yield plugin_name, plugin_klass
          end
        end
      end
    end

    private

    def plugins
      @plugins ||= {}
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
kuby-core-0.20.2 lib/kuby/plugin_registry.rb
kuby-core-0.20.1 lib/kuby/plugin_registry.rb
kuby-core-0.20.0 lib/kuby/plugin_registry.rb
kuby-core-0.19.0 lib/kuby/plugin_registry.rb
kuby-core-0.18.0 lib/kuby/plugin_registry.rb
kuby-core-0.17.0 lib/kuby/plugin_registry.rb