Class: Coradoc::AsciiDoc::Model::Base
- Inherits:
-
Lutaml::Model::Serializable
- Object
- Lutaml::Model::Serializable
- Coradoc::AsciiDoc::Model::Base
- Includes:
- Lutaml::Model::ComparableModel
- Defined in:
- lib/coradoc/asciidoc/model/base.rb
Overview
Base class for all Coradoc model objects.
The Base class provides common functionality for all document model elements, including serialization support, tree traversal, and attribute management.
All model classes inherit from this class to get:
- Lutaml::Model serialization capabilities
- ComparableModel functionality
- Visit pattern for tree traversal
- Polymorphic content serialization
Direct Known Subclasses
Attached, Attribute, AttributeList, AttributeListAttribute, Audio, Author, Bibliography, BibliographyEntry, Coradoc::AsciiDoc::Model::Break::PageBreak, Coradoc::AsciiDoc::Model::Break::ThematicBreak, CommentBlock, CommentLine, Document, DocumentAttributes, Glossaries, Header, Image::Core, Include, Inline::Base, LineBreak, List::Base, List::DefinitionItem, List::Item, NamedAttribute, RejectedPositionalAttribute, ReviewerNote, Revision, Section, Table, TableCell, TableRow, Term, TextElement, Title, Video
Instance Attribute Summary collapse
-
#id ⇒ String?
readonly
Optional identifier for the element.
Class Method Summary collapse
-
.visit(element) {|element, :post| ... } ⇒ Object
Visit pattern for traversing the document tree.
Instance Method Summary collapse
-
#block_level? ⇒ Boolean
Element classification for spacing and serialization decisions.
- #inline? ⇒ Boolean
-
#serialize_content(content) ⇒ Object
Serialize polymorphic content to AsciiDoc string Handles: Arrays, Strings, nil, and model objects Raises ArgumentError for unknown types to force proper handling.
-
#simplify_block_content(content) ⇒ Object
Generate a warning message whenever this method is called.
-
#to_adoc ⇒ String
Serialize this model element to AsciiDoc.
-
#to_h ⇒ Object
Does a shallow attribute dump of the object, for use when instantiating a new object (e.g. of a subclass) from an existing one.
- #visit(&block) ⇒ Object
Instance Attribute Details
#id ⇒ String? (readonly)
Returns Optional identifier for the element.
33 34 35 36 37 38 39 40 41 42 43 44 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/coradoc/asciidoc/model/base.rb', line 33 class Base < Lutaml::Model::Serializable include Lutaml::Model::ComparableModel attribute :id, :string # 1-indexed source line where this element begins, when known. # Populated by the Parslet transformer from the matched Slice's # line_and_column. nil for programmatically constructed models. # Single source of truth for source-position propagation through # AsciiDoc::Model → CoreModel (Issue 1, STATUS-2026-06-28). attribute :source_line, :integer # Element classification for spacing and serialization decisions. # Subclasses override these to declare their level. def block_level? false end def inline? false end # Generate a warning message whenever this method is called. def simplify_block_content(content) warn '[DEPRECATION] #simplify_block_content is called inside a Lutaml Model. This is still a WIP.' # print part of the stack trace caller_locations(1, 3).each do |location| warn " #{location.path}:#{location.lineno} in #{location.label}" end content end # Visit pattern for traversing the document tree def self.visit(element, &block) return element if element.nil? element = yield element, :pre element = case element when Coradoc::AsciiDoc::Model::Base element.visit(&block) when Array element.map { |child| visit(child, &block) } .flatten.compact when Hash result = {} element.each do |k, v| result[k] = visit(v, &block) end result else element end yield element, :post end def visit(&block) self.class.attributes.each_key do |attr_name| child = public_send(attr_name) result = self.class.visit(child, &block) public_send(:"#{attr_name}=", result) if result != child end self end # Serialize polymorphic content to AsciiDoc string # Handles: Arrays, Strings, nil, and model objects # Raises ArgumentError for unknown types to force proper handling def serialize_content(content) case content when Array content.map { |elem| serialize_content(elem) }.join when String content when nil '' when Coradoc::AsciiDoc::Model::Base, Lutaml::Model::Serializable # All model objects should respond to to_adoc content.to_adoc else # This is a programming error - we received an unexpected type raise ArgumentError, "Cannot serialize #{content.class.name} in content. " \ 'Expected String, nil, Array, Coradoc::AsciiDoc::Model::Base, or ' \ "Lutaml::Model::Serializable. Got: #{content.inspect[0..100]}" end end # Does a shallow attribute dump of the object, # for use when instantiating a new object (e.g. of a subclass) from an # existing one. def to_h self.class.attributes.keys.each_with_object({}) do |attribute, acc| acc[attribute] = public_send(attribute) end end # Serialize this model element to AsciiDoc # # This is the unified serialization method for all Model objects. # It uses the ElementRegistry system which provides serializers # for each model type. # # @return [String] AsciiDoc representation of this element # # @example Serialize a paragraph to AsciiDoc # para = Coradoc::AsciiDoc::Model::Paragraph.new("Hello World") # para.to_adoc # => "Hello World\n" # def to_adoc Coradoc::AsciiDoc::Serializer.serialize(self) end end |
Class Method Details
.visit(element) {|element, :post| ... } ⇒ Object
Visit pattern for traversing the document tree
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/coradoc/asciidoc/model/base.rb', line 67 def self.visit(element, &block) return element if element.nil? element = yield element, :pre element = case element when Coradoc::AsciiDoc::Model::Base element.visit(&block) when Array element.map { |child| visit(child, &block) } .flatten.compact when Hash result = {} element.each do |k, v| result[k] = visit(v, &block) end result else element end yield element, :post end |
Instance Method Details
#block_level? ⇒ Boolean
Element classification for spacing and serialization decisions. Subclasses override these to declare their level.
47 48 49 |
# File 'lib/coradoc/asciidoc/model/base.rb', line 47 def block_level? false end |
#inline? ⇒ Boolean
51 52 53 |
# File 'lib/coradoc/asciidoc/model/base.rb', line 51 def inline? false end |
#serialize_content(content) ⇒ Object
Serialize polymorphic content to AsciiDoc string Handles: Arrays, Strings, nil, and model objects Raises ArgumentError for unknown types to force proper handling
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/coradoc/asciidoc/model/base.rb', line 101 def serialize_content(content) case content when Array content.map { |elem| serialize_content(elem) }.join when String content when nil '' when Coradoc::AsciiDoc::Model::Base, Lutaml::Model::Serializable # All model objects should respond to to_adoc content.to_adoc else # This is a programming error - we received an unexpected type raise ArgumentError, "Cannot serialize #{content.class.name} in content. " \ 'Expected String, nil, Array, Coradoc::AsciiDoc::Model::Base, or ' \ "Lutaml::Model::Serializable. Got: #{content.inspect[0..100]}" end end |
#simplify_block_content(content) ⇒ Object
Generate a warning message whenever this method is called.
56 57 58 59 60 61 62 63 64 |
# File 'lib/coradoc/asciidoc/model/base.rb', line 56 def simplify_block_content(content) warn '[DEPRECATION] #simplify_block_content is called inside a Lutaml Model. This is still a WIP.' # print part of the stack trace caller_locations(1, 3).each do |location| warn " #{location.path}:#{location.lineno} in #{location.label}" end content end |
#to_adoc ⇒ String
Serialize this model element to AsciiDoc
This is the unified serialization method for all Model objects. It uses the ElementRegistry system which provides serializers for each model type.
142 143 144 |
# File 'lib/coradoc/asciidoc/model/base.rb', line 142 def to_adoc Coradoc::AsciiDoc::Serializer.serialize(self) end |
#to_h ⇒ Object
Does a shallow attribute dump of the object, for use when instantiating a new object (e.g. of a subclass) from an existing one.
124 125 126 127 128 |
# File 'lib/coradoc/asciidoc/model/base.rb', line 124 def to_h self.class.attributes.keys.each_with_object({}) do |attribute, acc| acc[attribute] = public_send(attribute) end end |
#visit(&block) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/coradoc/asciidoc/model/base.rb', line 89 def visit(&block) self.class.attributes.each_key do |attr_name| child = public_send(attr_name) result = self.class.visit(child, &block) public_send(:"#{attr_name}=", result) if result != child end self end |