Class: Udb::Yaml::CommentParser

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/udb/yaml/comment_parser.rb

Overview

Parses YAML files and extracts comments with their positions

Instance Method Summary collapse

Instance Method Details

#parse(yaml_string) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/udb/yaml/comment_parser.rb', line 202

def parse(yaml_string)
  lines = yaml_string.lines
  comment_map = CommentMap.new

   = extract_comments(lines)
  data = Psych.safe_load(yaml_string, permitted_classes: [Date, Symbol], aliases: true) || {}
  line_to_path = build_line_to_path_map(yaml_string)
  detect_string_styles(yaml_string, line_to_path, comment_map)
  associate_comments(, line_to_path, comment_map, data)

  { data: data, comments: comment_map }
end

#parse_file(file_path) ⇒ Object



218
219
220
# File 'lib/udb/yaml/comment_parser.rb', line 218

def parse_file(file_path)
  parse(File.read(file_path, encoding: "utf-8"))
end