Sha256: 4a06c3b5abbe20ecd298309219839831297de491b0aaba0b0538c9beed21c143

Contents?: true

Size: 808 Bytes

Versions: 7

Compression:

Stored size: 808 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

    def initialize
      @types = {}
    end

    # @type 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

7 entries across 7 versions & 1 rubygems

Version Path
yao-0.13.4 lib/yao/plugins/registry.rb
yao-0.13.3 lib/yao/plugins/registry.rb
yao-0.13.2 lib/yao/plugins/registry.rb
yao-0.13.1 lib/yao/plugins/registry.rb
yao-0.13.0 lib/yao/plugins/registry.rb
yao-0.12.0 lib/yao/plugins/registry.rb
yao-0.11.3 lib/yao/plugins/registry.rb