Class: Mutant::Reporter::CLI::Registry

Inherits:
Module
  • Object
show all
Defined in:
lib/mutant/reporter/cli/registry.rb

Overview

Mixin to generate registry semantics

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.newRegistry

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return new registry

Returns:



14
15
16
# File 'lib/mutant/reporter/cli/registry.rb', line 14

def self.new
  super({})
end

Instance Method Details

#handle(subject, handler) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Register handler for class

Parameters:

  • klass (Class)

Returns:

  • (self)


26
27
28
29
30
# File 'lib/mutant/reporter/cli/registry.rb', line 26

def handle(subject, handler)
  raise "Duplicate registration of #{subject}" if registry.key?(subject)
  registry[subject] = handler
  self
end

#included(host) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Hook called when module is included

Parameters:

  • host (Class, Module)

Returns:

  • (undefined)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mutant/reporter/cli/registry.rb', line 63

def included(host)
  super

  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

#lookup(subject) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Lookup handler

Parameters:

  • subject (Class)

Returns:

  • (Object)

    if found

Raises:

  • (RuntimeError)

    otherwise



44
45
46
47
48
49
50
51
52
53
# File 'lib/mutant/reporter/cli/registry.rb', line 44

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