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



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

Parameters:

  • value (String)

    Item value (already formatted)



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

Parameters:

  • name (String)

    Array of tables name



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

Parameters:

  • key (String)

    Key name



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

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)



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

Parameters:

  • key (String)

    Key name

  • pairs (Hash)

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

Parameters:

  • key (String)

    Key name

  • value (String)

    Value (already formatted as TOML)

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

    Optional inline comment



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

Parameters:

  • name (String)

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



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

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)


115
116
117
# File 'lib/toml/merge/emitter.rb', line 115

def to_toml
  to_s
end