Skip to content

Commit

Permalink
add a sample use of sinatra and tokyo cabinet
Browse files Browse the repository at this point in the history
'gem install rufus-tokyo uuidtool' to run
  • Loading branch information
nouse committed Apr 14, 2009
1 parent 98c50dd commit 353bc2c
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
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 added sinatra+tokyo/data.tct
Binary file not shown.
47 changes: 47 additions & 0 deletions sinatra+tokyo/notes.rb
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('<','&lt;').
gsub('>','&gt;').gsub('"', '&quot;')
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
2 changes: 2 additions & 0 deletions sinatra+tokyo/views/back_to_top.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
%div
%a{ :href => "/notes"} Back to Top
3 changes: 3 additions & 0 deletions sinatra+tokyo/views/edit.haml
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}
9 changes: 9 additions & 0 deletions sinatra+tokyo/views/form.haml
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"}
16 changes: 16 additions & 0 deletions sinatra+tokyo/views/index.haml
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 &nbsp;
%td &nbsp;
%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
4 changes: 4 additions & 0 deletions sinatra+tokyo/views/layout.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
%html
%head
%title note in sinatra
%body= yield
2 changes: 2 additions & 0 deletions sinatra+tokyo/views/new.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
%form{:action => "/notes", :method => "post"}
= haml :form, :locals => {:note => @note}
2 changes: 2 additions & 0 deletions sinatra+tokyo/views/show.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
%h3= h @note['title']
%div= @note['body']

0 comments on commit 353bc2c

Please sign in to comment.