Skip to content

Instantly share code, notes, and snippets.

@thomasjslone
Last active October 29, 2021 20:31
Show Gist options
  • Save thomasjslone/11dba23ca3f124fbda8a55c0dcb2657f to your computer and use it in GitHub Desktop.
Save thomasjslone/11dba23ca3f124fbda8a55c0dcb2657f to your computer and use it in GitHub Desktop.
dex.rb
## to_b methods dependant on ossy string class
class Dex
def initialize
## create data folder if it doesnt exist
@data_path=HOMEDIR+"/app/dex/data"
if File.exist?(@data_path)==false
Dir.mkdir(HOMEDIR+"/app/dex/data")
end
@bankfile=0
if File.exist?(@data_path+"/"[email protected]_s+".db")==false
f=File.open(@data_path+"/"[email protected]_s+".db","w");f.close
end
@database=[]
self.load_data
end
def database *args
if args.length==0;return @database
elsif args[0].is_a?(Array);@database=args[0]
end
end
def new_entry(name,data)
id = @database.length+1 ; e=Entry.new(id.to_s,name.to_s,data.to_s) ; @database << e
d=id.to_s+","+name.to_s.to_b+","+data.to_s.to_b
f=File.open(@data_path+"/"[email protected]_s+".db","a") ; f.write(d.to_s+"\n") ; f.close
return @database.length
end
def load_data
d=Dir.entries(@data_path)
loadlist=[]
db=[]
if d.length>2
d[2..-1].each do |f|
loadlist<<@data_path+"/"+f.to_s
end
loadlist.each do |l|
f=File.open(l,"r");d=f.read.to_s.split("\n");f.close
if d.length>0
d.each do |i|
i = i.split(",")
db<< [i[0].to_s,i[1].to_s.from_b,i[2].to_s.from_b]
end
end
end
if db.length>0
db.each do |i|
e=Entry.new;e.sets(i[0],i[1],i[2])
@database << e
end
end
end
return @database.length
end
############################################################
class Entry
def initialize *args # [id,name,data]
@id=args[0].to_s
@name=args[1].to_s
@data=args[2].to_s
return @id
end
def read
end
def gets
return @id.to_s+","+name.to_s.to_b+","+data.to_s.to_b
end
def sets id, name, data
@id=id.to_s;@name=name.to_s;@data=data.to_s
end
end
###########################################################
end
$dex=Dex.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment