Class: Coradoc::AsciiDoc::Serializer::SerializationContext

Inherits:
Object
  • Object
show all
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.

Examples:

Create a context

context = SerializationContext.new(
  format: :adoc,
  last_element: false,
  depth: 0
)

Create context for a child

child_context = context.for_child(0, 3)

Add custom option

context_with_option = context.with_option(:preserve_whitespace, true)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format: :adoc, last_element: false, depth: 0, parent: nil, options: {}) ⇒ SerializationContext

Create a new serialization context

Parameters:

  • format (Symbol) (defaults to: :adoc)

    Output format

  • last_element (Boolean) (defaults to: false)

    Whether this is the last element

  • depth (Integer) (defaults to: 0)

    Nesting depth

  • parent (SerializationContext, nil) (defaults to: nil)

    Parent context

  • options (Hash) (defaults to: {})

    Additional options



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 = options.freeze
  freeze
end

Instance Attribute Details

#depthInteger (readonly)

Nesting depth in the document tree Can be used for indentation or formatting decisions

Returns:

  • (Integer)

    current depth



40
41
42
# File 'lib/coradoc/asciidoc/serializer/serialization_context.rb', line 40

def depth
  @depth
end

#formatSymbol (readonly)

Output format (:adoc, :html, :xml, etc.)

Returns:

  • (Symbol)

    the output format



28
29
30
# File 'lib/coradoc/asciidoc/serializer/serialization_context.rb', line 28

def format
  @format
end

#last_elementBoolean (readonly)

Whether this is the last element in its container Used to control trailing newlines and spacing

Returns:

  • (Boolean)

    true if last element



34
35
36
# File 'lib/coradoc/asciidoc/serializer/serialization_context.rb', line 34

def last_element
  @last_element
end

#optionsHash (readonly)

Additional options hash for extension Provides backward compatibility and extensibility

Returns:

  • (Hash)

    additional options



52
53
54
# File 'lib/coradoc/asciidoc/serializer/serialization_context.rb', line 52

def options
  @options
end

#parentSerializationContext? (readonly)

Parent context (if any) Allows traversing up the context chain

Returns:



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

Examples:

Convert options hash

context = SerializationContext.from_options(last_element: true)

Parameters:

Returns:



216
217
218
219
220
221
222
223
224
225
226
# File 'lib/coradoc/asciidoc/serializer/serialization_context.rb', line 216

def self.from_options(options_or_context)
  return options_or_context if options_or_context.is_a?(SerializationContext)

  new(
    format: options_or_context.fetch(:format, :adoc),
    last_element: options_or_context.fetch(:last_element, false),
    depth: options_or_context.fetch(:depth, 0),
    parent: nil,
    options: options_or_context
  )
end

.root(format: :adoc, options: {}) ⇒ SerializationContext

Create a root context for a new serialization

Examples:

context = SerializationContext.root(format: :adoc)

Parameters:

  • format (Symbol) (defaults to: :adoc)

    Output format

  • options (Hash) (defaults to: {})

    Additional options

Returns:



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: options)
end

Instance Method Details

#as_last_elementSerializationContext

Create a context with last_element set to true

Returns:



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: 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.

Examples:

children.each_with_index do |child, i|
  serialize(child, context.for_child(i, children.length))
end

Parameters:

  • child_index (Integer)

    Index of this child (0-based)

  • total_children (Integer)

    Total number of children

Returns:



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: options
  )
end

#has_option?(key) ⇒ Boolean

Check if an option is present

Parameters:

  • key (Symbol)

    Option key

Returns:

  • (Boolean)

    true if option exists



186
187
188
# File 'lib/coradoc/asciidoc/serializer/serialization_context.rb', line 186

def has_option?(key)
  options.key?(key)
end

#in_table_cell?Boolean

Check if we’re inside a table cell

Traverses up the parent chain to detect table context.

Examples:

if context.in_table_cell?
  # Don't add block-level spacing
end

Returns:

  • (Boolean)

    true if in a table cell



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.options[:in_table] == true

    current = current.parent
  end
  false
end

#inspectString Also known as: to_s

String representation for debugging

Returns:

  • (String)

    Debug string



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

Examples:

preserve = context.option(:preserve_whitespace, false)

Parameters:

  • key (Symbol)

    Option key

  • default (Object) (defaults to: nil)

    Default value if not found

Returns:

  • (Object)

    Option value or default



178
179
180
# File 'lib/coradoc/asciidoc/serializer/serialization_context.rb', line 178

def option(key, default = nil)
  options.fetch(key, default)
end

#root?Boolean

Check if this is the root context (no parent)

Returns:

  • (Boolean)

    true if root



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

Examples:

Switch to HTML format

html_context = context.with_format(:html)

Parameters:

  • new_format (Symbol)

    New format

Returns:



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: 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.

Examples:

Add a custom option

context.with_option(:preserve_whitespace, true)

Parameters:

  • key (Symbol)

    Option key

  • value (Object)

    Option value

Returns:



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: options.merge(key => value)
  )
end