Skip to content

@solomon081 /keydb.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
KeyDB
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.