Class: Coradoc::Docx::Transform::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/coradoc/docx/transform/rule.rb

Overview

Base class for OOXML → CoreModel transform rules.

Each rule handles one OOXML element type and produces a CoreModel node. Rules are registered in RuleRegistry and dispatched by the ToCoreModel orchestrator.

Subclasses must implement:

- matches?(element) → true if this rule handles the element
- apply(element, context) → CoreModel node or Array of nodes

Examples:

Implementing a custom rule

class MyRule < Rule
  def matches?(element)
    element.is_a?(Uniword::Wordprocessingml::MyElement)
  end

  def apply(element, context)
    Coradoc::CoreModel::Block.new(
      element_type: 'paragraph',
      content: element.text
    )
  end
end

Instance Method Summary collapse

Instance Method Details

#apply(element, context) ⇒ Coradoc::CoreModel::Base, ...

Transform an OOXML element to a CoreModel node

Parameters:

  • element (Object)

    OOXML element to transform

  • context (Context)

    shared transform context

Returns:

  • (Coradoc::CoreModel::Base, Array, String, nil)

Raises:

  • (NotImplementedError)


43
44
45
# File 'lib/coradoc/docx/transform/rule.rb', line 43

def apply(element, context)
  raise NotImplementedError, "#{self.class}#apply not implemented"
end

#matches?(element) ⇒ Boolean

Check if this rule handles the given element

Parameters:

  • element (Object)

    OOXML element to check

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


34
35
36
# File 'lib/coradoc/docx/transform/rule.rb', line 34

def matches?(element)
  raise NotImplementedError, "#{self.class}#matches? not implemented"
end

#priorityInteger

Rule priority — higher priority rules are checked first. Override in subclasses when needed.

Returns:

  • (Integer)


51
52
53
# File 'lib/coradoc/docx/transform/rule.rb', line 51

def priority
  0
end