Class: Coradoc::CoreModel::Base
- Inherits:
-
Lutaml::Model::Serializable
- Object
- Lutaml::Model::Serializable
- Coradoc::CoreModel::Base
- Defined in:
- lib/coradoc/core_model/base.rb
Overview
Base class for all core models
Provides common functionality for schema-agnostic document models. This class establishes the foundational structure for all CoreModel classes, including semantic equivalence comparison and common attributes.
Direct Known Subclasses
Abbreviation, Bibliography, BibliographyEntry, Block, Callout, CommentLine, DefinitionItem, DefinitionList, ElementAttribute, Footnote, FootnoteReference, FrontmatterBlock::FrontmatterValue, Image, Include, IncludeLevelOffset, IncludeOptions, InlineElement, ListBlock, ListItem, Metadata, MetadataEntry, OutputArtifact, StructuralElement, Table, TableCell, TableRow, Term, TextContent, Toc, TocEntry
Instance Attribute Summary collapse
-
#element_attributes ⇒ Array<ElementAttribute>
Collection of element attributes.
-
#id ⇒ String?
Unique identifier for the element.
-
#metadata_entries ⇒ Array<MetadataEntry>
Additional metadata entries.
-
#title ⇒ String?
Title of the element.
Class Method Summary collapse
-
.build(**attrs) {|instance| ... } ⇒ Object
Construct an instance and yield it for in-place mutation.
Instance Method Summary collapse
-
#accept(visitor) ⇒ void
Accept a visitor to traverse this element.
-
#attr(name = nil) ⇒ Object
Get all element attributes as a hash, or a specific attribute value by name.
-
#body_content? ⇒ Boolean
True when this node counts as “real body content” for the purposes of empty-document detection and similar structural queries.
-
#flat_text ⇒ String
Flatten this element to a plain-text string.
-
#metadata(key = nil) ⇒ Object
Get all metadata as a hash, or a specific metadata value by key.
-
#semantically_equivalent?(other) ⇒ Boolean
Compare this model with another for semantic equivalence.
-
#set_attr(name, value) ⇒ Object
Set attribute value.
-
#set_metadata(key, value) ⇒ Object
Convenience method to set metadata.
-
#whitespace_only? ⇒ Boolean
True when this node is structurally present but carries no visible characters.
Instance Attribute Details
#element_attributes ⇒ Array<ElementAttribute>
Returns collection of element attributes.
38 |
# File 'lib/coradoc/core_model/base.rb', line 38 attribute :element_attributes, ElementAttribute, collection: true |
#id ⇒ String?
Returns unique identifier for the element.
30 |
# File 'lib/coradoc/core_model/base.rb', line 30 attribute :id, :string |
#metadata_entries ⇒ Array<MetadataEntry>
Returns additional metadata entries.
42 |
# File 'lib/coradoc/core_model/base.rb', line 42 attribute :metadata_entries, MetadataEntry, collection: true |
#title ⇒ String?
Returns title of the element.
34 |
# File 'lib/coradoc/core_model/base.rb', line 34 attribute :title, :string |
Class Method Details
.build(**attrs) {|instance| ... } ⇒ Object
Construct an instance and yield it for in-place mutation.
This is the programmatic-construction entry point for CoreModel nodes. It calls new exactly as a caller would, then yields the resulting instance for append-style construction. No new class hierarchy, no method_missing — the block operates on the real model object.
Per-class fluent helpers (e.g., ListBlock#add_item, ListItem#add_text) compose naturally with build:
list = ListBlock.build do |ul|
children.each { |c| ul.add_item { |li| li.add_link(c[:slug], text: c[:title]) } }
end
Without a block, build(**attrs) is identical to new(**attrs).
60 61 62 63 64 |
# File 'lib/coradoc/core_model/base.rb', line 60 def self.build(**attrs) instance = new(**attrs) yield instance if block_given? instance end |
Instance Method Details
#accept(visitor) ⇒ void
This method returns an undefined value.
Accept a visitor to traverse this element
Implements the visitor pattern for document traversal. The visitor’s visit method will be called with this element.
166 167 168 |
# File 'lib/coradoc/core_model/base.rb', line 166 def accept(visitor) visitor.visit(self) end |
#attr ⇒ Hash #attr(name) ⇒ String?
Get all element attributes as a hash, or a specific attribute value by name
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/coradoc/core_model/base.rb', line 102 def attr(name = nil) attrs = element_attributes || [] if name.nil? # Return all attributes as hash attrs.each_with_object({}) { |a, h| h[a.name] = a.value } else # Return specific value attrs.find { |a| a.name == name }&.value end end |
#body_content? ⇒ Boolean
True when this node counts as “real body content” for the purposes of empty-document detection and similar structural queries. Default is true; metadata and ephemeral nodes (FrontmatterBlock, CommentBlock, CommentLine) override to false. Polymorphic dispatch keeps the predicate open for future “skip-me” types — no central walker to edit (OCP).
176 177 178 |
# File 'lib/coradoc/core_model/base.rb', line 176 def body_content? true end |
#flat_text ⇒ String
Flatten this element to a plain-text string.
Subclasses that include ChildrenContent override this to concatenate their children’s text. Block-level elements without textual content (ListBlock, Table, etc.) fall back to the empty string — they are serialized structurally, not flattened into inline text.
155 156 157 |
# File 'lib/coradoc/core_model/base.rb', line 155 def flat_text "" end |
#metadata ⇒ Hash #metadata(key) ⇒ String?
Get all metadata as a hash, or a specific metadata value by key
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/coradoc/core_model/base.rb', line 72 def (key = nil) entries = || [] if key.nil? # Return all metadata as hash entries.each_with_object({}) { |e, h| h[e.key] = e.value } else # Return specific value entries.find { |e| e.key == key }&.value end end |
#semantically_equivalent?(other) ⇒ Boolean
Compare this model with another for semantic equivalence
Semantic equivalence means the models represent the same semantic content, even if their exact structure differs. This is different from equality, which requires exact matching.
134 135 136 137 138 139 140 |
# File 'lib/coradoc/core_model/base.rb', line 134 def semantically_equivalent?(other) return false unless other.is_a?(self.class) comparable_attributes.all? do |attr| compare_attribute(attr, other) end end |
#set_attr(name, value) ⇒ Object
Set attribute value
116 117 118 119 120 121 122 123 124 |
# File 'lib/coradoc/core_model/base.rb', line 116 def set_attr(name, value) self.element_attributes ||= [] existing = element_attributes.find { |a| a.name == name } if existing existing.value = value else element_attributes << ElementAttribute.new(name: name, value: value) end end |
#set_metadata(key, value) ⇒ Object
Convenience method to set metadata
86 87 88 89 90 91 92 93 94 |
# File 'lib/coradoc/core_model/base.rb', line 86 def (key, value) self. ||= [] existing = .find { |e| e.key == key } if existing existing.value = value else << MetadataEntry.new(key: key, value: value) end end |
#whitespace_only? ⇒ Boolean
True when this node is structurally present but carries no visible characters. Default is false; inline text and paragraph blocks override to inspect their text content.
183 184 185 |
# File 'lib/coradoc/core_model/base.rb', line 183 def whitespace_only? false end |