Class: Uniword::Transformation::TransformationRuleRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/transformation/transformation_rule_registry.rb

Overview

Registry for transformation rules.

Responsibility: Manage registration and lookup of transformation rules. Single Responsibility - only handles rule registration and retrieval.

Follows Open/Closed Principle - new rules can be registered without modifying this class.

Follows MECE Principle - each rule is mutually exclusive (handles specific element types and format combinations), and collectively exhaustive (fallback to NullTransformationRule ensures coverage).

Examples:

Register and use rules

registry = TransformationRuleRegistry.new
registry.register(ParagraphTransformationRule.new(
  source_format: :docx,
  target_format: :mhtml
))

rule = registry.find_rule(
  element_type: Paragraph,
  source_format: :docx,
  target_format: :mhtml
)

Instance Method Summary collapse

Constructor Details

#initializeTransformationRuleRegistry

Initialize empty registry



31
32
33
34
# File 'lib/uniword/transformation/transformation_rule_registry.rb', line 31

def initialize
  @rules = []
  @null_rule = NullTransformationRule.new
end

Instance Method Details

#clearself

Clear all registered rules

Useful for testing or reconfiguration

Returns:

  • (self)

    Returns self for method chaining



102
103
104
105
# File 'lib/uniword/transformation/transformation_rule_registry.rb', line 102

def clear
  @rules.clear
  self
end

#countInteger

Get count of registered rules

Returns:

  • (Integer)

    Number of registered rules



86
87
88
# File 'lib/uniword/transformation/transformation_rule_registry.rb', line 86

def count
  @rules.count
end

#empty?Boolean

Check if any rules are registered

Returns:

  • (Boolean)

    true if registry has rules



93
94
95
# File 'lib/uniword/transformation/transformation_rule_registry.rb', line 93

def empty?
  @rules.empty?
end

#find_rule(element_type:, source_format:, target_format:) ⇒ TransformationRule

Find a rule matching the given criteria

Returns the first matching rule, or NullTransformationRule if none match. NullTransformationRule returns element unchanged (deep copy).

Examples:

Find a rule

rule = registry.find_rule(
  element_type: Paragraph,
  source_format: :docx,
  target_format: :mhtml
)

Parameters:

  • element_type (Class)

    The element class to transform

  • source_format (Symbol)

    Source format (:docx or :mhtml)

  • target_format (Symbol)

    Target format (:docx or :mhtml)

Returns:



66
67
68
69
70
71
72
73
74
# File 'lib/uniword/transformation/transformation_rule_registry.rb', line 66

def find_rule(element_type:, source_format:, target_format:)
  @rules.find do |rule|
    rule.matches?(
      element_type: element_type,
      source_format: source_format,
      target_format: target_format,
    )
  end || @null_rule
end

#register(rule) ⇒ self

Register a transformation rule

Examples:

Register a rule

registry.register(CustomRule.new(source_format: :docx, target_format: :mhtml))

Parameters:

Returns:

  • (self)

    Returns self for method chaining

Raises:

  • (ArgumentError)

    if rule is not a TransformationRule



44
45
46
47
48
# File 'lib/uniword/transformation/transformation_rule_registry.rb', line 44

def register(rule)
  validate_rule(rule)
  @rules << rule
  self
end

#register_all(rules) ⇒ self

Register multiple rules at once

Examples:

Batch registration

registry.register_all([rule1, rule2, rule3])

Parameters:

Returns:

  • (self)

    Returns self for method chaining



114
115
116
117
# File 'lib/uniword/transformation/transformation_rule_registry.rb', line 114

def register_all(rules)
  rules.each { |rule| register(rule) }
  self
end

#rulesArray<TransformationRule>

Get all registered rules

Returns:



79
80
81
# File 'lib/uniword/transformation/transformation_rule_registry.rb', line 79

def rules
  @rules.dup
end