Class: Toml::Merge::Emitter
- Inherits:
-
Ast::Merge::EmitterBase
- Object
- Ast::Merge::EmitterBase
- Toml::Merge::Emitter
- Defined in:
- lib/toml/merge/emitter.rb
Overview
Custom TOML emitter that preserves comments and formatting.
This class provides utilities for emitting TOML while maintaining
the original structure, comments, and style choices.
Inherits common emitter functionality from Ast::Merge::EmitterBase.
Instance Method Summary collapse
-
#clear_subclass_state ⇒ Object
Clear subclass-specific state.
-
#emit_array_end ⇒ Object
Emit an array end.
-
#emit_array_item(value) ⇒ Object
Emit an array item.
-
#emit_array_of_tables_header(name) ⇒ Object
Emit an array of tables header.
-
#emit_array_start(key) ⇒ Object
Emit a multi-line array start.
-
#emit_comment(text, inline: false) ⇒ Object
Emit a comment line.
-
#emit_inline_array(key, items) ⇒ Object
Emit an inline array.
-
#emit_inline_table(key, pairs) ⇒ Object
Emit an inline table.
-
#emit_key_value(key, value, inline_comment: nil) ⇒ Object
Emit a key-value pair.
-
#emit_table_header(name) ⇒ Object
Emit a table header.
-
#emit_tracked_comment(comment) ⇒ Object
Emit a tracked comment from CommentTracker.
-
#initialize_subclass_state(**options) ⇒ Object
Initialize subclass-specific state.
-
#to_toml ⇒ String
Get the output as a TOML string.
Instance Method Details
#clear_subclass_state ⇒ Object
Clear subclass-specific state
22 23 24 |
# File 'lib/toml/merge/emitter.rb', line 22 def clear_subclass_state # Nothing to clear for TOML end |
#emit_array_end ⇒ Object
Emit an array end
107 108 109 110 |
# File 'lib/toml/merge/emitter.rb', line 107 def emit_array_end dedent @lines << "#{current_indent}]" end |
#emit_array_item(value) ⇒ Object
Emit an array item
102 103 104 |
# File 'lib/toml/merge/emitter.rb', line 102 def emit_array_item(value) @lines << "#{current_indent}#{value}," end |
#emit_array_of_tables_header(name) ⇒ Object
Emit an array of tables header
58 59 60 |
# File 'lib/toml/merge/emitter.rb', line 58 def emit_array_of_tables_header(name) @lines << "[[#{name}]]" end |
#emit_array_start(key) ⇒ Object
Emit a multi-line array start
94 95 96 97 |
# File 'lib/toml/merge/emitter.rb', line 94 def emit_array_start(key) @lines << "#{current_indent}#{key} = [" indent end |
#emit_comment(text, inline: false) ⇒ Object
Emit a comment line
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/toml/merge/emitter.rb', line 37 def emit_comment(text, inline: false) if inline # Inline comments are appended to the last line return if @lines.empty? @lines[-1] = "#{@lines[-1]} # #{text}" else @lines << "#{current_indent}# #{text}" end end |
#emit_inline_array(key, items) ⇒ Object
Emit an inline array
86 87 88 89 |
# File 'lib/toml/merge/emitter.rb', line 86 def emit_inline_array(key, items) formatted_items = items.join(", ") @lines << "#{current_indent}#{key} = [#{formatted_items}]" end |
#emit_inline_table(key, pairs) ⇒ Object
Emit an inline table
77 78 79 80 |
# File 'lib/toml/merge/emitter.rb', line 77 def emit_inline_table(key, pairs) formatted_pairs = pairs.map { |k, v| "#{k} = #{v}" }.join(", ") @lines << "#{current_indent}#{key} = { #{formatted_pairs} }" end |
#emit_key_value(key, value, inline_comment: nil) ⇒ Object
Emit a key-value pair
67 68 69 70 71 |
# File 'lib/toml/merge/emitter.rb', line 67 def emit_key_value(key, value, inline_comment: nil) line = "#{current_indent}#{key} = #{value}" line += " # #{inline_comment}" if inline_comment @lines << line end |
#emit_table_header(name) ⇒ Object
Emit a table header
51 52 53 |
# File 'lib/toml/merge/emitter.rb', line 51 def emit_table_header(name) @lines << "[#{name}]" end |
#emit_tracked_comment(comment) ⇒ Object
Emit a tracked comment from CommentTracker
28 29 30 31 |
# File 'lib/toml/merge/emitter.rb', line 28 def emit_tracked_comment(comment) indent = " " * (comment[:indent] || 0) @lines << "#{indent}# #{comment[:text]}" end |
#initialize_subclass_state(**options) ⇒ Object
Initialize subclass-specific state
17 18 19 |
# File 'lib/toml/merge/emitter.rb', line 17 def initialize_subclass_state(**) # TOML doesn't need separator tracking like JSON end |
#to_toml ⇒ String
Get the output as a TOML string
115 116 117 |
# File 'lib/toml/merge/emitter.rb', line 115 def to_toml to_s end |