Added Sequel ORM for database handling; added navbar
This commit is contained in:
parent
6f680e72e5
commit
6c1c8bca0a
3
.gitignore
vendored
3
.gitignore
vendored
@ -56,3 +56,6 @@ build-iPhoneSimulator/
|
||||
# Used by RuboCop. Remote config files pulled in from inherit_from directive.
|
||||
# .rubocop-https?--*
|
||||
|
||||
# Ignore local data
|
||||
data/
|
||||
|
||||
|
4
Gemfile
4
Gemfile
@ -4,8 +4,10 @@ gem 'sinatra', '~> 3.0'
|
||||
gem 'sinatra-contrib', '~> 3.0'
|
||||
gem 'puma', '~> 6.3'
|
||||
|
||||
gem 'sequel', '~> 5.70'
|
||||
gem 'sqlite3', '~> 1.6'
|
||||
|
||||
group :development, :test do
|
||||
gem 'rerun'
|
||||
gem 'wdm', '>= 0.1.0' if Gem.win_platform?
|
||||
end
|
||||
|
||||
|
@ -20,6 +20,7 @@ GEM
|
||||
rerun (0.14.0)
|
||||
listen (~> 3.0)
|
||||
ruby2_keywords (0.0.5)
|
||||
sequel (5.70.0)
|
||||
sinatra (3.0.6)
|
||||
mustermann (~> 3.0)
|
||||
rack (~> 2.2, >= 2.2.4)
|
||||
@ -31,6 +32,7 @@ GEM
|
||||
rack-protection (= 3.0.6)
|
||||
sinatra (= 3.0.6)
|
||||
tilt (~> 2.0)
|
||||
sqlite3 (1.6.3-x86_64-linux)
|
||||
tilt (2.2.0)
|
||||
|
||||
PLATFORMS
|
||||
@ -39,8 +41,10 @@ PLATFORMS
|
||||
DEPENDENCIES
|
||||
puma (~> 6.3)
|
||||
rerun
|
||||
sequel (~> 5.70)
|
||||
sinatra (~> 3.0)
|
||||
sinatra-contrib (~> 3.0)
|
||||
sqlite3 (~> 1.6)
|
||||
|
||||
BUNDLED WITH
|
||||
2.3.5
|
||||
|
6
Rakefile
6
Rakefile
@ -7,6 +7,12 @@ task :setup do
|
||||
system("bundle install")
|
||||
end
|
||||
|
||||
namespace :db do
|
||||
task :migrate do
|
||||
%x{sequel -m 'db/migrations/' 'sqlite://data/gamedata.db'}
|
||||
end
|
||||
end
|
||||
|
||||
namespace :server do
|
||||
task :start do
|
||||
ENV['APP_ENV'] = 'production'
|
||||
|
@ -1,3 +1,7 @@
|
||||
# Load application config
|
||||
require_relative 'src/config.rb'
|
||||
$conf = Config.new(File.join(__dir__, 'config/defaults.yaml'))
|
||||
|
||||
root = ::File.dirname(__FILE__)
|
||||
require ::File.join( root, 'src', 'server' )
|
||||
run GameData.new
|
||||
|
3
config/defaults.yaml
Normal file
3
config/defaults.yaml
Normal file
@ -0,0 +1,3 @@
|
||||
database:
|
||||
adapter: 'sqlite'
|
||||
database: 'data/gamedata.db'
|
0
data/.gitkeep
Normal file
0
data/.gitkeep
Normal file
40
db/migrations/0001_add_initial_tables.rb
Normal file
40
db/migrations/0001_add_initial_tables.rb
Normal file
@ -0,0 +1,40 @@
|
||||
Sequel.migration do
|
||||
|
||||
up do
|
||||
create_table(:hardware) do
|
||||
primary_key :id
|
||||
String :name, null: false
|
||||
String :type, null: false
|
||||
DateTime :created_at, default: Sequel::CURRENT_TIMESTAMP
|
||||
DateTime :updated_at, default: Sequel::CURRENT_TIMESTAMP
|
||||
end
|
||||
|
||||
create_table(:benchmarks) do
|
||||
primary_key :id
|
||||
String :name, null: false
|
||||
String :description
|
||||
String :scoring, null: false
|
||||
DateTime :created_at, default: Sequel::CURRENT_TIMESTAMP
|
||||
DateTime :updated_at, default: Sequel::CURRENT_TIMESTAMP
|
||||
end
|
||||
|
||||
create_table(:results) do
|
||||
primary_key :id
|
||||
String :name, null: false
|
||||
String :score, null: false
|
||||
DateTime :created_at, default: Sequel::CURRENT_TIMESTAMP
|
||||
DateTime :updated_at, default: Sequel::CURRENT_TIMESTAMP
|
||||
end
|
||||
alter_table(:results) do
|
||||
add_foreign_key :hardware_id, :hardware
|
||||
add_foreign_key :benchmark_id, :benchmarks
|
||||
end
|
||||
end
|
||||
|
||||
down do
|
||||
drop_table(:hardware)
|
||||
drop_table(:benchmarks)
|
||||
drop_table(:results)
|
||||
end
|
||||
|
||||
end
|
26
src/config.rb
Normal file
26
src/config.rb
Normal file
@ -0,0 +1,26 @@
|
||||
require 'yaml'
|
||||
|
||||
class Config
|
||||
|
||||
DEFAULT_CONFIG = 'config/defaults.yaml'
|
||||
|
||||
def initialize(config_path)
|
||||
@data = YAML::load_file(DEFAULT_CONFIG)
|
||||
|
||||
if File.exists?(config_path)
|
||||
@data.merge!(YAML::load_file(config_path))
|
||||
end
|
||||
end
|
||||
|
||||
def get(key, depth = 0)
|
||||
bits = key.split('.')
|
||||
value = @data
|
||||
|
||||
bits.each do |bit|
|
||||
value = value[bit]
|
||||
end
|
||||
|
||||
return value
|
||||
end
|
||||
|
||||
end
|
5
src/models/benchmark.rb
Normal file
5
src/models/benchmark.rb
Normal file
@ -0,0 +1,5 @@
|
||||
class Benchmark < Sequel::Model
|
||||
|
||||
one_to_many :results
|
||||
|
||||
end
|
5
src/models/hardware.rb
Normal file
5
src/models/hardware.rb
Normal file
@ -0,0 +1,5 @@
|
||||
class Hardware < Sequel::Model(:hardware)
|
||||
|
||||
one_to_many :results
|
||||
|
||||
end
|
3
src/models/init.rb
Normal file
3
src/models/init.rb
Normal file
@ -0,0 +1,3 @@
|
||||
require_relative 'hardware'
|
||||
require_relative 'benchmark'
|
||||
require_relative 'result'
|
6
src/models/result.rb
Normal file
6
src/models/result.rb
Normal file
@ -0,0 +1,6 @@
|
||||
class Result < Sequel::Model
|
||||
|
||||
many_to_one :hardware
|
||||
many_to_one :benchmarks
|
||||
|
||||
end
|
@ -1,8 +1,10 @@
|
||||
class GameData < Sinatra::Base
|
||||
get '/' do
|
||||
results = Result.reverse(:updated_at).limit(10).all()
|
||||
|
||||
erb :'index/index', locals: {
|
||||
title: 'Test!!!',
|
||||
results: []
|
||||
title: 'Dashboard',
|
||||
results: results
|
||||
}
|
||||
end
|
||||
|
||||
|
@ -1,4 +1,11 @@
|
||||
require 'sinatra/base'
|
||||
require 'sequel'
|
||||
require 'sqlite3'
|
||||
|
||||
# Load the Sequel timestamps plugin
|
||||
Sequel::Model.plugin(:timestamps)
|
||||
# Initialize Sequel gem for database actions
|
||||
DB = Sequel.connect(adapter: $conf.get('database.adapter'), database: $conf.get('database.database'))
|
||||
|
||||
# Base app
|
||||
class GameData < Sinatra::Base
|
||||
@ -14,3 +21,5 @@ end
|
||||
|
||||
# Load routes
|
||||
require_relative 'routes/init'
|
||||
# Load models
|
||||
require_relative 'models/init'
|
||||
|
@ -4,8 +4,8 @@
|
||||
<li><a href="/">Dashboard</a></li>
|
||||
<li><a href="/hardware">Hardware</a></li>
|
||||
<li><a href="/benchmarks">Benchmarks</a></li>
|
||||
<li><a href="/comparisons">Comparisons</a></li>
|
||||
<li><a href="/results">Results</a></li>
|
||||
<li><a href="/comparisons">Comparisons</a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
Loading…
Reference in New Issue
Block a user