aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFilipp Lepalaan <filipp@mac.com>2014-06-05 22:50:33 +0300
committerFilipp Lepalaan <filipp@mac.com>2014-06-05 22:50:33 +0300
commit9909d82ed9f89f1b72d5bead21e93bf2f2039c35 (patch)
tree069693c372c8002634e569ebcf3fe5da59326862
parent39bc2b230b5de771974e174b5b5bbb62b54e6015 (diff)
downloadmembrain-9909d82ed9f89f1b72d5bead21e93bf2f2039c35.tar.gz
membrain-9909d82ed9f89f1b72d5bead21e93bf2f2039c35.tar.bz2
membrain-9909d82ed9f89f1b72d5bead21e93bf2f2039c35.zip
Switched to sqlite
-rw-r--r--.gitignore1
-rw-r--r--README.rst8
-rwxr-xr-xapp.rb68
-rw-r--r--views/edit_object.erb7
-rw-r--r--views/index.erb4
-rw-r--r--views/view_object.erb3
6 files changed, 81 insertions, 10 deletions
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 @@
+<form action="" method="post">
+ <div class="form-group">
+ <label for="exampleInputEmail1">Name</label>
+ <input name="name" type="text" class="form-control" placeholder="Name" value="<%= @o.value %>">
+ </div>
+ <button type="submit" class="btn btn-default">Submit</button>
+</form>
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 %>
+
<a class="btn btn-default" href="add">Create</a>
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 %>
+
+<a class="btn btn-warning" href="<%= @data.get_url %>">Edit</a>