Sha256: dc61871f1e8ad770d8272496af5d77a3f319fcbafc3e903aeb73fb68742506f5

Contents?: true

Size: 830 Bytes

Versions: 8

Compression:

Stored size: 830 Bytes

Contents

require 'singleton'

# Regstiry Pattern - https://www.martinfowler.com/eaaCatalog/registry.html
# A well-known object that other objects can use to find common objects and services.
module Yao::Plugins
  class Registry
    include Singleton

    # @return [Hash]
    def initialize
      @types = {}
    end

    # @param type [Symbol]
    # @return [Object]
    def [](type)
      @types[type]
    end

    # @param klass [*]
    # @param type [Symbol]
    # @param name [Symbol]
    def register(klass, type: nil, name: :default)
      raise("Plugin registration requires both type and name.") if !type or !name
      @types[type] ||= {}
      @types[type][name] = klass
    end
  end

  # @param [*]
  # @param [Symbol]
  # @param [Symbol]
  def self.register(klass, **kw)
    Registry.instance.register(klass, **kw)
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
yao-0.21.0 lib/yao/plugins/registry.rb
yao-0.20.0 lib/yao/plugins/registry.rb
yao-0.19.0 lib/yao/plugins/registry.rb
yao-0.18.0 lib/yao/plugins/registry.rb
yao-0.17.0 lib/yao/plugins/registry.rb
yao-0.16.0 lib/yao/plugins/registry.rb
yao-0.15.0 lib/yao/plugins/registry.rb
yao-0.14.0 lib/yao/plugins/registry.rb