cfile.rb
rbmud-0.2/lib/cfile.rb · 3.3 KB
From the archive RbMUD-0.2.tar.bz2
Download the archive « Back to the archive contents
#!/usr/local/bin/ruby
# $Revision: 1.29 $
# $Date: 2003/12/04 23:25:31 $
# $Author: mikeman2, jefus $
#cfile.rb -- Simple filesystem on top of MySQL
#***Avoid using wierd symbols in owners, drawers, and files like ";", "::" and ":"***
#usage:
# cfile = CFile.new("disco:mobs:secretary")
# cfile.write("name", "disco's secretary")
# @secretaryname = cfile.attributes['name'] # @secretaryname = "name"
# array << stuff
# array << morestuff
# cfile.write("anarray", array)
# CFile.write("disco:mob:secretary", "anarray", array) # shortcut
# cfile.attributes['anarray'].each{|element| p element } #outputs: "stuff\nmorestuff"
# eqarray = cfile.attributes['aneqarray']
# eqarray = CFile.attributes("disco:mob:secretary")['eqarray'] #shortcut
#
# Note: files and drawers will be created on demand by CFile::new
require 'mysql'
begin
address = File.open("mysql.dat", "r").gets.chomp.split(/:/)
rescue
Logger.log("Couldn't load database info from mysql.dat!")
exit
end
DB = Mysql.new(address[0], address[1], address[2], address[3])
class CFile
def initialize(fname)
@filename = fname
fname = fname.split(":")
@owner = fname[0]
@drawer = fname[1]
@file = fname[2]
@table = "#{@owner}_#{@drawer}_#{@file}"
@db = DB
createTable if !tableExists?
return self
end
def filename
return @filename
end
def CFile.files(owner, drawer)
filearr = Array.new
DB.query("SELECT files,owner FROM drawers WHERE drawer LIKE '#{drawer}'").each_hash do |a|
if a['owner'] == owner
if a['files'].include?(";")
a['files'].split(";").each{|file|
filearr << file
}
else
filearr << a['files']
end
else
return "This CFile does not have access to #{owner}:#{drawer}." #raise exception
end
end
return filearr
end
def getData(owner = @owner, drawer = @drawer, file = @file, table = @table)
datahash = Hash.new
DB.query("SELECT * FROM #{table}").each_hash do |a|
if a['value'].include?("::")
datahash.store(a['key_'], a['value'].split("::"))
else
datahash.store(a['key_'], a['value']) if a['key_'] != nil
end
end
return datahash
end
def attributes
return CFile.new(@filename).getData
end
def [](key)
data = CFile.new(@filename).getData
return data.fetch(key) if data.key?(key)
end
def []=(key, value)
if value.instance_of?(Array)
@db.query("UPDATE `#{@table}` SET value='#{value.join("::")}' WHERE key_='#{key}'") if attributes.key?(key)
@db.query("INSERT INTO `#{@table}` VALUES ('#{key}', '#{value.join("::")}')") if !attributes.key?(key)
elsif value.instance_of?(String)
@db.query("UPDATE #{@table} SET value='#{value}' WHERE key_='#{key}'") if attributes.key?(key)
@db.query("INSERT INTO `#{@table}` VALUES ('#{key}', '#{value}')") if !attributes.key?(key)
end
return
end
def createTable
@db.query("CREATE TABLE `#{@table}` (`key_` VARCHAR(50) NOT NULL, `value` TEXT, UNIQUE KEY `key_` (`key_`))")
end
def tableExists?(table = @table)
begin
@db.query("SELECT `key_` FROM `#{table}`")
rescue
return false
else
return true
end
end
def CFile.write(fname, key, value)
CFile.new(fname)[key] = value
return
end
end