fast prototyping with sinatra sequel w2tags

Post on 19-Jan-2015

3,671 views 0 download

Tags:

description

fast prototyping with sinatra sequel w2tags, when you need to prototype sometime it need framework that works simple complete and less typing, w2tags use HAML like syntax to speed up the prototyping

transcript

SINATRASINATRASEQUELSEQUELW2TAGSW2TAGS

➢WorkWork: : SKYEIGHTSKYEIGHT ( (S8S8))

➢AtAt--WorkWork: : monorailsmonorails//NETNET 2.02.0

➢OOututsideside--WorkWork: : rubyruby//javascriptjavascript

Sinatradon't doMagic

SinatraSinatra basicbasic ConceptConcept

ThrowThrow URLURL DODO Some StuffSome Stuff

require 'rubygems'require 'sinatra'

get '/' do 'Hello World' end

ThrowThrow URLURL DODO Some StuffSome Stuff

DODO Some StuffSome StuffThrowThrow

ControllerController

Return stringReturn stringURLURL

require 'rubygems'require 'sinatra'

get '/' do 'Hello World' end

require 'rubygems' require 'sinatra'

get '/' do @data = 'Hello World' erb :index

endindex.erbindex.erb

<%= @data %>

ViewView

Return stringReturn stringURLURL ControllerController

SimplySinatraSequel

require 'rubygems' require 'sequel' require 'sinatra' DB = Sequel.connect(....) class Entry < Sequel::Model end get '/' do

@recs = Entry.allerb :index

endindex.erbindex.erb

<h1>Entry List</h1><% @recs.each do |r| %> <%= r.title %> <br/><% end %>

require 'rubygems' require 'sequel' require 'sinatra' DB = Sequel.connect(....) class Entry < Sequel::Model end get '/' do

@recs = Entry.allerb :index

endindex.erbindex.erb

<h1>Entry List</h1><% @recs.each do |r| %> <%= r.title %> <br/><% end %>

MModelodel

require 'rubygems' require 'sequel' require 'sinatra' DB = Sequel.connect(....) class Entry < Sequel::Model end get '/' do

@recs = Entry.allerb :index

endindex.erbindex.erb

<h1>Entry List</h1><% @recs.each do |r| %> <%= r.title %> <br/><% end %>

MModelodel

VView iew

require 'rubygems' require 'sequel' require 'sinatra' DB = Sequel.connect(....) class Entry < Sequel::Model end get '/' do

@recs = Entry.allerb :index

endindex.erbindex.erb

<h1>Entry List</h1><% @recs.each do |r| %> <%= r.title %> <br/><% end %>

MModelodel

VView iew

CControllerontroller

require 'rubygems' require 'sequel' require 'sinatra' DB = Sequel.connect(....) class Entry < Sequel::Model end get '/' do

@recs = Entry.allerb :index

endindex.erbindex.erb

<h1>Entry List</h1><% @recs.each do |r| %> <%= r.title %> <br/><% end %>

MModelodel

VView iew

CControllerontrollerRRououtete

require 'rubygems' require 'sequel' require 'sinatra' DB = Sequel.connect(....) class Entry < Sequel::Model end get '/' do

@recs = Entry.allerb :index

endindex.erbindex.erb

<h1>Entry List</h1><% @recs.each do |r| %> <%= r.title %> <br/><% end %>

MModelodel

VView iew

CControllerontrollerRRououtete

MVC MVC - - RRSome StuffSome Stuff

SimplySinatraSequelW2Tags

require 'rubygems' require 'sequel' require 'sinatra' require 'w2tags' require 'w2tags/sinatra_hook'

DB = Sequel.connect(....) class Entry < Sequel::Model end get '/' do

@recs = Entry.allerb :index

end

index.w2erbindex.w2erb

%h1%h1 Entry List-each-each @recs;r == r.title %br/%br/

index.erbindex.erb

<h1>Entry List</h1><% @recs.each do |r| %> <%= r.title %> <br/><% end %>

Auto G

enerate

Auto G

enerate

index.w2erbindex.w2erb

%h1%h1 Entry List-each-each @recs;r == r.title %br/%br/

index.erbindex.erb

<h1>Entry List</h1><% @recs.each do |r| %> <%= r.title %> <br/><% end %>See the flow

clearly

W2Tags

E n t r yAction Method 1 Method 2

ListNewEdit

Delete

