Class: Coradoc::Dispatch

Inherits:
Object
  • Object
show all
Defined in:
lib/coradoc/dispatch.rb

Overview

Type-keyed handler dispatch.

Replaces the bespoke ‘register/lookup` hashes that recur across gems with one cohesive registry. Each gem that needs type-keyed dispatch configures a single Dispatch instance and exposes its legacy DSL as thin delegates.

Two resolution policies cover the common shapes:

  • ‘Coradoc::Dispatch.strict` — exact key match, raises on miss

  • ‘Coradoc::Dispatch.hierarchical` — walks ancestors on miss, returns nil

Registries that need priority ordering, predicate matching, or lazy loading do not fit this shape and stay as they are; the friction there is genuine semantic difference, not duplicated mechanism.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(walk_ancestors: false, miss: :raise, &default) ⇒ Dispatch

Returns a new instance of Dispatch.



33
34
35
36
37
38
# File 'lib/coradoc/dispatch.rb', line 33

def initialize(walk_ancestors: false, miss: :raise, &default)
  @walk_ancestors = walk_ancestors
  @miss = miss
  @default = default
  @entries = {}
end

Class Method Details

.hierarchicalObject

Walks the key’s class ancestors on miss; returns nil if no entry. Used by registries that want base-class handlers to apply to all subclasses (e.g. Mirror HandlerRegistry).



30
# File 'lib/coradoc/dispatch.rb', line 30

def hierarchical = new(walk_ancestors: true, miss: :return_nil)

.strictObject

Exact key match; raises if no entry. Used by registries that map a concrete type to its sole handler (e.g. AsciiDoc ElementRegistry).



25
# File 'lib/coradoc/dispatch.rb', line 25

def strict = new(walk_ancestors: false)

Instance Method Details

#clear!Object



75
# File 'lib/coradoc/dispatch.rb', line 75

def clear! = @entries.clear

#lookup(key) ⇒ Object

Resolve the handler for a key. Returns nil if no handler matches unless the dispatch is configured to raise.



58
59
60
61
62
63
64
# File 'lib/coradoc/dispatch.rb', line 58

def lookup(key)
  exact = @entries[key]
  return exact if exact
  return walk(key) if @walk_ancestors && key.is_a?(Class)

  apply_default(key)
end

#lookup!(key) ⇒ Object

Resolve the handler, raising Coradoc::Error on miss.



67
68
69
# File 'lib/coradoc/dispatch.rb', line 67

def lookup!(key)
  lookup(key) || raise(Coradoc::Error, "no handler registered for #{key.inspect}")
end

#override(key, handler) ⇒ Object

Replace the handler for an existing key. Returns the previous handler so wrappers can chain: original = dispatch.override(K, Wrapper.new(original))



46
47
48
49
50
# File 'lib/coradoc/dispatch.rb', line 46

def override(key, handler)
  previous = @entries[key]
  @entries[key] = handler
  previous
end

#register(key, handler) ⇒ Object



40
41
42
# File 'lib/coradoc/dispatch.rb', line 40

def register(key, handler)
  @entries[key] = handler
end

#registered?(key) ⇒ Boolean

Returns:

  • (Boolean)


71
# File 'lib/coradoc/dispatch.rb', line 71

def registered?(key) = @entries.key?(key)

#registered_keysObject



73
# File 'lib/coradoc/dispatch.rb', line 73

def registered_keys = @entries.keys

#unregister(key) ⇒ Object



52
53
54
# File 'lib/coradoc/dispatch.rb', line 52

def unregister(key)
  @entries.delete(key)
end