This repository has been archived on 2024-06-24. You can view files and clone it, but cannot push or open issues or pull requests.
ace-of-base/lib/ace_of_base/record_serialiser.rb

26 lines
561 B
Ruby
Raw Normal View History

2019-07-10 21:03:07 +12:00
# frozen_string_literal: true
require 'yaml'
module AceOfBase
# Implements serialisation for our records.
module RecordSerialiser
Error = Class.new(AceOfBase::Error)
module_function
def encode(record)
YAML.dump(record)
end
def decode(data)
record = YAML.safe_load(data, permitted_classes: [AceOfBase::Record, DateTime, Time])
raise Error, 'Unable to load invalid record.' unless record.is_a?(Record)
record
rescue Psych::SyntaxError
raise Error, 'Unable to load invalid record.'
end
end
end