public
Last active

KeyDB

  • Download Gist
keydb.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
module KeyDB
class DB
# Makes filename and version into attributes
attr_reader :filename, :version
# Global version variable
$KEYDB_VERSION = "5.0.0"
 
def initialize filename
# Makes sure the filename ends in .db
@filename = filename.split(".")
@filename = "#{@filename[0]}.db"
# Checks if file exists
unless Dir.entries('.').detect {|f| f.match "#{@filename}"}
@db_file = File.new(@filename, "w")
@db_file.close
end
end
 
def change_key keyname, value
@db_file = File.open(@filename, "a")
# Writes a changekey sequence
@db_file.write(Marshal.dump("CK-***-#{keyname}-***-#{value}"))
@db_file.write("\n")
@db_file.close
end
 
def delete_key keyname
@db_file = File.open(@filename, "a")
# Writes a deletekey sequence
@db_file.write(Marshal.dump("DK(---)#{keyname}"))
@db_file.write("\n")
@db_file.close
end
 
def to_s
# Returns a string representation
return "KeyDB-#{@filename}"
end
 
def keys
# Creates a new array of keys to ignore
to_ignore = Array.new
File.open(@filename).each_line do |line|
if Marshal.load(line).split("(---)")[1] != nil
# Pushes deleted keys
to_ignore.push(Marshal.load(line).split("(---)")[1])
elsif Marshal.load(line).split("-***-")[1] != nil
# Pushes changed keys
to_ignore.push(Marshal.load(line).split("-***-")[1])
end
end
puts to_ignore
File.open(@filename).each_line do |line|
line = Marshal.load(line)
@keys_and_vals = line.split('///')
# Checks if key is ignored
if line.split("(---)").length == 2 or to_ignore.include? line.split("///")[0]
print
elsif to_ignore.include? line.split("-***-")[1]
print
else
# Prints valid keys
puts @keys_and_vals[0]
end
end
end
 
def delete_db
# Deletes the database
system("rm #{@filename}")
end
 
def wipe_db
# Creates a new, clean database
delete_db
File.new(@filename, "w")
end
 
def method_missing(keyname, *value)
# Converts the keyname (method) to a string
keyname = keyname.to_s
# Checks if it is an accessor method
if keyname =~ /=/
keyname.gsub!("=", "")
value = value[0]
# Validates the keyname
if keyname =~ /\/\/\/|\(---\)|-\*\*\*-/
raise "Invalid Keyname!"
return
end
File.open(@filename, "r").each_line do |line|
if Marshal.load(line).split("///")[0] == keyname
# Checks key for uniqueness
raise "Keynames must be unique"
return
end
end
@db_file = File.open(@filename, "a")
# Writes key
@db_file.write(Marshal.dump("#{keyname}///#{value}"))
@db_file.write("\n")
@db_file.close
else
File.open(@filename, "r").each_line do |line|
# Checks if key has been deleted
if Marshal.load(line).split("(---)")[1] == keyname
return "Key has been deleted"
# Checks for a changekey sequence
elsif Marshal.load(line).split("-***-")[1] == keyname
return Marshal.load(line).split("-***-")[2]
end
end
File.open(@filename, "r").each_line do |line|
line = Marshal.load(line)
# Returns unchanged, undeleted keys
if line.split("///")[0] == keyname
return line.split("///")[1]
end
end
end
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.