Class: Coradoc::AsciiDoc::Serializer::SerializationContext
- Inherits:
-
Object
- Object
- Coradoc::AsciiDoc::Serializer::SerializationContext
- Defined in:
- lib/coradoc/asciidoc/serializer/serialization_context.rb
Overview
Context object passed through serialization pipeline
Provides a type-safe, extensible way to pass serialization state through the document tree, replacing the opaque options hash.
Instance Attribute Summary collapse
-
#depth ⇒ Integer
readonly
Nesting depth in the document tree Can be used for indentation or formatting decisions.
-
#format ⇒ Symbol
readonly
Output format (:adoc, :html, :xml, etc.).
-
#last_element ⇒ Boolean
readonly
Whether this is the last element in its container Used to control trailing newlines and spacing.
-
#options ⇒ Hash
readonly
Additional options hash for extension Provides backward compatibility and extensibility.
-
#parent ⇒ SerializationContext?
readonly
Parent context (if any) Allows traversing up the context chain.
Class Method Summary collapse
-
.from_options(options_or_context) ⇒ SerializationContext
Backward compatibility: convert options hash to context.
-
.root(format: :adoc, options: {}) ⇒ SerializationContext
Create a root context for a new serialization.
Instance Method Summary collapse
-
#as_last_element ⇒ SerializationContext
Create a context with last_element set to true.
-
#for_child(child_index, total_children) ⇒ SerializationContext
Create a context for a child element.
-
#has_option?(key) ⇒ Boolean
Check if an option is present.
-
#in_table_cell? ⇒ Boolean
Check if we’re inside a table cell.
-
#initialize(format: :adoc, last_element: false, depth: 0, parent: nil, options: {}) ⇒ SerializationContext
constructor
Create a new serialization context.
-
#inspect ⇒ String
(also: #to_s)
String representation for debugging.
-
#option(key, default = nil) ⇒ Object
Get an option value.
-
#root? ⇒ Boolean
Check if this is the root context (no parent).
-
#with_format(new_format) ⇒ SerializationContext
Create a context for a different format.
-
#with_option(key, value) ⇒ SerializationContext
Create a context with an additional option.
Constructor Details
#initialize(format: :adoc, last_element: false, depth: 0, parent: nil, options: {}) ⇒ SerializationContext
Create a new serialization context
61 62 63 64 65 66 67 68 69 |
# File 'lib/coradoc/asciidoc/serializer/serialization_context.rb', line 61 def initialize(format: :adoc, last_element: false, depth: 0, parent: nil, options: {}) @format = format @last_element = last_element @depth = depth @parent = parent @options = .freeze freeze end |
Instance Attribute Details
#depth ⇒ Integer (readonly)
Nesting depth in the document tree Can be used for indentation or formatting decisions
40 41 42 |
# File 'lib/coradoc/asciidoc/serializer/serialization_context.rb', line 40 def depth @depth end |
#format ⇒ Symbol (readonly)
Output format (:adoc, :html, :xml, etc.)
28 29 30 |
# File 'lib/coradoc/asciidoc/serializer/serialization_context.rb', line 28 def format @format end |
#last_element ⇒ Boolean (readonly)
Whether this is the last element in its container Used to control trailing newlines and spacing
34 35 36 |
# File 'lib/coradoc/asciidoc/serializer/serialization_context.rb', line 34 def last_element @last_element end |
#options ⇒ Hash (readonly)
Additional options hash for extension Provides backward compatibility and extensibility
52 53 54 |
# File 'lib/coradoc/asciidoc/serializer/serialization_context.rb', line 52 def @options end |
#parent ⇒ SerializationContext? (readonly)
Parent context (if any) Allows traversing up the context chain
46 47 48 |
# File 'lib/coradoc/asciidoc/serializer/serialization_context.rb', line 46 def parent @parent end |
Class Method Details
.from_options(options_or_context) ⇒ SerializationContext
Backward compatibility: convert options hash to context
216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/coradoc/asciidoc/serializer/serialization_context.rb', line 216 def self.() return if .is_a?(SerializationContext) new( format: .fetch(:format, :adoc), last_element: .fetch(:last_element, false), depth: .fetch(:depth, 0), parent: nil, options: ) end |
.root(format: :adoc, options: {}) ⇒ SerializationContext
Create a root context for a new serialization
205 206 207 |
# File 'lib/coradoc/asciidoc/serializer/serialization_context.rb', line 205 def self.root(format: :adoc, options: {}) new(format: format, last_element: false, depth: 0, parent: nil, options: ) end |
Instance Method Details
#as_last_element ⇒ SerializationContext
Create a context with last_element set to true
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/coradoc/asciidoc/serializer/serialization_context.rb', line 135 def as_last_element return self if last_element SerializationContext.new( format: format, last_element: true, depth: depth, parent: parent, options: ) end |
#for_child(child_index, total_children) ⇒ SerializationContext
Create a context for a child element
Automatically adjusts depth and calculates last_element based on the child’s position.
84 85 86 87 88 89 90 91 92 |
# File 'lib/coradoc/asciidoc/serializer/serialization_context.rb', line 84 def for_child(child_index, total_children) SerializationContext.new( format: format, last_element: child_index == total_children - 1, depth: depth + 1, parent: self, options: ) end |
#has_option?(key) ⇒ Boolean
Check if an option is present
186 187 188 |
# File 'lib/coradoc/asciidoc/serializer/serialization_context.rb', line 186 def has_option?(key) .key?(key) end |
#in_table_cell? ⇒ Boolean
Check if we’re inside a table cell
Traverses up the parent chain to detect table context.
157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/coradoc/asciidoc/serializer/serialization_context.rb', line 157 def in_table_cell? return false unless parent # Check if any ancestor is a table current = parent while current return true if current.[:in_table] == true current = current.parent end false end |
#inspect ⇒ String Also known as: to_s
String representation for debugging
231 232 233 |
# File 'lib/coradoc/asciidoc/serializer/serialization_context.rb', line 231 def inspect "#<#{self.class.name} format=#{format} last_element=#{last_element} depth=#{depth}>" end |
#option(key, default = nil) ⇒ Object
Get an option value
178 179 180 |
# File 'lib/coradoc/asciidoc/serializer/serialization_context.rb', line 178 def option(key, default = nil) .fetch(key, default) end |
#root? ⇒ Boolean
Check if this is the root context (no parent)
193 194 195 |
# File 'lib/coradoc/asciidoc/serializer/serialization_context.rb', line 193 def root? parent.nil? end |
#with_format(new_format) ⇒ SerializationContext
Create a context for a different format
122 123 124 125 126 127 128 129 130 |
# File 'lib/coradoc/asciidoc/serializer/serialization_context.rb', line 122 def with_format(new_format) SerializationContext.new( format: new_format, last_element: last_element, depth: depth, parent: parent, options: ) end |
#with_option(key, value) ⇒ SerializationContext
Create a context with an additional option
Returns a new context with the option added to the options hash. Does not mutate the original context.
105 106 107 108 109 110 111 112 113 |
# File 'lib/coradoc/asciidoc/serializer/serialization_context.rb', line 105 def with_option(key, value) SerializationContext.new( format: format, last_element: last_element, depth: depth, parent: parent, options: .merge(key => value) ) end |