Class: Udb::Yaml::PreservingEmitter
- Inherits:
-
Object
- Object
- Udb::Yaml::PreservingEmitter
- Extended by:
- T::Sig
- Defined in:
- lib/udb/yaml/preserving_emitter.rb
Overview
Emits YAML while preserving comments and formatting
Instance Method Summary collapse
- #emit(data, io = nil) ⇒ Object
- #emit_file(data, file_path) ⇒ Object
-
#initialize(comment_map = nil) ⇒ PreservingEmitter
constructor
A new instance of PreservingEmitter.
Constructor Details
#initialize(comment_map = nil) ⇒ PreservingEmitter
Returns a new instance of PreservingEmitter.
19 20 21 |
# File 'lib/udb/yaml/preserving_emitter.rb', line 19 def initialize(comment_map = nil) @comment_map = T.let(comment_map || CommentMap.new, CommentMap) end |
Instance Method Details
#emit(data, io = nil) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/udb/yaml/preserving_emitter.rb', line 29 def emit(data, io = nil) output = StringIO.new @comment_map.header_comments.each do |comment| content = comment.content.strip output.puts "# #{content}" unless content.empty? end output.puts if @comment_map.header_comments.any? emit_value(data, output, [], 0) @comment_map.trailing_comments.each do |comment| output.puts "#{" " * comment.indent}# #{comment.content}" end source_locations = @comment_map.all_source_locations if source_locations.any? output.puts output.puts "# ===== SOURCE MAP BEGIN =====" output.puts "# This map tracks the original source file and line:column for each key" output.puts "# Format: key_path -> file:line:column" source_locations.keys.sort.each do |path_key| location = source_locations.fetch(path_key) output.puts "# #{path_key} -> #{location[:file]}:#{location[:line]}:#{location[:column]}" end output.puts "# ===== SOURCE MAP END =====" end result = output.string if io if io.is_a?(String) File.write(io, result) else io.write(result) end end result end |
#emit_file(data, file_path) ⇒ Object
79 80 81 |
# File 'lib/udb/yaml/preserving_emitter.rb', line 79 def emit_file(data, file_path) emit(data, file_path.to_s) end |