From 6dd3e4c7d6e369b988d6eeaaed32dfdbb67c5389 Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Wed, 7 Dec 2022 17:52:27 -0500 Subject: [PATCH] Initial Sinatra project strucutre --- .gitignore | 64 ++++++++++++++++++++++++--- Gemfile | 7 +++ Gemfile.lock | 32 ++++++++++++++ LICENSE | 2 +- data/defaults.yaml | 3 ++ db/migrations/0001_add_items_table.rb | 22 +++++++++ lib/config.rb | 20 +++++++++ lib/models/item.rb | 5 +++ lib/routes.rb | 7 +++ raven.rb | 27 +++++++++++ views/index.erb | 11 +++++ views/layout.erb | 13 ++++++ 12 files changed, 206 insertions(+), 7 deletions(-) create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 data/defaults.yaml create mode 100644 db/migrations/0001_add_items_table.rb create mode 100644 lib/config.rb create mode 100644 lib/models/item.rb create mode 100644 lib/routes.rb create mode 100644 raven.rb create mode 100644 views/index.erb create mode 100644 views/layout.erb diff --git a/.gitignore b/.gitignore index f712bc5..58fffb3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,60 @@ -# ---> Composer -composer.phar -/vendor/ +# ---> Ruby +*.gem +*.rbc +/.config +/coverage/ +/InstalledFiles +/pkg/ +/spec/reports/ +/spec/examples.txt +/test/tmp/ +/test/version_tmp/ +/tmp/ -# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control -# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file -# composer.lock +# Used by dotenv library to load environment variables. +# .env +# Ignore Byebug command history file. +.byebug_history + +## Specific to RubyMotion: +.dat* +.repl_history +build/ +*.bridgesupport +build-iPhoneOS/ +build-iPhoneSimulator/ + +## Specific to RubyMotion (use of CocoaPods): +# +# We recommend against adding the Pods directory to your .gitignore. However +# you should judge for yourself, the pros and cons are mentioned at: +# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control +# +# vendor/Pods/ + +## Documentation cache and generated files: +/.yardoc/ +/_yardoc/ +/doc/ +/rdoc/ + +## Environment normalization: +/.bundle/ +/vendor/bundle +/lib/bundler/man/ + +# for a library or gem, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# Gemfile.lock +# .ruby-version +# .ruby-gemset + +# unless supporting rvm < 1.11.0 or doing something fancy, ignore this: +.rvmrc + +# Used by RuboCop. Remote config files pulled in from inherit_from directive. +# .rubocop-https?--* + +# Local database storage +data/raven.db diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..f30fc95 --- /dev/null +++ b/Gemfile @@ -0,0 +1,7 @@ +source 'https://rubygems.org' + +gem 'sinatra', '~> 3.0' +gem 'puma', '~> 6.0' + +gem 'sequel', '~> 5.63' +gem 'sqlite3', '~> 1.5' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..3f84920 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,32 @@ +GEM + remote: https://rubygems.org/ + specs: + mustermann (3.0.0) + ruby2_keywords (~> 0.0.1) + nio4r (2.5.8) + puma (6.0.0) + nio4r (~> 2.0) + rack (2.2.4) + rack-protection (3.0.4) + rack + ruby2_keywords (0.0.5) + sequel (5.63.0) + sinatra (3.0.4) + mustermann (~> 3.0) + rack (~> 2.2, >= 2.2.4) + rack-protection (= 3.0.4) + tilt (~> 2.0) + sqlite3 (1.5.4-x64-mingw-ucrt) + tilt (2.0.11) + +PLATFORMS + x64-mingw-ucrt + +DEPENDENCIES + puma (~> 6.0) + sequel (~> 5.63) + sinatra (~> 3.0) + sqlite3 (~> 1.5) + +BUNDLED WITH + 2.3.7 diff --git a/LICENSE b/LICENSE index 5f662b3..f8df596 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) +Copyright (c) 2022 Metaunix.net Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/data/defaults.yaml b/data/defaults.yaml new file mode 100644 index 0000000..6d7b685 --- /dev/null +++ b/data/defaults.yaml @@ -0,0 +1,3 @@ +database: + adapter: 'sqlite' + database: 'data/raven.db' diff --git a/db/migrations/0001_add_items_table.rb b/db/migrations/0001_add_items_table.rb new file mode 100644 index 0000000..86f67ad --- /dev/null +++ b/db/migrations/0001_add_items_table.rb @@ -0,0 +1,22 @@ +Sequel.migration do + + up do + create_table(:items) do + primary_key :id + String :name, null: false + String :manufacturer + String :serial_number + String :sku_number + String :type + String :purchased_from + DateTime :purchased_at + DateTime :created_at + DateTime :updated_at + end + end + + down do + drop_table(:items) + end + +end diff --git a/lib/config.rb b/lib/config.rb new file mode 100644 index 0000000..fe21f39 --- /dev/null +++ b/lib/config.rb @@ -0,0 +1,20 @@ +require 'yaml' + +class Config + + def initialize(config_path) + @data = YAML::load_file(config_path) + end + + def get(key, depth = 0) + bits = key.split('.') + value = @data + + bits.each do |bit| + value = value[bit] + end + + return value + end + +end diff --git a/lib/models/item.rb b/lib/models/item.rb new file mode 100644 index 0000000..ab0351d --- /dev/null +++ b/lib/models/item.rb @@ -0,0 +1,5 @@ +class Item < Sequel::Model + + + +end diff --git a/lib/routes.rb b/lib/routes.rb new file mode 100644 index 0000000..9aa4b1a --- /dev/null +++ b/lib/routes.rb @@ -0,0 +1,7 @@ +get '/' do + items = Item.all + erb :index, :locals => { + :title => 'Dashboard', + :items => items + } +end diff --git a/raven.rb b/raven.rb new file mode 100644 index 0000000..57b0f6f --- /dev/null +++ b/raven.rb @@ -0,0 +1,27 @@ +require 'logger' +require 'sequel' +require 'sqlite3' +require 'sinatra' + +require_relative 'lib/config.rb' + +set :public_folder, __dir__ + '/public' + +set :views, settings.root + '/views' + +# Load configuration file +conf = Config.new(File.join(__dir__, 'data/defaults.yaml')) + +# Initialize logging +logger = Logger.new(STDOUT) +logger.level = Logger::INFO + +# 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')) +# Load models +require_relative 'lib/models/item.rb' + +# Register route handlers +require_relative 'lib/routes.rb' diff --git a/views/index.erb b/views/index.erb new file mode 100644 index 0000000..cf581a6 --- /dev/null +++ b/views/index.erb @@ -0,0 +1,11 @@ +

This is a test.

+ +<% if items.length > 0 %> +
    + <% items.each do |item| %> +
  • <%= item.name %>
  • + <% end %> +
+<% else %> +

There are no items to display.

+<% end %> diff --git a/views/layout.erb b/views/layout.erb new file mode 100644 index 0000000..f4841fe --- /dev/null +++ b/views/layout.erb @@ -0,0 +1,13 @@ + + + + + + <%= title %> | Raven + + + + + <%= yield %> + +