Class: Coradoc::AsciiDoc::Transform::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/coradoc/asciidoc/transform/registry.rb

Overview

Registry for model transformers

Provides a flexible, extensible way to register and lookup transformers that convert between different model types. This replaces case statements with a registry pattern, following the Open/Closed Principle.

Examples:

Register a transformer

Coradoc::AsciiDoc::Transform::Registry.register(
  Coradoc::AsciiDoc::Model::Document,
  ->(model) { transform_document(model) }
)

Lookup and apply a transformer

transformer = Coradoc::AsciiDoc::Transform::Registry.lookup(model.class)
result = transformer.call(model) if transformer

Class Method Summary collapse

Class Method Details

.clearvoid

This method returns an undefined value.

Clear all registrations (useful for testing)



125
126
127
128
# File 'lib/coradoc/asciidoc/transform/registry.rb', line 125

def clear
  registry.clear
  @prioritized_registry = nil
end

.lookup(model_class) ⇒ #call?

Lookup a transformer for a model

First checks exact class match, then checks prioritized registry, then walks up the inheritance chain.

Parameters:

  • model_class (Class)

    the model class to find a transformer for

Returns:

  • (#call, nil)

    the transformer or nil if not found



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/coradoc/asciidoc/transform/registry.rb', line 73

def lookup(model_class)
  # 1. Check exact match in main registry
  return registry[model_class] if registry.key?(model_class)

  # 2. Check prioritized registry
  if @prioritized_registry
    entry = @prioritized_registry.find { |e| model_class <= e[:class] }
    return entry[:transformer] if entry
  end

  # 3. Walk up inheritance chain
  model_class.ancestors.each do |ancestor|
    next if ancestor == model_class
    next if [Object, BasicObject].include?(ancestor)

    return registry[ancestor] if registry.key?(ancestor)
  end

  nil
end

.register(source_class, transformer, target_class: nil) ⇒ void

This method returns an undefined value.

Register a transformer for a source class

Parameters:

  • source_class (Class)

    the source model class

  • transformer (#call)

    a callable that transforms the model

  • target_class (Class, nil) (defaults to: nil)

    optional target class for bidirectional lookup



37
38
39
40
41
42
43
44
# File 'lib/coradoc/asciidoc/transform/registry.rb', line 37

def register(source_class, transformer, target_class: nil)
  registry[source_class] = transformer

  # Store reverse mapping if target class specified
  return unless target_class

  reverse_registry[target_class] = transformer
end

.register_with_priority(source_class, transformer, priority: 0) ⇒ void

This method returns an undefined value.

Register a transformer with a priority

Higher priority transformers are checked first. This is useful for handling subclasses before parent classes.

Parameters:

  • source_class (Class)

    the source model class

  • transformer (#call)

    a callable that transforms the model

  • priority (Integer) (defaults to: 0)

    priority level (higher = checked first)



55
56
57
58
59
60
61
62
63
64
# File 'lib/coradoc/asciidoc/transform/registry.rb', line 55

def register_with_priority(source_class, transformer, priority: 0)
  @prioritized_registry ||= []
  @prioritized_registry << {
    class: source_class,
    transformer: transformer,
    priority: priority
  }
  # Sort by priority descending
  @prioritized_registry.sort_by! { |e| -e[:priority] }
end

.registered?(model_class) ⇒ Boolean

Check if a transformer is registered for a class

Parameters:

  • model_class (Class)

    the class to check

Returns:

  • (Boolean)


118
119
120
# File 'lib/coradoc/asciidoc/transform/registry.rb', line 118

def registered?(model_class)
  !lookup(model_class).nil?
end

.registered_classesArray<Class>

Get all registered source classes

Returns:

  • (Array<Class>)


133
134
135
# File 'lib/coradoc/asciidoc/transform/registry.rb', line 133

def registered_classes
  registry.keys
end

.registryHash

Get the global registry instance

Returns:

  • (Hash)

    the registry hash



27
28
29
# File 'lib/coradoc/asciidoc/transform/registry.rb', line 27

def registry
  @registry ||= {}
end

.transform(model) ⇒ Object

Transform a model using the registered transformer

Parameters:

  • model (Object)

    the model to transform

Returns:

  • (Object)

    the transformed model

Raises:

  • (ArgumentError)

    if no transformer is registered



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/coradoc/asciidoc/transform/registry.rb', line 99

def transform(model)
  return model if model.nil?

  # Handle arrays specially
  return model.map { |item| transform(item) } if model.is_a?(Array)

  transformer = lookup(model.class)
  if transformer
    transformer.call(model)
  else
    # Return unchanged if no transformer found
    model
  end
end