Class: Uniword::Transformation::TransformationRule

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

Overview

Base class for transformation rules.

Responsibility: Define the interface for element transformation rules. Each rule transforms one element type from source format to target format.

Follows Single Responsibility Principle - one rule handles one transformation pattern. Follows Open/Closed Principle - new rules can be added without modifying this class.

Examples:

Create a custom transformation rule

class CustomRule < TransformationRule
  def matches?(element_type:, source_format:, target_format:)
    element_type == CustomElement &&
      source_format == :docx &&
      target_format == :mhtml
  end

  def transform(element)
    # Transform CustomElement from DOCX to MHTML format
    transformed = CustomElement.new
    # Copy and adapt properties
    transformed
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_format:, target_format:) ⇒ TransformationRule

Initialize transformation rule

Parameters:

  • source_format (Symbol)

    Source format (:docx or :mhtml)

  • target_format (Symbol)

    Target format (:docx or :mhtml)



35
36
37
38
# File 'lib/uniword/transformation/transformation_rule.rb', line 35

def initialize(source_format:, target_format:)
  @source_format = source_format
  @target_format = target_format
end

Instance Attribute Details

#source_formatObject (readonly)

Returns the value of attribute source_format.



29
30
31
# File 'lib/uniword/transformation/transformation_rule.rb', line 29

def source_format
  @source_format
end

#target_formatObject (readonly)

Returns the value of attribute target_format.



29
30
31
# File 'lib/uniword/transformation/transformation_rule.rb', line 29

def target_format
  @target_format
end

Instance Method Details

#matches?(element_type:, source_format:, target_format:) ⇒ Boolean

Check if this rule matches the transformation request

Parameters:

  • element_type (Class)

    The element class to transform

  • source_format (Symbol)

    Source format

  • target_format (Symbol)

    Target format

Returns:

  • (Boolean)

    true if this rule can handle the transformation

Raises:

  • (NotImplementedError)


46
47
48
# File 'lib/uniword/transformation/transformation_rule.rb', line 46

def matches?(element_type:, source_format:, target_format:)
  raise NotImplementedError, "#{self.class} must implement #matches?"
end

#transform(element) ⇒ Element+

Transform an element from source format to target format

Parameters:

  • element (Element)

    The element to transform

Returns:

Raises:

  • (NotImplementedError)


54
55
56
# File 'lib/uniword/transformation/transformation_rule.rb', line 54

def transform(element)
  raise NotImplementedError, "#{self.class} must implement #transform"
end