Method: GSD::Database#modify

Defined in:
lib/gsd/database.rb

#modify(file_path) {|new_gsd_entry| ... } ⇒ Object

Yields:

  • (new_gsd_entry)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/gsd/database.rb', line 30

def modify(file_path, &block)
  raw_json_data = File.read(file_path)
  old_gsd_entry = JSON.parse(raw_json_data)
  new_gsd_entry = old_gsd_entry.deep_dup

  yield new_gsd_entry

  if new_gsd_entry != old_gsd_entry
    indent = json_indent_value(
      parsed_json: old_gsd_entry,
      raw_json: raw_json_data,
      gsd_id: new_gsd_entry['gsd']['osvSchema']['id']
    )
    # Sort by key and include a trailing newline
    contents = json_string(input: new_gsd_entry.sort.to_h, indent: indent) + "\n"
    File.write(file_path, contents)
    add_file(file_path)
    puts "Staged changes!"
  else
    puts "No changes!"
  end
end