Class: Psych::Merge::Emitter
- Inherits:
-
Ast::Merge::EmitterBase
- Object
- Ast::Merge::EmitterBase
- Psych::Merge::Emitter
- 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) ⇒ Object
Emit an alias reference.
-
#emit_comment(text, inline: false) ⇒ Object
Emit a comment line.
-
#emit_mapping_end ⇒ Object
Emit a mapping end.
-
#emit_mapping_start(key, anchor: nil) ⇒ Object
Emit a mapping start (for nested mappings).
-
#emit_merge_key(anchor) ⇒ Object
Emit a merge key with alias.
-
#emit_scalar_entry(key, value, style: :plain, inline_comment: nil) ⇒ Object
Emit a scalar value.
-
#emit_sequence_end ⇒ Object
Emit a sequence end.
-
#emit_sequence_item(value, inline_comment: nil) ⇒ Object
Emit a sequence item.
-
#emit_sequence_start(key, anchor: 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
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
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
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_end ⇒ Object
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)
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
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
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_end ⇒ Object
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
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
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
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(**) # YAML doesn't need comma tracking like JSON end |
#to_yaml ⇒ String
Get the output as a YAML string
120 121 122 |
# File 'lib/psych/merge/emitter.rb', line 120 def to_yaml to_s end |