Class: Emfsvg::Translation::HandlerRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/emfsvg/translation/handler_registry.rb

Overview

Maps SVG element class -> handler class. Handlers implement .call(element, context) which appends records to the context's emitter.

OCP: adding a new SVG element type means writing a new handler class and registering it. No existing handler code changes.

Instance Method Summary collapse

Constructor Details

#initializeHandlerRegistry

Returns a new instance of HandlerRegistry.



12
13
14
# File 'lib/emfsvg/translation/handler_registry.rb', line 12

def initialize
  @handlers = {}
end

Instance Method Details

#handler_for(element) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/emfsvg/translation/handler_registry.rb', line 20

def handler_for(element)
  klass = element.class
  while klass
    return @handlers[klass] if @handlers.key?(klass)

    klass = klass.superclass
  end
  nil
end

#register(element_class, handler) ⇒ Object



16
17
18
# File 'lib/emfsvg/translation/handler_registry.rb', line 16

def register(element_class, handler)
  @handlers[element_class] = handler
end

#translate(element, context) ⇒ Object



30
31
32
33
34
35
# File 'lib/emfsvg/translation/handler_registry.rb', line 30

def translate(element, context)
  handler = handler_for(element)
  return unless handler

  handler.call(element, context)
end