Class: Psych::Merge::Emitter

Inherits:
Ast::Merge::EmitterBase
  • Object
show all
Defined in:
lib/psych/merge/emitter.rb

Overview

Custom YAML emitter that preserves comments and formatting.
This class provides utilities for emitting YAML 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_mapping_entry(key, value, leading_comments: comments)

Instance Method Summary collapse

Instance Method Details

#clear_subclass_stateObject

Clear subclass-specific state



21
22
23
# File 'lib/psych/merge/emitter.rb', line 21

def clear_subclass_state
  # Nothing to clear for YAML
end

#emit_alias(key, anchor) ⇒ Object

Emit an alias reference

Parameters:

  • key (String)

    Key name

  • anchor (String)

    Anchor name being referenced (without *)



106
107
108
# File 'lib/psych/merge/emitter.rb', line 106

def emit_alias(key, anchor)
  @lines << "#{current_indent}#{key}: *#{anchor}"
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



36
37
38
39
40
41
42
43
44
45
# File 'lib/psych/merge/emitter.rb', line 36

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_mapping_endObject

Emit a mapping end



71
72
73
# File 'lib/psych/merge/emitter.rb', line 71

def emit_mapping_end
  dedent
end

#emit_mapping_start(key, anchor: nil) ⇒ Object

Emit a mapping start (for nested mappings)

Parameters:

  • key (String)

    Key name

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

    Anchor name (without &)



64
65
66
67
68
# File 'lib/psych/merge/emitter.rb', line 64

def emit_mapping_start(key, anchor: nil)
  anchor_str = anchor ? " &#{anchor}" : ""
  @lines << "#{current_indent}#{key}:#{anchor_str}"
  indent
end

#emit_merge_key(anchor) ⇒ Object

Emit a merge key with alias

Parameters:

  • anchor (String)

    Anchor name to merge (without *)



113
114
115
# File 'lib/psych/merge/emitter.rb', line 113

def emit_merge_key(anchor)
  @lines << "#{current_indent}<<: *#{anchor}"
end

#emit_scalar_entry(key, value, style: :plain, inline_comment: nil) ⇒ Object

Emit a scalar value

Parameters:

  • key (String)

    Key name

  • value (String)

    Value

  • style (Symbol) (defaults to: :plain)

    Style (:plain, :single_quoted, :double_quoted, :literal, :folded)

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

    Optional inline comment



53
54
55
56
57
58
# File 'lib/psych/merge/emitter.rb', line 53

def emit_scalar_entry(key, value, style: :plain, inline_comment: nil)
  formatted_value = format_scalar(value, style)
  line = "#{current_indent}#{key}: #{formatted_value}"
  line += " # #{inline_comment}" if inline_comment
  @lines << line
end

#emit_sequence_endObject

Emit a sequence end



98
99
100
# File 'lib/psych/merge/emitter.rb', line 98

def emit_sequence_end
  dedent
end

#emit_sequence_item(value, inline_comment: nil) ⇒ Object

Emit a sequence item

Parameters:

  • value (String)

    Item value

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

    Optional inline comment



91
92
93
94
95
# File 'lib/psych/merge/emitter.rb', line 91

def emit_sequence_item(value, inline_comment: nil)
  line = "#{current_indent}- #{value}"
  line += " # #{inline_comment}" if inline_comment
  @lines << line
end

#emit_sequence_start(key, anchor: nil) ⇒ Object

Emit a sequence start

Parameters:

  • key (String, nil)

    Key name (nil for inline sequence)

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

    Anchor name



79
80
81
82
83
84
85
# File 'lib/psych/merge/emitter.rb', line 79

def emit_sequence_start(key, anchor: nil)
  if key
    anchor_str = anchor ? " &#{anchor}" : ""
    @lines << "#{current_indent}#{key}:#{anchor_str}"
    indent
  end
end

#emit_tracked_comment(comment) ⇒ Object

Emit a tracked comment from CommentTracker

Parameters:

  • comment (Hash)

    Comment with :text, :indent



27
28
29
30
# File 'lib/psych/merge/emitter.rb', line 27

def emit_tracked_comment(comment)
  indent = " " * (comment[:indent] || 0)
  @lines << "#{indent}# #{comment[:text]}"
end

#initialize_subclass_state(**options) ⇒ Object

Initialize subclass-specific state



16
17
18
# File 'lib/psych/merge/emitter.rb', line 16

def initialize_subclass_state(**options)
  # YAML doesn't need comma tracking like JSON
end

#to_yamlString

Get the output as a YAML string

Returns:

  • (String)


120
121
122
# File 'lib/psych/merge/emitter.rb', line 120

def to_yaml
  to_s
end