Debug lib & refactor

master
E. Almqvist 3 years ago
parent 2d86e1d139
commit 39fb587af2
  1. 4
      src/app.rb
  2. 27
      src/database.rb
  3. 14
      src/debug.rb

@ -1,7 +1,9 @@
#!/usr/bin/ruby -w
DEBUG = ARGV[0] == "debug"
require "sinatra"
require "sinatra/reloader" if ARGV[0] == "debug"
require "sinatra/reloader" if DEBUG
require "slim"
require "sqlite3"
require "sassc"

@ -2,11 +2,24 @@ DEFAULT_DB_PATH = "db/main.db"
require_relative "db_models.rb"
class Table
attr_reader :name
attr_accessor :db
def initialize(db, name)
@db = db
@name = name
end
end
class Database # Database class
attr_reader :path
def initialize(table_structure, db_path=DEFAULT_DB_PATH)
attr_reader :name, :path
def initialize(name, table_structure, db_path=DEFAULT_DB_PATH)
@name = name
@path = db_path
# generate table_structure if it doesn't exist
@tables = {}
# generate table objects
end
private def db
@ -37,17 +50,17 @@ class Database # Database class
self.query query # execute query
end
def update(table, data, filter="")
def update(table, data, filter="") # Updates the table with specified data hash
self.query( "UPDATE #{table} SET #{ self.gen_update_query(data.keys) } WHERE #{filter}", *data.values )
end
def insert(table, data, filter="")
def insert(table, data, filter="") # Inserts new data into the table
entstr, valstr = gen_insert_query data.keys
self.query( "INSERT INTO #{table} #{entstr} VALUES #{valstr}", *data.values )
end
# sets or updates a specific field in the table
def set(table, data={}, filter="") # slow, shouldn't really be used
def set(table, data, filter="") # slower but more lazy
if self.get(table, filter).length > 0 then
self.update(table, data, filter)
else
@ -55,7 +68,3 @@ class Database # Database class
end
end
end
class TableModel
end

@ -0,0 +1,14 @@
def log(str, *args)
puts "#{str}"
args.each do |s|
puts " :: #{s}"
end
end
def debug(str, *args)
if DEBUG then
print "DEBUG :: "
log "#{str}", *args
end
end
Loading…
Cancel
Save