Class: Toml::Merge::Emitter
- Inherits:
-
Ast::Merge::EmitterBase
- Object
- Ast::Merge::EmitterBase
- Toml::Merge::Emitter
- Includes:
- Ast::Merge::EmitterLineMetadataSupport
- 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(metadata: nil) ⇒ Object
Emit an array end.
-
#emit_array_item(value, metadata: nil) ⇒ Object
Emit an array item.
-
#emit_array_of_tables_header(name, inline_comment: nil, metadata: nil) ⇒ Object
Emit an array of tables header.
-
#emit_array_start(key, metadata: nil) ⇒ Object
Emit a multi-line array start.
- #emit_blank_line ⇒ Object
-
#emit_comment(text, inline: false) ⇒ Object
Emit a comment line.
-
#emit_inline_array(key, items, metadata: nil) ⇒ Object
Emit an inline array.
-
#emit_inline_table(key, pairs, metadata: nil) ⇒ Object
Emit an inline table.
-
#emit_key_value(key, value, inline_comment: nil, metadata: nil) ⇒ Object
Emit a key-value pair.
- #emit_raw_lines(raw_lines, metadata: nil) ⇒ Object
-
#emit_table_header(name, inline_comment: nil, metadata: 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
24 25 26 |
# File 'lib/toml/merge/emitter.rb', line 24 def clear_subclass_state end |
#emit_array_end(metadata: nil) ⇒ Object
Emit an array end
119 120 121 122 |
# File 'lib/toml/merge/emitter.rb', line 119 def emit_array_end(metadata: nil) dedent append_line("#{current_indent}]", ) end |
#emit_array_item(value, metadata: nil) ⇒ Object
Emit an array item
114 115 116 |
# File 'lib/toml/merge/emitter.rb', line 114 def emit_array_item(value, metadata: nil) append_line("#{current_indent}#{value},", ) end |
#emit_array_of_tables_header(name, inline_comment: nil, metadata: nil) ⇒ Object
Emit an array of tables header
68 69 70 71 72 |
# File 'lib/toml/merge/emitter.rb', line 68 def emit_array_of_tables_header(name, inline_comment: nil, metadata: nil) line = "[[#{name}]]" line += " # #{inline_comment}" if inline_comment append_line(line, ) end |
#emit_array_start(key, metadata: nil) ⇒ Object
Emit a multi-line array start
106 107 108 109 |
# File 'lib/toml/merge/emitter.rb', line 106 def emit_array_start(key, metadata: nil) append_line("#{current_indent}#{key} = [", ) indent end |
#emit_blank_line ⇒ Object
28 29 30 |
# File 'lib/toml/merge/emitter.rb', line 28 def emit_blank_line append_line('') end |
#emit_comment(text, inline: false) ⇒ Object
Emit a comment line
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/toml/merge/emitter.rb', line 43 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 append_line("#{current_indent}# #{text}") end end |
#emit_inline_array(key, items, metadata: nil) ⇒ Object
Emit an inline array
98 99 100 101 |
# File 'lib/toml/merge/emitter.rb', line 98 def emit_inline_array(key, items, metadata: nil) formatted_items = items.join(', ') append_line("#{current_indent}#{key} = [#{formatted_items}]", ) end |
#emit_inline_table(key, pairs, metadata: nil) ⇒ Object
Emit an inline table
89 90 91 92 |
# File 'lib/toml/merge/emitter.rb', line 89 def emit_inline_table(key, pairs, metadata: nil) formatted_pairs = pairs.map { |k, v| "#{k} = #{v}" }.join(', ') append_line("#{current_indent}#{key} = { #{formatted_pairs} }", ) end |
#emit_key_value(key, value, inline_comment: nil, metadata: nil) ⇒ Object
Emit a key-value pair
79 80 81 82 83 |
# File 'lib/toml/merge/emitter.rb', line 79 def emit_key_value(key, value, inline_comment: nil, metadata: nil) line = "#{current_indent}#{key} = #{value}" line += " # #{inline_comment}" if inline_comment append_line(line, ) end |
#emit_raw_lines(raw_lines, metadata: nil) ⇒ Object
124 125 126 127 128 |
# File 'lib/toml/merge/emitter.rb', line 124 def emit_raw_lines(raw_lines, metadata: nil) raw_lines.each_with_index do |line, idx| append_line(line.chomp, (, idx)) end end |
#emit_table_header(name, inline_comment: nil, metadata: nil) ⇒ Object
Emit a table header
58 59 60 61 62 |
# File 'lib/toml/merge/emitter.rb', line 58 def emit_table_header(name, inline_comment: nil, metadata: nil) line = "[#{name}]" line += " # #{inline_comment}" if inline_comment append_line(line, ) end |
#emit_tracked_comment(comment) ⇒ Object
Emit a tracked comment from CommentTracker
34 35 36 37 |
# File 'lib/toml/merge/emitter.rb', line 34 def emit_tracked_comment(comment) indent = ' ' * (comment[:indent] || 0) append_line("#{indent}# #{comment[:text]}") end |
#initialize_subclass_state(**_options) ⇒ Object
Initialize subclass-specific state
19 20 21 |
# File 'lib/toml/merge/emitter.rb', line 19 def initialize_subclass_state(**) end |
#to_toml ⇒ String
Get the output as a TOML string
133 134 135 |
# File 'lib/toml/merge/emitter.rb', line 133 def to_toml to_s end |