Sha256: 3828bf4bc211aaae2df0cc385ceee3880220487444f38a5e1f766e01effb0c76

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

require 'industrialist/type'
require 'industrialist/warning_helper'

module Industrialist
  class Registrar
    DEFAULT_KEY = :__manufacturable_default__
    REDEFINED_KEY_WARNING_MESSAGE = 'warning: overriding a previously registered class'.freeze
    REDEFINED_DEFAULT_WARNING_MESSAGE = 'warning: overriding a previously registered default class'.freeze

    class << self
      def register(type, key, klass)
        WarningHelper.warning(REDEFINED_KEY_WARNING_MESSAGE) if overriding?(type, key, klass)

        registry[type][key] = klass
      end

      def register_default(type, klass)
        WarningHelper.warning(REDEFINED_DEFAULT_WARNING_MESSAGE) if overriding?(type, DEFAULT_KEY, klass)

        registry[type][DEFAULT_KEY] = klass
      end

      def value_for(type, key)
        return unless registry.key?(type)

        registry[type][key] || registry[type][DEFAULT_KEY]
      end

      def registered_types
        registry.keys
      end

      def registered_keys(type)
        registry[type].keys
      end

      private

      def overriding?(type, key, klass)
        !registry[type][key].nil? && registry[type][key].name != klass.name
      end

      def registry
        @registry ||= Hash.new { |hash, key| hash[key] = {} }
      end
    end
  end

  def self.registered_types
    Registrar.registered_types
  end

  def self.registered_keys(type)
    Registrar.registered_keys(type)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
industrialist-1.0.0 lib/industrialist/registrar.rb