module OpenRAReplay::MiniYaml

Public Class Methods

dump(object) click to toggle source
# File lib/openrareplay/miniyaml.rb, line 46
def self.dump(object)
  thing = (Psych.dump object, indentation: 1)
          .force_encoding('ASCII-8BIT').gsub(/^---\n/m, '')
          .gsub('  ', "\t").gsub(': false', ': False')
          .gsub(': true', ': True').gsub(": \n", ":\n")
  searches = []
  thing.scan(/: '(.+?)'\n/m).each do |match|
    match = match.first
    searches.append [
      ": '#{match}'\n",
      ": #{match.gsub("''", "'")}\n"
    ]
  end
  thing.scan(/: "(.+?)"\n/m).each do |match|
    match = match.first
    searches.append [
      ": \"#{match}\"\n",
      ": #{match.gsub('\"', '"').gsub('\\\\', '\\')}\n"
    ]
  end
  searches.each do |pair|
    thing.gsub!(pair.first, pair.last)
  end
  thing
end
dump_time(time) click to toggle source
# File lib/openrareplay/miniyaml.rb, line 76
def self.dump_time(time)
  time.utc.strftime('%Y-%m-%d %H-%M-%S')
end
load(str) click to toggle source
# File lib/openrareplay/miniyaml.rb, line 26
def self.load(str)
  thing = str.gsub("\t", '  ')
  searches = []
  thing.scan(/MapTitle: (.+?)\n/m).each do |match|
    match = match.first
    searches.append [
      "MapTitle: #{match}\n",
      "MapTitle: '#{match.gsub("'", "''")}'\n"
    ]
  end
  thing.scan(/Name: (.+?)\n/m).each do |match|
    match = match.first
    searches.append ["Name: #{match}\n", "Name: '#{match.gsub("'", "''")}'\n"]
  end
  searches.each do |pair|
    thing.gsub!(pair.first, pair.last)
  end
  (Psych.safe_load thing)
end
load_time(str) click to toggle source
# File lib/openrareplay/miniyaml.rb, line 72
def self.load_time(str)
  Time.strptime(str + ' UTC', '%Y-%m-%d %H-%M-%S %Z')
end