Class: Coradoc::AsciiDoc::Transform::Registry
- Inherits:
-
Object
- Object
- Coradoc::AsciiDoc::Transform::Registry
- 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.
Class Method Summary collapse
-
.clear ⇒ void
Clear all registrations (useful for testing).
-
.lookup(model_class) ⇒ #call?
Lookup a transformer for a model.
-
.register(source_class, transformer, target_class: nil) ⇒ void
Register a transformer for a source class.
-
.register_with_priority(source_class, transformer, priority: 0) ⇒ void
Register a transformer with a priority.
-
.registered?(model_class) ⇒ Boolean
Check if a transformer is registered for a class.
-
.registered_classes ⇒ Array<Class>
Get all registered source classes.
-
.registry ⇒ Hash
Get the global registry instance.
-
.transform(model) ⇒ Object
Transform a model using the registered transformer.
Class Method Details
.clear ⇒ void
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.
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
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.
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
118 119 120 |
# File 'lib/coradoc/asciidoc/transform/registry.rb', line 118 def registered?(model_class) !lookup(model_class).nil? end |
.registered_classes ⇒ Array<Class>
Get all registered source classes
133 134 135 |
# File 'lib/coradoc/asciidoc/transform/registry.rb', line 133 def registered_classes registry.keys end |
.registry ⇒ Hash
Get the global registry instance
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
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 |