Sha256: 74ad9b4f148bb01d46ef1a1b905a2b394d6868d7b91a24e35a46289c4de5fcb4
Contents?: true
Size: 1.2 KB
Versions: 5
Compression:
Stored size: 1.2 KB
Contents
require 'donjon/encrypted_file' require 'json' module Donjon class Database include Enumerable def initialize(actor:) @actor = actor end def [](key) file = _file(key) return unless file.readable? _key, value = _unpack(file.read) assert(key == _key, "bad stored data for #{key}!") return value end def []=(key, value) if value.nil? _file(key).write(nil) else _file(key).write(_pack(key, value)) end end def each parent = @actor.repo.join('data') return unless parent.exist? parent.children.each do |child| path = "data/#{child.basename}" file = EncryptedFile.new(path: path, actor: @actor) next unless file.readable? yield *_unpack(file.read) end end def update each do |key, value| self[key] = value end nil end private def _pack(key, value) JSON.dump([key, value]) end def _unpack(data) JSON.parse(data) end def _hash(key) OpenSSL::Digest::SHA256.hexdigest(key) end def _file(key) EncryptedFile.new(path: "data/#{_hash(key)}", actor: @actor) end end end
Version data entries
5 entries across 5 versions & 1 rubygems
Version | Path |
---|---|
donjon-2.0.1 | lib/donjon/database.rb |
donjon-2.0.0 | lib/donjon/database.rb |
donjon-1.0.1 | lib/donjon/database.rb |
donjon-1.0.0 | lib/donjon/database.rb |
donjon-0.0.5 | lib/donjon/database.rb |