-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a sample use of sinatra and tokyo cabinet
'gem install rufus-tokyo uuidtool' to run
- Loading branch information
Showing
10 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
sinatra+sequel/notes.db | ||
sinatra+rails/tmp | ||
sinatra+rails/log | ||
*.swp |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'rubygems' | ||
require 'sinatra' | ||
require 'rufus/edo' | ||
require 'uuidtools' | ||
|
||
helpers do | ||
def h(string) | ||
string.to_s.gsub('<','<'). | ||
gsub('>','>').gsub('"', '"') | ||
end | ||
end | ||
|
||
configure do | ||
DB = Rufus::Edo::Table.new('data.tct') | ||
end | ||
|
||
get '/notes' do | ||
@notes = DB.query{|q| q.add 'table', :eq, 'blogs'}.map{|b|b} | ||
haml :index | ||
end | ||
|
||
get '/notes/:id' do | ||
pass if params[:id] == 'new' | ||
@note = DB['blogs_' + params[:id]] | ||
haml(:show) + haml(:back_to_top) | ||
end | ||
|
||
get '/notes/new' do | ||
@note = {'title' => '', 'body' => ''} | ||
haml(:new) + haml(:back_to_top) | ||
end | ||
|
||
get '/notes/:id/edit' do | ||
@note = DB['blogs_' + params[:id]] | ||
haml(:edit) + haml(:back_to_top) | ||
end | ||
|
||
put '/notes/:id' do | ||
DB['blogs_'+params[:id]] = {'title' => params[:title], 'body' => params[:body], 'table' => 'blogs'} | ||
redirect "/notes/#{params[:id]}" | ||
end | ||
|
||
post '/notes' do | ||
uuid = UUID.random_create.to_s | ||
DB['blogs_'+uuid] = {'title' => params[:title], 'body' => params[:body], 'table' => 'blogs'} | ||
redirect "/notes" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
%div | ||
%a{ :href => "/notes"} Back to Top |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
%form{:action => "/notes/#{h(params[:id])}", :method => "post"} | ||
%input{ :type => "hidden", :name => "_method", :value => "put"} | ||
= haml :form, :locals => {:note => @note} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
%label{:for=>"title"} Description | ||
%br | ||
%input{:name=>"title", :value=>h(note['title']), :size => 50} | ||
%br | ||
%label{:for => "body"}Content | ||
%br | ||
%textarea{:name => "body", :cols => 30, :rows => 10}= note['body'] | ||
%br | ||
%input{:type => "submit"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
%div.body | ||
%table{:width => "100%", :style => "border-collapse: collapse", :border => 1} | ||
%thead | ||
%tr | ||
%td title | ||
%td | ||
%td | ||
%tbody | ||
- @notes.each do |note| | ||
%tr | ||
%td= h note['title'] | ||
%td | ||
%a{:href => "/notes/#{note[:pk].split('_')[1]}"} show | ||
%td | ||
%a{:href => "/notes/#{note[:pk].split('_')[1]}/edit"} edit | ||
%a{:href => '/notes/new'} new note |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
%html | ||
%head | ||
%title note in sinatra | ||
%body= yield |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
%form{:action => "/notes", :method => "post"} | ||
= haml :form, :locals => {:note => @note} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
%h3= h @note['title'] | ||
%div= @note['body'] |