2013-04-27 14:07:14 +02:00
|
|
|
|
|
|
|
require 'forwardable'
|
|
|
|
|
|
|
|
module Dyndnsd
|
|
|
|
class Database
|
|
|
|
extend Forwardable
|
2018-02-23 12:54:43 +01:00
|
|
|
|
2013-04-27 14:07:14 +02:00
|
|
|
def_delegators :@db, :[], :[]=, :each, :has_key?
|
2018-02-23 12:54:43 +01:00
|
|
|
|
2013-04-27 14:07:14 +02:00
|
|
|
def initialize(db_file)
|
|
|
|
@db_file = db_file
|
|
|
|
end
|
2018-02-23 12:54:43 +01:00
|
|
|
|
2013-04-27 14:07:14 +02:00
|
|
|
def load
|
|
|
|
if File.file?(@db_file)
|
2018-02-23 12:54:43 +01:00
|
|
|
@db = JSON.parse(File.open(@db_file, 'r', &:read))
|
2013-04-27 14:07:14 +02:00
|
|
|
else
|
|
|
|
@db = {}
|
|
|
|
end
|
|
|
|
@db_hash = @db.hash
|
|
|
|
end
|
2018-02-23 12:54:43 +01:00
|
|
|
|
2013-04-27 14:07:14 +02:00
|
|
|
def save
|
2018-01-26 16:25:15 +01:00
|
|
|
Helper.span('database_save') do |_span|
|
|
|
|
File.open(@db_file, 'w') { |f| JSON.dump(@db, f) }
|
|
|
|
@db_hash = @db.hash
|
|
|
|
end
|
2013-04-27 14:07:14 +02:00
|
|
|
end
|
2018-02-23 12:54:43 +01:00
|
|
|
|
2013-04-27 14:07:14 +02:00
|
|
|
def changed?
|
|
|
|
@db_hash != @db.hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|