Class: Uniword::Ooxml::Schema::ElementSerializer
- Inherits:
-
Object
- Object
- Uniword::Ooxml::Schema::ElementSerializer
- Defined in:
- lib/uniword/ooxml/schema/element_serializer.rb
Overview
Schema-driven serializer for OOXML elements.
Responsibility: Serialize ANY element to OOXML XML using schema definition. Single Responsibility - element serialization only, schema-driven.
Follows configuration over convention - serialization behavior is defined by external YAML schema, not hardcoded logic.
Instance Method Summary collapse
-
#initialize(schema: nil) ⇒ ElementSerializer
constructor
Initialize element serializer.
-
#serialize(element, options = {}) ⇒ String
Serialize element to OOXML XML.
Constructor Details
#initialize(schema: nil) ⇒ ElementSerializer
Initialize element serializer
27 28 29 |
# File 'lib/uniword/ooxml/schema/element_serializer.rb', line 27 def initialize(schema: nil) @schema = schema || load_default_schema end |
Instance Method Details
#serialize(element, options = {}) ⇒ String
Serialize element to OOXML XML
Uses schema definition to determine structure, attributes, children.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/uniword/ooxml/schema/element_serializer.rb', line 45 def serialize(element, = {}) validate_element(element) # If element has its own to_xml method, use it directly # This ensures proper serialization with all attributes and content if element.respond_to?(:to_xml) && ![:use_schema] xml_str = element.to_xml(pretty: [:pretty]) # Remove XML declaration unless standalone xml_str = xml_str.sub(/<\?xml[^?]*\?>\n?/, "") unless .fetch( :standalone, false ) # Add XML declaration if standalone xml_str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n#{xml_str}" if .fetch( :standalone, false ) return xml_str.strip end # Get schema definition schema_def = @schema.definition_for(element.class) # Build XML document doc = Nokogiri::XML::Document.new # Create root element with proper namespace root = serialize_element_to_node(doc, element, schema_def, ) doc.root = root # Format output xml_str = if [:pretty] doc.to_xml(indent: 2) else root.to_xml end # Remove XML declaration unless standalone xml_str = xml_str.sub(/<\?xml[^?]*\?>\n?/, "") unless .fetch( :standalone, false ) # Add XML declaration if standalone xml_str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n#{xml_str}" if .fetch( :standalone, false ) xml_str.strip end |