Sha256: 05b34f2126d73728538dddc079bf80d0f30273ae6a817f46e83e98b73c41ebf1

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

module Mutant
  class Reporter
    class CLI
      # Mixin to generate registry semantics
      class Registry < Module
        include Concord.new(:registry)

        # Return new registry
        #
        # @return [Registry]
        #
        # @api private
        #
        def self.new
          super({})
        end

        # Register handler for class
        #
        # @param [Class] klass
        #
        # @return [self]
        #
        # @api private
        #
        def handle(subject, handler)
          raise "Duplicate registration of #{subject}" if registry.key?(subject)
          registry[subject] = handler
          self
        end

        # Lookup handler
        #
        # @param [Class] subject
        #
        # @return [Object]
        #   if found
        #
        # @raise [RuntimeError]
        #   otherwise
        #
        # @api private
        #
        def lookup(subject)
          current = subject
          until current.equal?(Object)
            if registry.key?(current)
              return registry.fetch(current)
            end
            current = current.superclass
          end
          raise "No printer for: #{subject}"
        end

        # Hook called when module is included
        #
        # @param [Class,Module] host
        #
        # @return [undefined]
        #
        # @api private
        #
        def included(host)
          object = self
          host.class_eval do
            define_singleton_method(:lookup, &object.method(:lookup))
            private_class_method :lookup

            define_singleton_method(:handle) do |subject|
              object.handle(subject, self)
            end
            private_class_method :handle
          end
        end

      end # Registry
    end # CLI
  end # Reporter
end # Mutant

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mutant-0.5.24 lib/mutant/reporter/cli/registry.rb