Class: Psych::Merge::Emitter

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



23
24
25
# File 'lib/psych/merge/emitter.rb', line 23

def clear_subclass_state
  
end

#emit_alias(key, anchor, metadata: nil) ⇒ Object

Emit an alias reference

Parameters:

  • key (String)

    Key name

  • anchor (String)

    Anchor name being referenced (without *)



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

def emit_alias(key, anchor, metadata: nil)
  append_line("#{current_indent}#{key}: *#{anchor}", )
end

#emit_blank_lineObject



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

def emit_blank_line
  append_line('')
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



42
43
44
45
46
47
48
49
50
51
# File 'lib/psych/merge/emitter.rb', line 42

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_mapping_endObject

Emit a mapping end



77
78
79
# File 'lib/psych/merge/emitter.rb', line 77

def emit_mapping_end
  dedent
end

#emit_mapping_start(key, anchor: nil, metadata: nil) ⇒ Object

Emit a mapping start (for nested mappings)

Parameters:

  • key (String)

    Key name

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

    Anchor name (without &)



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

def emit_mapping_start(key, anchor: nil, metadata: nil)
  anchor_str = anchor ? " &#{anchor}" : ''
  append_line("#{current_indent}#{key}:#{anchor_str}", )
  indent
end

#emit_merge_key(anchor, metadata: nil) ⇒ Object

Emit a merge key with alias

Parameters:

  • anchor (String)

    Anchor name to merge (without *)



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

def emit_merge_key(anchor, metadata: nil)
  append_line("#{current_indent}<<: *#{anchor}", )
end

#emit_raw_lines(raw_lines, metadata: nil) ⇒ Object



123
124
125
126
127
# File 'lib/psych/merge/emitter.rb', line 123

def emit_raw_lines(raw_lines, metadata: nil)
  raw_lines.each_with_index do |line, idx|
    append_line(line.chomp, (, idx))
  end
end

#emit_scalar_entry(key, value, style: :plain, inline_comment: nil, metadata: 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



59
60
61
62
63
64
# File 'lib/psych/merge/emitter.rb', line 59

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

#emit_sequence_endObject

Emit a sequence end



104
105
106
# File 'lib/psych/merge/emitter.rb', line 104

def emit_sequence_end
  dedent
end

#emit_sequence_item(value, inline_comment: nil, metadata: nil) ⇒ Object

Emit a sequence item

Parameters:

  • value (String)

    Item value

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

    Optional inline comment



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

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

#emit_sequence_start(key, anchor: nil, metadata: nil) ⇒ Object

Emit a sequence start

Parameters:

  • key (String, nil)

    Key name (nil for inline sequence)

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

    Anchor name



85
86
87
88
89
90
91
# File 'lib/psych/merge/emitter.rb', line 85

def emit_sequence_start(key, anchor: nil, metadata: nil)
  return unless key

  anchor_str = anchor ? " &#{anchor}" : ''
  append_line("#{current_indent}#{key}:#{anchor_str}", )
  indent
end

#emit_tracked_comment(comment) ⇒ Object

Emit a tracked comment from CommentTracker

Parameters:

  • comment (Hash)

    Comment with :text, :indent



33
34
35
36
# File 'lib/psych/merge/emitter.rb', line 33

def emit_tracked_comment(comment)
  indent = ' ' * (comment[:indent] || 0)
  append_line("#{indent}# #{comment[:text]}")
end

#initialize_subclass_state(**_options) ⇒ Object

Initialize subclass-specific state



18
19
20
# File 'lib/psych/merge/emitter.rb', line 18

def initialize_subclass_state(**_options)
  
end

#to_yamlString

Get the output as a YAML string

Returns:

  • (String)


132
133
134
# File 'lib/psych/merge/emitter.rb', line 132

def to_yaml
  to_s
end