Sha256: b70808d3cf5fcc6a356cf3d7c390663d229f45998e9ea118db85b0bcafe15b7b

Contents?: true

Size: 1.03 KB

Versions: 4

Compression:

Stored size: 1.03 KB

Contents

module Qa::Authorities
  module Local
    class Registry
      def initialize
        @hash = {}
        yield self if block_given?
      end

      def keys
        @hash.keys
      end

      def instance_for(key)
        fetch(key).instance
      end

      def fetch(key)
        @hash.fetch(key)
      end

      def self.logger
        @logger ||= begin
          ::Rails.logger if defined? Rails and Rails.respond_to? :logger
        end
      end

      def self.logger= logger
        @logger = logger
      end

      def add(subauthority, class_name)
        Registry.logger.debug "Registering Local QA authority: #{subauthority} - #{class_name}"
        @hash[subauthority] = RegistryEntry.new(subauthority, class_name)
      end


      class RegistryEntry
        def initialize(subauthority, class_name)
          @subauthority, @class_name = subauthority, class_name
        end

        def klass
          @class_name.constantize
        end

        def instance
          klass.new(@subauthority)
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
qa-0.10.1 lib/qa/authorities/local/registry.rb
qa-0.10.0 lib/qa/authorities/local/registry.rb
qa-0.9.0 lib/qa/authorities/local/registry.rb
qa-0.8.0 lib/qa/authorities/local/registry.rb