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, inline_comment: nil) ⇒ 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, inline_comment: nil) ⇒ 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
113 114 115 116 |
# File 'lib/toml/merge/emitter.rb', line 113 def emit_array_end dedent @lines << "#{current_indent}]" end |
#emit_array_item(value) ⇒ Object
Emit an array item
108 109 110 |
# File 'lib/toml/merge/emitter.rb', line 108 def emit_array_item(value) @lines << "#{current_indent}#{value}," end |
#emit_array_of_tables_header(name, inline_comment: nil) ⇒ Object
Emit an array of tables header
62 63 64 65 66 |
# File 'lib/toml/merge/emitter.rb', line 62 def emit_array_of_tables_header(name, inline_comment: nil) line = "[[#{name}]]" line += " # #{inline_comment}" if inline_comment @lines << line end |
#emit_array_start(key) ⇒ Object
Emit a multi-line array start
100 101 102 103 |
# File 'lib/toml/merge/emitter.rb', line 100 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
92 93 94 95 |
# File 'lib/toml/merge/emitter.rb', line 92 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
83 84 85 86 |
# File 'lib/toml/merge/emitter.rb', line 83 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
73 74 75 76 77 |
# File 'lib/toml/merge/emitter.rb', line 73 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, inline_comment: nil) ⇒ Object
Emit a table header
52 53 54 55 56 |
# File 'lib/toml/merge/emitter.rb', line 52 def emit_table_header(name, inline_comment: nil) line = "[#{name}]" line += " # #{inline_comment}" if inline_comment @lines << line 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
121 122 123 |
# File 'lib/toml/merge/emitter.rb', line 121 def to_toml to_s end |