Class: Coradoc::Docx::Transform::RuleRegistry
- Inherits:
-
Object
- Object
- Coradoc::Docx::Transform::RuleRegistry
- Defined in:
- lib/coradoc/docx/transform/rule_registry.rb
Overview
Registry for transform rules.
Manages registration and lookup of rules for OOXML element types. Rules are checked in priority order (highest first). Falls back to NullRule which raises ArgumentError.
Follows Open/Closed Principle: new rules are added by registering, not by modifying the registry class.
Instance Method Summary collapse
-
#find_rule(element) ⇒ Rule
Find the first rule that matches the element.
-
#initialize ⇒ RuleRegistry
constructor
A new instance of RuleRegistry.
-
#matches?(element) ⇒ Boolean
Check if any rule matches the element.
-
#register(rule) ⇒ self
Register a rule instance.
-
#size ⇒ Integer
Number of registered rules.
Constructor Details
#initialize ⇒ RuleRegistry
Returns a new instance of RuleRegistry.
15 16 17 |
# File 'lib/coradoc/docx/transform/rule_registry.rb', line 15 def initialize @rules = [] end |
Instance Method Details
#find_rule(element) ⇒ Rule
Find the first rule that matches the element
39 40 41 42 |
# File 'lib/coradoc/docx/transform/rule_registry.rb', line 39 def find_rule(element) @rules.find { |r| r.matches?(element) } || raise(ArgumentError, "No transform rule registered for #{element.class}") end |
#matches?(element) ⇒ Boolean
Check if any rule matches the element
48 49 50 |
# File 'lib/coradoc/docx/transform/rule_registry.rb', line 48 def matches?(element) @rules.any? { |r| r.matches?(element) } end |
#register(rule) ⇒ self
Register a rule instance
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/coradoc/docx/transform/rule_registry.rb', line 23 def register(rule) unless rule.is_a?(Rule) raise ArgumentError, "Expected Rule, got #{rule.class}" end @rules << rule @rules.sort_by! { |r| -r.priority } self end |
#size ⇒ Integer
Number of registered rules
54 55 56 |
# File 'lib/coradoc/docx/transform/rule_registry.rb', line 54 def size @rules.size end |