25 lines
561 B
Ruby
25 lines
561 B
Ruby
# 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
|