mattpuchlerz / sinatra_presentation

The slides and code samples from my Sinatra presentation at the ChicagoRuby.org meetup on May 16, 2009.

This URL has Read+Write access

sinatra_presentation / app.rb
100644 250 lines (172 sloc) 3.901 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# Note the application's root for convenience
ROOT = File.expand_path File.dirname(__FILE__) unless defined?(ROOT)
 
require 'rubygems'
require 'sinatra'
require 'datamapper'
 
# Dave Giunta's Bowling::Game library
$LOAD_PATH << File.join( ROOT, *%w[ vendor bowling lib ] )
require 'bowling'
 
 
 
#
# Models
#
 
class Game
 
  include DataMapper::Resource
  
  property :id, Serial
  property :hits, Object
  
  def hits
    attribute_get(:hits).to_a
  end
  
  def hits=(hits)
    hits = hits.split(',') if hits.is_a? String
    hits.map! { |hit| hit.to_i }
    attribute_set :hits, hits
  end
  
  def score
    game = Bowling::Game.new
    hits.each { |hit| game.hit(hit) }
    game.score
  end
 
end
 
 
 
#
# Set up database
#
 
DataMapper.setup :default, "sqlite3://#{ ROOT }/db/#{ Sinatra::Application.environment }.sqlite3"
DataMapper.auto_upgrade!
 
 
 
#
# Routes
#
 
use_in_file_templates!
 
get '/' do
  redirect '/games'
end
 
get '/games' do
  @games = Game.all
  erb :index
end
 
get '/games/new' do
  @game = Game.new
  erb :new
end
 
post '/games' do
  @game = Game.new params
  
  if @game.save
    redirect "/games/#{ @game.id }"
  else
    erb :new
  end
end
 
get '/games/:id' do
  @game = Game.get params[:id]
  erb :show
end
 
get '/games/:id/edit' do
  @game = Game.get params[:id]
  erb :edit
end
 
put '/games/:id' do
  @game = Game.get params[:id]
  params.delete('_method')
  
  if @game.update_attributes(params)
    redirect "/games/#{ @game.id }"
  else
    erb :edit
  end
end
 
delete '/games/:id' do
  @game = Game.get params[:id]
  @game.destroy
  
  redirect "/games"
end
 
__END__
 
 
 
@@layout
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
<head>
<title>Bowling!</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
 
<body>
 
<%= yield %>
 
</body>
 
</html>
 
 
 
@@index
 
<h1>Games</h1>
 
<ul>
<li><a href="/games/new">New Game</a></li>
</ul>
 
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>Hits</th>
<th>Score</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% @games.each do |game| %>
<tr>
<td><%= game.id %></td>
<td><%= game.hits.join(',') %></td>
<td><%= game.score %></td>
<td><a href="/games/<%= game.id %>">Show</a></td>
<td><a href="/games/<%= game.id %>/edit">Edit</a></td>
<td>
<form action="/games/<%= game.id %>" method="post">
<div>
<input type="hidden" name="_method" value="delete" />
<input type="submit" value="Delete" />
</div>
</form>
</td>
</tr>
<% end %>
 
</tbody>
</table>
 
 
 
@@new
 
<h1>New Game</h1>
 
<ul>
<li><a href="/games">Back to Games</a></li>
</ul>
 
<form action="/games" method="post">
<dl>
<dt><label for="hits">Hits</label></dt>
<dd><input id="hits" type="text" name="hits" value="<%= @game.hits %>" size="35" /></dd>
</dl>
 
<p>
<input type="submit" value="Save" />
</p>
</form>
 
 
 
@@show
 
<h1>Game</h1>
 
<ul>
<li><a href="/games">Back to Games</a></li>
<li><a href="/games/<%= @game.id %>/edit">Edit Game</a></li>
</ul>
 
<dl>
<dt>Hits</dt>
<dd><%= @game.hits.join(',') %></dd>
</dl>
 
<dl>
<dt>Score</dt>
<dd><%= @game.score %></dd>
</dl>
 
 
 
@@edit
 
<h1>Edit Game</h1>
 
<ul>
<li><a href="/games">Back to Games</a></li>
<li><a href="/games/<%= @game.id %>">Show Game</a></li>
</ul>
 
<form action="/games/<%= @game.id %>" method="post">
<dl>
<dt><label for="hits">Hits</label></dt>
<dd><input id="hits" type="text" name="hits" value="<%= @game.hits.join(',') %>" size="35" /></dd>
</dl>
 
<p>
<input type="hidden" name="_method" value="put" />
<input type="submit" value="Save" />
</p>
</form>