Class: Psych::Merge::Emitter
- Inherits:
-
Ast::Merge::EmitterBase
- Object
- Ast::Merge::EmitterBase
- Psych::Merge::Emitter
- 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.
Instance Method Summary collapse
-
#clear_subclass_state ⇒ Object
Clear subclass-specific state.
-
#emit_alias(key, anchor, metadata: nil) ⇒ Object
Emit an alias reference.
- #emit_blank_line ⇒ Object
-
#emit_comment(text, inline: false) ⇒ Object
Emit a comment line.
-
#emit_mapping_end ⇒ Object
Emit a mapping end.
-
#emit_mapping_start(key, anchor: nil, metadata: nil) ⇒ Object
Emit a mapping start (for nested mappings).
-
#emit_merge_key(anchor, metadata: nil) ⇒ Object
Emit a merge key with alias.
- #emit_raw_lines(raw_lines, metadata: nil) ⇒ Object
-
#emit_scalar_entry(key, value, style: :plain, inline_comment: nil, metadata: nil) ⇒ Object
Emit a scalar value.
-
#emit_sequence_end ⇒ Object
Emit a sequence end.
-
#emit_sequence_item(value, inline_comment: nil, metadata: nil) ⇒ Object
Emit a sequence item.
-
#emit_sequence_start(key, anchor: nil, metadata: nil) ⇒ Object
Emit a sequence start.
-
#emit_tracked_comment(comment) ⇒ Object
Emit a tracked comment from CommentTracker.
-
#initialize_subclass_state(**_options) ⇒ Object
Initialize subclass-specific state.
-
#to_yaml ⇒ String
Get the output as a YAML string.
Instance Method Details
#clear_subclass_state ⇒ Object
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
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_line ⇒ Object
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
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_end ⇒ Object
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)
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
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
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_end ⇒ Object
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
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
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
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(**) end |
#to_yaml ⇒ String
Get the output as a YAML string
132 133 134 |
# File 'lib/psych/merge/emitter.rb', line 132 def to_yaml to_s end |