class Donjon::Database
Public Class Methods
new(actor:)
click to toggle source
# File lib/donjon/database.rb, line 8 def initialize(actor:) @actor = actor end
Public Instance Methods
[](key)
click to toggle source
# File lib/donjon/database.rb, line 12 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
[]=(key, value)
click to toggle source
# File lib/donjon/database.rb, line 20 def []=(key, value) if value.nil? _file(key).write(nil) else _file(key).write(_pack(key, value)) end end
each() { |*_unpack(read)| ... }
click to toggle source
# File lib/donjon/database.rb, line 28 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
update()
click to toggle source
# File lib/donjon/database.rb, line 39 def update each do |key, value| self[key] = value end nil end
Private Instance Methods
_file(key)
click to toggle source
# File lib/donjon/database.rb, line 61 def _file(key) EncryptedFile.new(path: "data/#{_hash(key)}", actor: @actor) end
_hash(key)
click to toggle source
# File lib/donjon/database.rb, line 57 def _hash(key) OpenSSL::Digest::SHA256.hexdigest(key) end
_pack(key, value)
click to toggle source
# File lib/donjon/database.rb, line 49 def _pack(key, value) JSON.dump([key, value]) end
_unpack(data)
click to toggle source
# File lib/donjon/database.rb, line 53 def _unpack(data) JSON.parse(data) end