From 9909d82ed9f89f1b72d5bead21e93bf2f2039c35 Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Thu, 5 Jun 2014 22:50:33 +0300 Subject: Switched to sqlite --- .gitignore | 1 + README.rst | 8 ++++++ app.rb | 68 +++++++++++++++++++++++++++++++++++++++++++-------- views/edit_object.erb | 7 ++++++ views/index.erb | 4 +++ views/view_object.erb | 3 +++ 6 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 .gitignore create mode 100644 README.rst create mode 100644 views/edit_object.erb create mode 100644 views/view_object.erb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..98e6ef6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.db diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..b26626f --- /dev/null +++ b/README.rst @@ -0,0 +1,8 @@ +Membrain is an app for remembering things. The idea is very simple - you enter data by defining both the name and value of the data that is being saved. + +## DB Schema + +``` +create table objects (id integer primary key autoincrement, title text, icon text, updated datetime); +create table properties (id integer primary key autoincrement, object_id integer, key text, value text); +``` diff --git a/app.rb b/app.rb index 0b9e4e2..55f00c8 100755 --- a/app.rb +++ b/app.rb @@ -1,27 +1,75 @@ #! /usr/bin/env ruby -w -require "redis" +require "sqlite3" require "sinatra" -redis = Redis.new -redis.set("mykey", "lalala") +class MBObject + + attr_reader :id, :title + + @@db = SQLite3::Database.new "membrain.db" + + def initialize + @id = 0 + @keys = [] + @values = [] + @title = "" + end + + def init_with_row(row) + @id = row[0] + @title = row[1] + @updated = row[2] + end + + def self.create(name) + @@db.execute("INSERT INTO objects (title, updated) VALUES (?, DATETIME())", name) + @@db.execute("SELECT last_insert_rowid()")[0][0] + end + + def self.all + all = [] + @@db.execute("SELECT id, title, updated FROM objects") do | row | + o = MBObject.new + all << o.init_with_row(row) + end + return all + end + + def self.get(id) + o = MBObject.new + @@db.execute("SELECT id, icon, updated FROM objects WHERE id = ?", id) do | row | + o = MBObject.new + o.init_with_row(row) + end + return o + end + + def get_url + "/#{@id}/edit" + end +end get '/' do - erb :index + @data = MBObject.all + erb :index end get '/add' do - erb :form + erb :form end post '/add' do - redis.set('blaa', params[:name]) + i = MBObject.create(params[:name]) + redirect "/#{i}" end -get '/:cat' do | cat | - # /machines +get '/:id' do + @data = MBObject.get(params[:id]) + erb :view_object end -get '/:cat/:instance' do | cat, inst | - # /machines/fileserver +get '/:id/edit' do + @o = MBObject.get(params[:id]) + erb :edit_object end diff --git a/views/edit_object.erb b/views/edit_object.erb new file mode 100644 index 0000000..aa44acc --- /dev/null +++ b/views/edit_object.erb @@ -0,0 +1,7 @@ +
+
+ + +
+ +
diff --git a/views/index.erb b/views/index.erb index 11ef5a6..551ef06 100644 --- a/views/index.erb +++ b/views/index.erb @@ -1 +1,5 @@ +<% for item in @data %> + <%= item.title %> +<% end %> + Create diff --git a/views/view_object.erb b/views/view_object.erb new file mode 100644 index 0000000..20a58d7 --- /dev/null +++ b/views/view_object.erb @@ -0,0 +1,3 @@ +<%= @data.title %> + +Edit -- cgit v1.2.3