Class: Toml::Merge::Emitter

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

Examples:

Basic usage

emitter = Emitter.new
emitter.emit_table_header("section")
emitter.emit_key_value("key", "value")

Instance Method Summary collapse

Instance Method Details

#clear_subclass_stateObject

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_endObject

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

Parameters:

  • value (String)

    Item value (already formatted)



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

Parameters:

  • name (String)

    Array of tables name

  • inline_comment (String, nil) (defaults to: nil)

    Optional inline comment to keep on the header line



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

Parameters:

  • key (String)

    Key name



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

Parameters:

  • text (String)

    Comment text (without #)

  • inline (Boolean) (defaults to: false)

    Whether this is an inline comment



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

Parameters:

  • key (String)

    Key name

  • items (Array)

    Array items (already formatted)



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

Parameters:

  • key (String)

    Key name

  • pairs (Hash)

    Key-value pairs for the 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

Parameters:

  • key (String)

    Key name

  • value (String)

    Value (already formatted as TOML)

  • inline_comment (String, nil) (defaults to: nil)

    Optional inline comment



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

Parameters:

  • name (String)

    Table name (e.g., “package” or “dependencies.dev”)

  • inline_comment (String, nil) (defaults to: nil)

    Optional inline comment to keep on the header line



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

Parameters:

  • comment (Hash)

    Comment with :text, :indent



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(**options)
  # TOML doesn't need separator tracking like JSON
end

#to_tomlString

Get the output as a TOML string

Returns:

  • (String)


121
122
123
# File 'lib/toml/merge/emitter.rb', line 121

def to_toml
  to_s
end