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, DefinitionItem, DefinitionList, ElementAttribute, Footnote, FootnoteReference, Image, InlineElement, ListBlock, ListItem, Metadata, MetadataEntry, StructuralElement, Table, TableCell, TableRow, Term, 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.
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.
-
#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.
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 |
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.
127 128 129 |
# File 'lib/coradoc/core_model/base.rb', line 127 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
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/coradoc/core_model/base.rb', line 80 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 |
#metadata ⇒ Hash #metadata(key) ⇒ String?
Get all metadata as a hash, or a specific metadata value by key
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/coradoc/core_model/base.rb', line 50 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.
112 113 114 115 116 117 118 |
# File 'lib/coradoc/core_model/base.rb', line 112 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
94 95 96 97 98 99 100 101 102 |
# File 'lib/coradoc/core_model/base.rb', line 94 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
64 65 66 67 68 69 70 71 72 |
# File 'lib/coradoc/core_model/base.rb', line 64 def (key, value) self. ||= [] existing = .find { |e| e.key == key } if existing existing.value = value else << MetadataEntry.new(key: key, value: value) end end |