get '/entries'get '/entries/new' post '/entries/new'get '/entries/:id' put '/entries/:id'

delete '/entries/:id'

SinatraSinatracrudcrud

E n t r yAction Method 1 Method 2

ListNewEdit

Delete

get '/entries'get '/entries/new' post '/entries/new'get '/entries/:id' put '/entries/:id'

delete '/entries/:id'

get '/entries' doerb :entries_index

get '/entries/new' doerb :entries_new

post '/entries/new' doredirect '/entries'

get '/entries/:id' doerb :entries_edit

put '/entries/:id' doredirect '/entries'

delete '/entries/:id' doredirect '/entries'

SinatraSinatracrudcrud

11

22

33

Sinatra Sinatra crudcrud

DisplayDisplay ActionAction

SinatraSinatraSimplySimplyMagicMagic

W2TagsW2TagsSimplySimplyMagicMagic

11 22 33

Auto GenerateAuto Generate11

Auto GenerateAuto Generate11

Auto GenerateAuto Generate22

Auto GenerateAuto Generate33

Sinatra Sinatra :: http:/github.com/bmizerany/http:/github.com/bmizerany/sinatrasinatra SequelSequel :: http:/github.com/jeremyevans/http:/github.com/jeremyevans/sequelsequel W2TagsW2Tags :: http:/github.com/wharsojo/http:/github.com/wharsojo/w2tagsw2tags DemoDemo :: http:/github.com/wharsojo/http:/github.com/wharsojo/demo_sinatrademo_sinatra

ResourcesResources

Thank you!Thank you!slideshare.com/wharsojo...slideshare.com/wharsojo...

When error doWhen error doSinatra.applicationSinatra.application.error.erroror selector select .errors.errors

rackrack

Ec = New EventContexEc = New EventContex●Ec.request = New Rack::RequestEc.request = New Rack::Request●Ec.response = New Rack::ResponseEc.response = New Rack::Response

Lookup EventLookup Event ((post,delete,put,get,headpost,delete,put,get,head) or) or

do/raise error do/raise error not_foundnot_found

RenderRender● W2Tags/ErbW2Tags/Erb● Haml, SassHaml, Sass● BuilderBuilder

beforebefore Sinatra.application.filters

Sinatra.application.events

Array ofcode-blockall will beexecute

Array ofcode-blockfirst match

will beexecute

Code-block/render Execute inside instance object ofEventContext

wh/id-ruby

erb.hoterb.hot

sinatra_table.hotsinatra_table.hot

1122

33

44

11

22

33

44

55

55

66

66

77

77

88

88

99

1010

99

1010

1111

1111

1212

1212

11

sinatra_form2.hotsinatra_form2.hot

DBDB[[:tabel:tabel]] #Dataset chainable method, return:Dataset #Dataset chainable method, return:Datasetwherewhere, , filterfilter, , excludeexclude, , orderorder, , reverse_orderreverse_order, , uniquniq, , limitlimit,,paginatepaginate, , joinjoin, , inner_joininner_join, , left_outer_joinleft_outer_join,,

DBDB[[:tabel:tabel]...]...allall #records (Array of record) #records (Array of record)DBDB[[:tabel:tabel]...]...firstfirst #record (Hash of field_name / value) #record (Hash of field_name / value)DBDB[[:tabel:tabel]...]...order(order(:field:field))..lastlast #record #recordDBDB[[:tabel:tabel]...]...mapmap :field :field #Array of value#Array of value

DBDB[[:tabel:tabel]...[]...[Integer / HashInteger / Hash]]IntegerInteger=> ...=> ...limitlimit((IntegerInteger).).allallHashHash => ... => ...wherewhere((Hash Hash ).).firstfirst

DBDB[[:tabel:tabel]...[ ]...[ 22]] #eq #eq DBDB[[:tabel:tabel].].limitlimit( ( 22).).allallDBDB[[:tabel:tabel]...[]...[:id:id=>=>22]] #eq #eq DBDB[[:tabel:tabel].].wherewhere((:id:id=>=>22).).firstfirst

#Summarizing: result Number #Summarizing: result Number ......countcount, , maxmax, , minmin, , avgavg, , sumsum

#Manipulation #Manipulation DBDB[[:tabel:tabel]...]...deletedelete, ..., ...updateupdate, , insertinsert, , <<<<

Sequel – DB ToolkitSequel – DB Toolkit