Capcode 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -10,6 +10,10 @@ Capcode is a web microframework
10
10
 
11
11
  == FEATURES/PROBLEMS:
12
12
 
13
+ === 0.4.0
14
+ * Major bug correction !
15
+ * Add views...
16
+
13
17
  === 0.3.0
14
18
  * Work with Rack 1.0.0
15
19
 
@@ -38,12 +42,12 @@ Capcode is a web microframework
38
42
 
39
43
  Capcode.run( )
40
44
 
41
- == Running Capcode Apps
45
+ === Running Capcode Apps
42
46
 
43
47
  * Run: <tt>ruby sample.rb</tt>
44
48
  * Visit http://localhost:3000/
45
49
 
46
- == Create model
50
+ === Create model
47
51
 
48
52
  require 'rubygems'
49
53
  require 'capcode'
@@ -60,6 +64,58 @@ Capcode is a web microframework
60
64
 
61
65
  See <tt>examples/blog-dm.rb</tt> and/or <tt>examples/blog-couchdb.rb</tt> for complete examples.
62
66
 
67
+ === Create View
68
+
69
+ # file: sample.rb
70
+ require 'rubygems'
71
+ require 'capcode'
72
+
73
+ module Capcode
74
+ class Hello < Route '/hello'
75
+ def get
76
+ @t = Time.now
77
+ render :time
78
+ end
79
+ end
80
+ end
81
+
82
+ module Capcode::Views
83
+ def time
84
+ "Hello world #{@t}"
85
+ end
86
+ end
87
+
88
+ Capcode.run( )
89
+
90
+ === Create Helper
91
+
92
+ # file: sample.rb
93
+ require 'rubygems'
94
+ require 'capcode'
95
+
96
+ module Capcode
97
+ class Hello < Route '/hello'
98
+ def get
99
+ @t = Time.now
100
+ render :time
101
+ end
102
+ end
103
+ end
104
+
105
+ module Capcode::Helpers
106
+ def bold( &b )
107
+ "<b>"+yield+"</b>"
108
+ end
109
+ end
110
+
111
+ module Capcode::Views
112
+ def time
113
+ "Hello world " + bold { @t }
114
+ end
115
+ end
116
+
117
+ Capcode.run( )
118
+
63
119
  == REQUIREMENTS:
64
120
 
65
121
  * rack
@@ -66,6 +66,7 @@
66
66
  "<a href="../files/lib/capcode_rb.html" value="File: capcode.rb">lib/capcode.rb</a>",
67
67
  <a href="http://greg.rubyfr.net">nil</a>].each do<br />
68
68
  <a href="../classes/Capcode.html" title="Module: Capcode">Capcode</a>.view_html<br />
69
+ <a href="../classes/Capcode/Views.html" title="Module: Capcode::Views">Capcode::Views</a>.view_html<br />
69
70
  <a href="../classes/Capcode/Helpers.html" title="Module: Capcode::Helpers">Capcode::Helpers</a>.view_html<br />
70
71
  <a href="../classes/Capcode/HTTPError.html" title="Class: Capcode::HTTPError">Capcode::HTTPError</a>.view_html<br />
71
72
  <a href="../classes/Capcode/RouteError.html" title="Class: Capcode::RouteError">Capcode::RouteError</a>.view_html<br />
@@ -92,6 +93,7 @@ end</strong>
92
93
  <ul>
93
94
  <li>Rack</li>
94
95
  <li><a href="Capcode/Helpers.html">Capcode::Helpers</a></li>
96
+ <li><a href="Capcode/Views.html">Capcode::Views</a></li>
95
97
  </ul>
96
98
 
97
99
  <h2 class="ruled">Methods</h2>
@@ -127,120 +129,116 @@ If the regexp in the route does not match, all arguments will be
127
129
  <p class="source-link">[ <a href="javascript:toggleSource('M000001_source')" id="l_M000001_source">show source</a> ]</p>
128
130
  <div id="M000001_source" class="dyn-source">
129
131
  <pre>
130
- <span class="ruby-comment cmt"># File lib/capcode.rb, line 130</span>
131
- 130: <span class="ruby-keyword kw">def</span> <span class="ruby-constant">Route</span> <span class="ruby-operator">*</span><span class="ruby-identifier">u</span>
132
- 131: <span class="ruby-constant">Class</span>.<span class="ruby-identifier">new</span> {
133
- 132: <span class="ruby-identifier">meta_def</span>(<span class="ruby-identifier">:__urls__</span>){
134
- 133: <span class="ruby-comment cmt"># &lt; Route '/hello/world/([^\/]*)/id(\d*)', '/hello/(.*)'</span>
135
- 134: <span class="ruby-comment cmt"># # =&gt; [ {'/hello/world' =&gt; '([^\/]*)/id(\d*)', '/hello' =&gt; '(.*)'}, 2, &lt;Capcode::Klass&gt; ]</span>
136
- 135: <span class="ruby-identifier">h</span> = {}
137
- 136: <span class="ruby-identifier">max</span> = <span class="ruby-value">0</span>
138
- 137: <span class="ruby-identifier">u</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">_u</span><span class="ruby-operator">|</span>
139
- 138: <span class="ruby-identifier">m</span> = <span class="ruby-regexp re">/\/([^\/]*\(.*)/</span>.<span class="ruby-identifier">match</span>( <span class="ruby-identifier">_u</span> )
140
- 139: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">m</span>.<span class="ruby-identifier">nil?</span>
141
- 140: <span class="ruby-identifier">raise</span> <span class="ruby-constant">Capcode</span><span class="ruby-operator">::</span><span class="ruby-constant">RouteError</span>, <span class="ruby-node">&quot;Route `#{_u}' already defined with regexp `#{h[_u]}' !&quot;</span>, <span class="ruby-identifier">caller</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">h</span>.<span class="ruby-identifier">keys</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">_u</span>)
142
- 141: <span class="ruby-identifier">h</span>[<span class="ruby-identifier">_u</span>] = <span class="ruby-value str">''</span>
143
- 142: <span class="ruby-keyword kw">else</span>
144
- 143: <span class="ruby-identifier">_pre</span> = <span class="ruby-identifier">m</span>.<span class="ruby-identifier">pre_match</span>
145
- 144: <span class="ruby-identifier">_pre</span> = <span class="ruby-value str">&quot;/&quot;</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">_pre</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
146
- 145: <span class="ruby-identifier">raise</span> <span class="ruby-constant">Capcode</span><span class="ruby-operator">::</span><span class="ruby-constant">RouteError</span>, <span class="ruby-node">&quot;Route `#{_pre}' already defined with regexp `#{h[_pre]}' !&quot;</span>, <span class="ruby-identifier">caller</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">h</span>.<span class="ruby-identifier">keys</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">_pre</span>)
147
- 146: <span class="ruby-identifier">h</span>[<span class="ruby-identifier">_pre</span>] = <span class="ruby-identifier">m</span>.<span class="ruby-identifier">captures</span>[<span class="ruby-value">0</span>]
148
- 147: <span class="ruby-identifier">max</span> = <span class="ruby-constant">Regexp</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">m</span>.<span class="ruby-identifier">captures</span>[<span class="ruby-value">0</span>]).<span class="ruby-identifier">number_of_captures</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">max</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">Regexp</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">m</span>.<span class="ruby-identifier">captures</span>[<span class="ruby-value">0</span>]).<span class="ruby-identifier">number_of_captures</span>
149
- 148: <span class="ruby-keyword kw">end</span>
150
- 149: <span class="ruby-keyword kw">end</span>
151
- 150: [<span class="ruby-identifier">h</span>, <span class="ruby-identifier">max</span>, <span class="ruby-keyword kw">self</span>]
152
- 151: }
153
- 152:
154
- 153: <span class="ruby-comment cmt"># Hash containing all the request parameters (GET or POST)</span>
155
- 154: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">params</span>
156
- 155: <span class="ruby-ivar">@request</span>.<span class="ruby-identifier">params</span>
157
- 156: <span class="ruby-keyword kw">end</span>
158
- 157:
159
- 158: <span class="ruby-comment cmt"># Hash containing all the environment variables</span>
160
- 159: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">env</span>
161
- 160: <span class="ruby-ivar">@env</span>
162
- 161: <span class="ruby-keyword kw">end</span>
163
- 162:
164
- 163: <span class="ruby-comment cmt"># Hash session</span>
165
- 164: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">session</span>
166
- 165: <span class="ruby-ivar">@env</span>[<span class="ruby-value str">'rack.session'</span>]
167
- 166: <span class="ruby-keyword kw">end</span>
168
- 167:
169
- 168: <span class="ruby-comment cmt"># Return the Rack::Request object</span>
170
- 169: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">request</span>
171
- 170: <span class="ruby-ivar">@request</span>
172
- 171: <span class="ruby-keyword kw">end</span>
173
- 172:
174
- 173: <span class="ruby-comment cmt"># Return the Rack::Response object</span>
175
- 174: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">response</span>
176
- 175: <span class="ruby-ivar">@response</span>
177
- 176: <span class="ruby-keyword kw">end</span>
178
- 177:
179
- 178: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">call</span>( <span class="ruby-identifier">e</span> ) <span class="ruby-comment cmt">#:nodoc:</span>
180
- 179: <span class="ruby-ivar">@env</span> = <span class="ruby-identifier">e</span>
181
- 180: <span class="ruby-ivar">@response</span> = <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Response</span>.<span class="ruby-identifier">new</span>
182
- 181: <span class="ruby-ivar">@request</span> = <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Request</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@env</span>)
183
- 182:
184
- 183: <span class="ruby-identifier">r</span> = <span class="ruby-keyword kw">case</span> <span class="ruby-ivar">@env</span>[<span class="ruby-value str">&quot;REQUEST_METHOD&quot;</span>]
185
- 184: <span class="ruby-keyword kw">when</span> <span class="ruby-value str">&quot;GET&quot;</span>
186
- 185: <span class="ruby-identifier">finalPath</span> = <span class="ruby-keyword kw">nil</span>
187
- 186: <span class="ruby-identifier">finalArgs</span> = <span class="ruby-keyword kw">nil</span>
188
- 187: <span class="ruby-identifier">finalNArgs</span> = <span class="ruby-keyword kw">nil</span>
189
- 188:
190
- 189: <span class="ruby-identifier">aPath</span> = <span class="ruby-ivar">@request</span>.<span class="ruby-identifier">path</span>.<span class="ruby-identifier">gsub</span>( <span class="ruby-regexp re">/^\//</span>, <span class="ruby-value str">&quot;&quot;</span> ).<span class="ruby-identifier">split</span>( <span class="ruby-value str">&quot;/&quot;</span> )
191
- 190: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">__urls__</span>[<span class="ruby-value">0</span>].<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">p</span>, <span class="ruby-identifier">r</span><span class="ruby-operator">|</span>
192
- 191: <span class="ruby-identifier">xPath</span> = <span class="ruby-identifier">p</span>.<span class="ruby-identifier">gsub</span>( <span class="ruby-regexp re">/^\//</span>, <span class="ruby-value str">&quot;&quot;</span> ).<span class="ruby-identifier">split</span>( <span class="ruby-value str">&quot;/&quot;</span> )
193
- 192: <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">xPath</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">aPath</span>).<span class="ruby-identifier">size</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
194
- 193: <span class="ruby-identifier">diffArgs</span> = <span class="ruby-identifier">aPath</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">xPath</span>
195
- 194: <span class="ruby-identifier">diffNArgs</span> = <span class="ruby-identifier">diffArgs</span>.<span class="ruby-identifier">size</span>
196
- 195: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">finalNArgs</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-keyword kw">or</span> <span class="ruby-identifier">finalNArgs</span> <span class="ruby-operator">&gt;</span> <span class="ruby-identifier">diffNArgs</span>
197
- 196: <span class="ruby-identifier">finalPath</span> = <span class="ruby-identifier">p</span>
198
- 197: <span class="ruby-identifier">finalNArgs</span> = <span class="ruby-identifier">diffNArgs</span>
199
- 198: <span class="ruby-identifier">finalArgs</span> = <span class="ruby-identifier">diffArgs</span>
200
- 199: <span class="ruby-keyword kw">end</span>
201
- 200: <span class="ruby-keyword kw">end</span>
202
- 201:
203
- 202: <span class="ruby-keyword kw">end</span>
204
- 203:
205
- 204: <span class="ruby-comment cmt"># TODO : correct above to use this :</span>
206
- 205: <span class="ruby-comment cmt">#puts &quot;finalPath = #{finalPath}&quot;</span>
207
- 206: <span class="ruby-comment cmt">#puts &quot;finalNArgs = #{finalNArgs}&quot;</span>
208
- 207: <span class="ruby-comment cmt">#print &quot;finalArgs = &quot;; p finalArgs</span>
209
- 208:
210
- 209: <span class="ruby-identifier">nargs</span> = <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">__urls__</span>[<span class="ruby-value">1</span>]
211
- 210: <span class="ruby-identifier">regexp</span> = <span class="ruby-constant">Regexp</span>.<span class="ruby-identifier">new</span>( <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">__urls__</span>[<span class="ruby-value">0</span>][<span class="ruby-identifier">finalPath</span>] )
212
- 211:
213
- 212: <span class="ruby-identifier">args</span> = <span class="ruby-identifier">regexp</span>.<span class="ruby-identifier">match</span>( <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Utils</span>.<span class="ruby-identifier">unescape</span>(<span class="ruby-ivar">@request</span>.<span class="ruby-identifier">path_info</span>).<span class="ruby-identifier">gsub</span>( <span class="ruby-regexp re">/^\//</span>, <span class="ruby-value str">&quot;&quot;</span> ) )
214
- 213: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">nil?</span>
215
- 214: <span class="ruby-identifier">raise</span> <span class="ruby-constant">Capcode</span><span class="ruby-operator">::</span><span class="ruby-constant">ParameterError</span>, <span class="ruby-node">&quot;Path info `#{@request.path_info}' does not match route regexp `#{regexp.source}'&quot;</span>
216
- 215: <span class="ruby-keyword kw">else</span>
217
- 216: <span class="ruby-identifier">args</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">captures</span>
218
- 217: <span class="ruby-keyword kw">end</span>
219
- 218:
220
- 219: <span class="ruby-keyword kw">while</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">nargs</span>
221
- 220: <span class="ruby-identifier">args</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-keyword kw">nil</span>
222
- 221: <span class="ruby-keyword kw">end</span>
223
- 222:
224
- 223: <span class="ruby-identifier">get</span>( <span class="ruby-operator">*</span><span class="ruby-identifier">args</span> )
225
- 224: <span class="ruby-keyword kw">when</span> <span class="ruby-value str">&quot;POST&quot;</span>
226
- 225: <span class="ruby-identifier">post</span>
227
- 226: <span class="ruby-keyword kw">end</span>
228
- 227: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">r</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:to_ary</span>)
229
- 228: <span class="ruby-ivar">@response</span>.<span class="ruby-identifier">status</span> = <span class="ruby-identifier">r</span>[<span class="ruby-value">0</span>]
230
- 229: <span class="ruby-identifier">r</span>[<span class="ruby-value">1</span>].<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">k</span>,<span class="ruby-identifier">v</span><span class="ruby-operator">|</span>
231
- 230: <span class="ruby-ivar">@response</span>[<span class="ruby-identifier">k</span>] = <span class="ruby-identifier">v</span>
232
- 231: <span class="ruby-keyword kw">end</span>
233
- 232: <span class="ruby-ivar">@response</span>.<span class="ruby-identifier">body</span> = <span class="ruby-identifier">r</span>[<span class="ruby-value">2</span>]
234
- 233: <span class="ruby-keyword kw">else</span>
235
- 234: <span class="ruby-ivar">@response</span>.<span class="ruby-identifier">write</span> <span class="ruby-identifier">r</span>
236
- 235: <span class="ruby-keyword kw">end</span>
237
- 236:
238
- 237: <span class="ruby-ivar">@response</span>.<span class="ruby-identifier">finish</span>
239
- 238: <span class="ruby-keyword kw">end</span>
240
- 239:
241
- 240: <span class="ruby-identifier">include</span> <span class="ruby-constant">Capcode</span><span class="ruby-operator">::</span><span class="ruby-constant">Helpers</span>
242
- 241: }
243
- 242: <span class="ruby-keyword kw">end</span>
132
+ <span class="ruby-comment cmt"># File lib/capcode.rb, line 137</span>
133
+ 137: <span class="ruby-keyword kw">def</span> <span class="ruby-constant">Route</span> <span class="ruby-operator">*</span><span class="ruby-identifier">u</span>
134
+ 138: <span class="ruby-constant">Class</span>.<span class="ruby-identifier">new</span> {
135
+ 139: <span class="ruby-identifier">meta_def</span>(<span class="ruby-identifier">:__urls__</span>){
136
+ 140: <span class="ruby-comment cmt"># &lt; Route '/hello/world/([^\/]*)/id(\d*)', '/hello/(.*)'</span>
137
+ 141: <span class="ruby-comment cmt"># # =&gt; [ {'/hello/world' =&gt; '([^\/]*)/id(\d*)', '/hello' =&gt; '(.*)'}, 2, &lt;Capcode::Klass&gt; ]</span>
138
+ 142: <span class="ruby-identifier">h</span> = {}
139
+ 143: <span class="ruby-identifier">max</span> = <span class="ruby-value">0</span>
140
+ 144: <span class="ruby-identifier">u</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">_u</span><span class="ruby-operator">|</span>
141
+ 145: <span class="ruby-identifier">m</span> = <span class="ruby-regexp re">/\/([^\/]*\(.*)/</span>.<span class="ruby-identifier">match</span>( <span class="ruby-identifier">_u</span> )
142
+ 146: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">m</span>.<span class="ruby-identifier">nil?</span>
143
+ 147: <span class="ruby-identifier">raise</span> <span class="ruby-constant">Capcode</span><span class="ruby-operator">::</span><span class="ruby-constant">RouteError</span>, <span class="ruby-node">&quot;Route `#{_u}' already defined with regexp `#{h[_u]}' !&quot;</span>, <span class="ruby-identifier">caller</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">h</span>.<span class="ruby-identifier">keys</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">_u</span>)
144
+ 148: <span class="ruby-identifier">h</span>[<span class="ruby-identifier">_u</span>] = <span class="ruby-value str">''</span>
145
+ 149: <span class="ruby-keyword kw">else</span>
146
+ 150: <span class="ruby-identifier">_pre</span> = <span class="ruby-identifier">m</span>.<span class="ruby-identifier">pre_match</span>
147
+ 151: <span class="ruby-identifier">_pre</span> = <span class="ruby-value str">&quot;/&quot;</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">_pre</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
148
+ 152: <span class="ruby-identifier">raise</span> <span class="ruby-constant">Capcode</span><span class="ruby-operator">::</span><span class="ruby-constant">RouteError</span>, <span class="ruby-node">&quot;Route `#{_pre}' already defined with regexp `#{h[_pre]}' !&quot;</span>, <span class="ruby-identifier">caller</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">h</span>.<span class="ruby-identifier">keys</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">_pre</span>)
149
+ 153: <span class="ruby-identifier">h</span>[<span class="ruby-identifier">_pre</span>] = <span class="ruby-identifier">m</span>.<span class="ruby-identifier">captures</span>[<span class="ruby-value">0</span>]
150
+ 154: <span class="ruby-identifier">max</span> = <span class="ruby-constant">Regexp</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">m</span>.<span class="ruby-identifier">captures</span>[<span class="ruby-value">0</span>]).<span class="ruby-identifier">number_of_captures</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">max</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">Regexp</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">m</span>.<span class="ruby-identifier">captures</span>[<span class="ruby-value">0</span>]).<span class="ruby-identifier">number_of_captures</span>
151
+ 155: <span class="ruby-keyword kw">end</span>
152
+ 156: <span class="ruby-keyword kw">end</span>
153
+ 157: [<span class="ruby-identifier">h</span>, <span class="ruby-identifier">max</span>, <span class="ruby-keyword kw">self</span>]
154
+ 158: }
155
+ 159:
156
+ 160: <span class="ruby-comment cmt"># Hash containing all the request parameters (GET or POST)</span>
157
+ 161: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">params</span>
158
+ 162: <span class="ruby-ivar">@request</span>.<span class="ruby-identifier">params</span>
159
+ 163: <span class="ruby-keyword kw">end</span>
160
+ 164:
161
+ 165: <span class="ruby-comment cmt"># Hash containing all the environment variables</span>
162
+ 166: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">env</span>
163
+ 167: <span class="ruby-ivar">@env</span>
164
+ 168: <span class="ruby-keyword kw">end</span>
165
+ 169:
166
+ 170: <span class="ruby-comment cmt"># Hash session</span>
167
+ 171: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">session</span>
168
+ 172: <span class="ruby-ivar">@env</span>[<span class="ruby-value str">'rack.session'</span>]
169
+ 173: <span class="ruby-keyword kw">end</span>
170
+ 174:
171
+ 175: <span class="ruby-comment cmt"># Return the Rack::Request object</span>
172
+ 176: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">request</span>
173
+ 177: <span class="ruby-ivar">@request</span>
174
+ 178: <span class="ruby-keyword kw">end</span>
175
+ 179:
176
+ 180: <span class="ruby-comment cmt"># Return the Rack::Response object</span>
177
+ 181: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">response</span>
178
+ 182: <span class="ruby-ivar">@response</span>
179
+ 183: <span class="ruby-keyword kw">end</span>
180
+ 184:
181
+ 185: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">call</span>( <span class="ruby-identifier">e</span> ) <span class="ruby-comment cmt">#:nodoc:</span>
182
+ 186: <span class="ruby-ivar">@env</span> = <span class="ruby-identifier">e</span>
183
+ 187: <span class="ruby-ivar">@response</span> = <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Response</span>.<span class="ruby-identifier">new</span>
184
+ 188: <span class="ruby-ivar">@request</span> = <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Request</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@env</span>)
185
+ 189:
186
+ 190: <span class="ruby-identifier">r</span> = <span class="ruby-keyword kw">case</span> <span class="ruby-ivar">@env</span>[<span class="ruby-value str">&quot;REQUEST_METHOD&quot;</span>]
187
+ 191: <span class="ruby-keyword kw">when</span> <span class="ruby-value str">&quot;GET&quot;</span>
188
+ 192: <span class="ruby-identifier">finalPath</span> = <span class="ruby-keyword kw">nil</span>
189
+ 193: <span class="ruby-identifier">finalArgs</span> = <span class="ruby-keyword kw">nil</span>
190
+ 194: <span class="ruby-identifier">finalNArgs</span> = <span class="ruby-keyword kw">nil</span>
191
+ 195:
192
+ 196: <span class="ruby-identifier">aPath</span> = <span class="ruby-ivar">@request</span>.<span class="ruby-identifier">path</span>.<span class="ruby-identifier">gsub</span>( <span class="ruby-regexp re">/^\//</span>, <span class="ruby-value str">&quot;&quot;</span> ).<span class="ruby-identifier">split</span>( <span class="ruby-value str">&quot;/&quot;</span> )
193
+ 197: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">__urls__</span>[<span class="ruby-value">0</span>].<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">p</span>, <span class="ruby-identifier">r</span><span class="ruby-operator">|</span>
194
+ 198: <span class="ruby-identifier">xPath</span> = <span class="ruby-identifier">p</span>.<span class="ruby-identifier">gsub</span>( <span class="ruby-regexp re">/^\//</span>, <span class="ruby-value str">&quot;&quot;</span> ).<span class="ruby-identifier">split</span>( <span class="ruby-value str">&quot;/&quot;</span> )
195
+ 199: <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">xPath</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">aPath</span>).<span class="ruby-identifier">size</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span>
196
+ 200: <span class="ruby-identifier">diffArgs</span> = <span class="ruby-identifier">aPath</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">xPath</span>
197
+ 201: <span class="ruby-identifier">diffNArgs</span> = <span class="ruby-identifier">diffArgs</span>.<span class="ruby-identifier">size</span>
198
+ 202: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">finalNArgs</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-keyword kw">or</span> <span class="ruby-identifier">finalNArgs</span> <span class="ruby-operator">&gt;</span> <span class="ruby-identifier">diffNArgs</span>
199
+ 203: <span class="ruby-identifier">finalPath</span> = <span class="ruby-identifier">p</span>
200
+ 204: <span class="ruby-identifier">finalNArgs</span> = <span class="ruby-identifier">diffNArgs</span>
201
+ 205: <span class="ruby-identifier">finalArgs</span> = <span class="ruby-identifier">diffArgs</span>
202
+ 206: <span class="ruby-keyword kw">end</span>
203
+ 207: <span class="ruby-keyword kw">end</span>
204
+ 208:
205
+ 209: <span class="ruby-keyword kw">end</span>
206
+ 210:
207
+ 211: <span class="ruby-identifier">nargs</span> = <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">__urls__</span>[<span class="ruby-value">1</span>]
208
+ 212: <span class="ruby-identifier">regexp</span> = <span class="ruby-constant">Regexp</span>.<span class="ruby-identifier">new</span>( <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">__urls__</span>[<span class="ruby-value">0</span>][<span class="ruby-identifier">finalPath</span>] )
209
+ 213:
210
+ 214: <span class="ruby-identifier">args</span> = <span class="ruby-identifier">regexp</span>.<span class="ruby-identifier">match</span>( <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Utils</span>.<span class="ruby-identifier">unescape</span>(<span class="ruby-ivar">@request</span>.<span class="ruby-identifier">path</span>).<span class="ruby-identifier">gsub</span>( <span class="ruby-constant">Regexp</span>.<span class="ruby-identifier">new</span>( <span class="ruby-node">&quot;^#{finalPath}&quot;</span> ), <span class="ruby-value str">&quot;&quot;</span> ).<span class="ruby-identifier">gsub</span>( <span class="ruby-regexp re">/^\//</span>, <span class="ruby-value str">&quot;&quot;</span> ) )
211
+ 215: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">nil?</span>
212
+ 216: <span class="ruby-identifier">raise</span> <span class="ruby-constant">Capcode</span><span class="ruby-operator">::</span><span class="ruby-constant">ParameterError</span>, <span class="ruby-node">&quot;Path info `#{@request.path_info}' does not match route regexp `#{regexp.source}'&quot;</span>
213
+ 217: <span class="ruby-keyword kw">else</span>
214
+ 218: <span class="ruby-identifier">args</span> = <span class="ruby-identifier">args</span>.<span class="ruby-identifier">captures</span>
215
+ 219: <span class="ruby-keyword kw">end</span>
216
+ 220:
217
+ 221: <span class="ruby-keyword kw">while</span> <span class="ruby-identifier">args</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">nargs</span>
218
+ 222: <span class="ruby-identifier">args</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-keyword kw">nil</span>
219
+ 223: <span class="ruby-keyword kw">end</span>
220
+ 224:
221
+ 225: <span class="ruby-identifier">get</span>( <span class="ruby-operator">*</span><span class="ruby-identifier">args</span> )
222
+ 226: <span class="ruby-keyword kw">when</span> <span class="ruby-value str">&quot;POST&quot;</span>
223
+ 227: <span class="ruby-identifier">post</span>
224
+ 228: <span class="ruby-keyword kw">end</span>
225
+ 229: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">r</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:to_ary</span>)
226
+ 230: <span class="ruby-ivar">@response</span>.<span class="ruby-identifier">status</span> = <span class="ruby-identifier">r</span>[<span class="ruby-value">0</span>]
227
+ 231: <span class="ruby-identifier">r</span>[<span class="ruby-value">1</span>].<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">k</span>,<span class="ruby-identifier">v</span><span class="ruby-operator">|</span>
228
+ 232: <span class="ruby-ivar">@response</span>[<span class="ruby-identifier">k</span>] = <span class="ruby-identifier">v</span>
229
+ 233: <span class="ruby-keyword kw">end</span>
230
+ 234: <span class="ruby-ivar">@response</span>.<span class="ruby-identifier">body</span> = <span class="ruby-identifier">r</span>[<span class="ruby-value">2</span>]
231
+ 235: <span class="ruby-keyword kw">else</span>
232
+ 236: <span class="ruby-ivar">@response</span>.<span class="ruby-identifier">write</span> <span class="ruby-identifier">r</span>
233
+ 237: <span class="ruby-keyword kw">end</span>
234
+ 238:
235
+ 239: <span class="ruby-ivar">@response</span>.<span class="ruby-identifier">finish</span>
236
+ 240: <span class="ruby-keyword kw">end</span>
237
+ 241:
238
+ 242: <span class="ruby-identifier">include</span> <span class="ruby-constant">Capcode</span><span class="ruby-operator">::</span><span class="ruby-constant">Helpers</span>
239
+ 243: <span class="ruby-identifier">include</span> <span class="ruby-constant">Capcode</span><span class="ruby-operator">::</span><span class="ruby-constant">Views</span>
240
+ 244: }
241
+ 245: <span class="ruby-keyword kw">end</span>
244
242
  </pre>
245
243
  </div>
246
244
  </div>
@@ -255,10 +253,10 @@ Hash containing all the environment variables
255
253
  <p class="source-link">[ <a href="javascript:toggleSource('M000003_source')" id="l_M000003_source">show source</a> ]</p>
256
254
  <div id="M000003_source" class="dyn-source">
257
255
  <pre>
258
- <span class="ruby-comment cmt"># File lib/capcode.rb, line 159</span>
259
- 159: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">env</span>
260
- 160: <span class="ruby-ivar">@env</span>
261
- 161: <span class="ruby-keyword kw">end</span>
256
+ <span class="ruby-comment cmt"># File lib/capcode.rb, line 166</span>
257
+ 166: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">env</span>
258
+ 167: <span class="ruby-ivar">@env</span>
259
+ 168: <span class="ruby-keyword kw">end</span>
262
260
  </pre>
263
261
  </div>
264
262
  </div>
@@ -279,10 +277,10 @@ Rack or What you want Helper
279
277
  <p class="source-link">[ <a href="javascript:toggleSource('M000007_source')" id="l_M000007_source">show source</a> ]</p>
280
278
  <div id="M000007_source" class="dyn-source">
281
279
  <pre>
282
- <span class="ruby-comment cmt"># File lib/capcode.rb, line 249</span>
283
- 249: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">map</span>( <span class="ruby-identifier">r</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">b</span> )
284
- 250: <span class="ruby-ivar">@@__ROUTES</span>[<span class="ruby-identifier">r</span>] = <span class="ruby-keyword kw">yield</span>
285
- 251: <span class="ruby-keyword kw">end</span>
280
+ <span class="ruby-comment cmt"># File lib/capcode.rb, line 252</span>
281
+ 252: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">map</span>( <span class="ruby-identifier">r</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">b</span> )
282
+ 253: <span class="ruby-ivar">@@__ROUTES</span>[<span class="ruby-identifier">r</span>] = <span class="ruby-keyword kw">yield</span>
283
+ 254: <span class="ruby-keyword kw">end</span>
286
284
  </pre>
287
285
  </div>
288
286
  </div>
@@ -298,10 +296,10 @@ parameters (GET or POST)
298
296
  <p class="source-link">[ <a href="javascript:toggleSource('M000002_source')" id="l_M000002_source">show source</a> ]</p>
299
297
  <div id="M000002_source" class="dyn-source">
300
298
  <pre>
301
- <span class="ruby-comment cmt"># File lib/capcode.rb, line 154</span>
302
- 154: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">params</span>
303
- 155: <span class="ruby-ivar">@request</span>.<span class="ruby-identifier">params</span>
304
- 156: <span class="ruby-keyword kw">end</span>
299
+ <span class="ruby-comment cmt"># File lib/capcode.rb, line 161</span>
300
+ 161: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">params</span>
301
+ 162: <span class="ruby-ivar">@request</span>.<span class="ruby-identifier">params</span>
302
+ 163: <span class="ruby-keyword kw">end</span>
305
303
  </pre>
306
304
  </div>
307
305
  </div>
@@ -316,10 +314,10 @@ Return the Rack::Request object
316
314
  <p class="source-link">[ <a href="javascript:toggleSource('M000005_source')" id="l_M000005_source">show source</a> ]</p>
317
315
  <div id="M000005_source" class="dyn-source">
318
316
  <pre>
319
- <span class="ruby-comment cmt"># File lib/capcode.rb, line 169</span>
320
- 169: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">request</span>
321
- 170: <span class="ruby-ivar">@request</span>
322
- 171: <span class="ruby-keyword kw">end</span>
317
+ <span class="ruby-comment cmt"># File lib/capcode.rb, line 176</span>
318
+ 176: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">request</span>
319
+ 177: <span class="ruby-ivar">@request</span>
320
+ 178: <span class="ruby-keyword kw">end</span>
323
321
  </pre>
324
322
  </div>
325
323
  </div>
@@ -334,10 +332,10 @@ Return the Rack::Response object
334
332
  <p class="source-link">[ <a href="javascript:toggleSource('M000006_source')" id="l_M000006_source">show source</a> ]</p>
335
333
  <div id="M000006_source" class="dyn-source">
336
334
  <pre>
337
- <span class="ruby-comment cmt"># File lib/capcode.rb, line 174</span>
338
- 174: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">response</span>
339
- 175: <span class="ruby-ivar">@response</span>
340
- 176: <span class="ruby-keyword kw">end</span>
335
+ <span class="ruby-comment cmt"># File lib/capcode.rb, line 181</span>
336
+ 181: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">response</span>
337
+ 182: <span class="ruby-ivar">@response</span>
338
+ 183: <span class="ruby-keyword kw">end</span>
341
339
  </pre>
342
340
  </div>
343
341
  </div>
@@ -372,6 +370,9 @@ See Rack::Session for more informations
372
370
  </li>
373
371
  <li><tt>:daemonize</tt> = Daemonize application (default: false)
374
372
 
373
+ </li>
374
+ <li><tt>:db_config</tt> = database configuration file (default: database.yml)
375
+
375
376
  </li>
376
377
  </ul>
377
378
 
@@ -379,91 +380,91 @@ See Rack::Session for more informations
379
380
  <p class="source-link">[ <a href="javascript:toggleSource('M000008_source')" id="l_M000008_source">show source</a> ]</p>
380
381
  <div id="M000008_source" class="dyn-source">
381
382
  <pre>
382
- <span class="ruby-comment cmt"># File lib/capcode.rb, line 263</span>
383
- 263: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">run</span>( <span class="ruby-identifier">args</span> = {} )
384
- 264: <span class="ruby-identifier">conf</span> = {
385
- 265: <span class="ruby-identifier">:port</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">args</span>[<span class="ruby-identifier">:port</span>]<span class="ruby-operator">||</span><span class="ruby-value">3000</span>,
386
- 266: <span class="ruby-identifier">:host</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">args</span>[<span class="ruby-identifier">:host</span>]<span class="ruby-operator">||</span><span class="ruby-value str">&quot;localhost&quot;</span>,
387
- 267: <span class="ruby-identifier">:server</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">args</span>[<span class="ruby-identifier">:server</span>]<span class="ruby-operator">||</span><span class="ruby-keyword kw">nil</span>,
388
- 268: <span class="ruby-identifier">:log</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">args</span>[<span class="ruby-identifier">:log</span>]<span class="ruby-operator">||</span><span class="ruby-identifier">$stdout</span>,
389
- 269: <span class="ruby-identifier">:session</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">args</span>[<span class="ruby-identifier">:session</span>]<span class="ruby-operator">||</span>{},
390
- 270: <span class="ruby-identifier">:pid</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">args</span>[<span class="ruby-identifier">:pid</span>]<span class="ruby-operator">||</span><span class="ruby-node">&quot;#{$0}.pid&quot;</span>,
391
- 271: <span class="ruby-identifier">:daemonize</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">args</span>[<span class="ruby-identifier">:daemonize</span>]<span class="ruby-operator">||</span><span class="ruby-keyword kw">false</span>,
392
- 272: <span class="ruby-identifier">:db_config</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">args</span>[<span class="ruby-identifier">:db_config</span>]<span class="ruby-operator">||</span><span class="ruby-value str">&quot;database.yml&quot;</span>
393
- 273: }
394
- 274:
395
- 275: <span class="ruby-comment cmt"># Check that mongrel exists </span>
396
- 276: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:server</span>].<span class="ruby-identifier">nil?</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:server</span>] <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;mongrel&quot;</span>
397
- 277: <span class="ruby-keyword kw">begin</span>
398
- 278: <span class="ruby-identifier">require</span> <span class="ruby-value str">'mongrel'</span>
399
- 279: <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:server</span>] = <span class="ruby-value str">&quot;mongrel&quot;</span>
400
- 280: <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">LoadError</span>
401
- 281: <span class="ruby-identifier">puts</span> <span class="ruby-value str">&quot;!! could not load mongrel. Falling back to webrick.&quot;</span>
402
- 282: <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:server</span>] = <span class="ruby-value str">&quot;webrick&quot;</span>
403
- 283: <span class="ruby-keyword kw">end</span>
404
- 284: <span class="ruby-keyword kw">end</span>
405
- 285:
406
- 286: <span class="ruby-constant">Capcode</span>.<span class="ruby-identifier">constants</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">k</span><span class="ruby-operator">|</span>
407
- 287: <span class="ruby-keyword kw">begin</span>
408
- 288: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">eval</span> <span class="ruby-node">&quot;Capcode::#{k}.public_methods(true).include?( '__urls__' )&quot;</span>
409
- 289: <span class="ruby-identifier">u</span>, <span class="ruby-identifier">m</span>, <span class="ruby-identifier">c</span> = <span class="ruby-identifier">eval</span> <span class="ruby-node">&quot;Capcode::#{k}.__urls__&quot;</span>
410
- 290: <span class="ruby-identifier">u</span>.<span class="ruby-identifier">keys</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">_u</span><span class="ruby-operator">|</span>
411
- 291: <span class="ruby-identifier">raise</span> <span class="ruby-constant">Capcode</span><span class="ruby-operator">::</span><span class="ruby-constant">RouteError</span>, <span class="ruby-node">&quot;Route `#{_u}' already define !&quot;</span>, <span class="ruby-identifier">caller</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@@__ROUTES</span>.<span class="ruby-identifier">keys</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">_u</span>)
412
- 292: <span class="ruby-ivar">@@__ROUTES</span>[<span class="ruby-identifier">_u</span>] = <span class="ruby-identifier">c</span>.<span class="ruby-identifier">new</span>
413
- 293: <span class="ruby-keyword kw">end</span>
414
- 294: <span class="ruby-keyword kw">end</span>
415
- 295: <span class="ruby-keyword kw">rescue</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span>
416
- 296: <span class="ruby-identifier">raise</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">message</span>
417
- 297: <span class="ruby-keyword kw">end</span>
418
- 298: <span class="ruby-keyword kw">end</span>
419
- 299:
420
- 300: <span class="ruby-identifier">app</span> = <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">URLMap</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@@__ROUTES</span>)
421
- 301: <span class="ruby-identifier">app</span> = <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Session</span><span class="ruby-operator">::</span><span class="ruby-constant">Cookie</span>.<span class="ruby-identifier">new</span>( <span class="ruby-identifier">app</span>, <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:session</span>] )
422
- 302: <span class="ruby-identifier">app</span> = <span class="ruby-constant">Capcode</span><span class="ruby-operator">::</span><span class="ruby-constant">HTTPError</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">app</span>)
423
- 303: <span class="ruby-identifier">app</span> = <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">ContentLength</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">app</span>)
424
- 304: <span class="ruby-identifier">app</span> = <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Lint</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">app</span>)
425
- 305: <span class="ruby-identifier">app</span> = <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">ShowExceptions</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">app</span>)
426
- 306: <span class="ruby-comment cmt"># app = Rack::Reloader.new(app) ## -- NE RELOAD QUE capcode.rb -- So !!!</span>
427
- 307: <span class="ruby-identifier">app</span> = <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">CommonLogger</span>.<span class="ruby-identifier">new</span>( <span class="ruby-identifier">app</span>, <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:log</span>]) )
428
- 308:
429
- 309: <span class="ruby-comment cmt"># From rackup !!!</span>
430
- 310: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:daemonize</span>]
431
- 311: <span class="ruby-keyword kw">if</span> <span class="ruby-constant">RUBY_VERSION</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value str">&quot;1.9&quot;</span>
432
- 312: <span class="ruby-identifier">exit</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">fork</span>
433
- 313: <span class="ruby-constant">Process</span>.<span class="ruby-identifier">setsid</span>
434
- 314: <span class="ruby-identifier">exit</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">fork</span>
435
- 315: <span class="ruby-comment cmt"># Dir.chdir &quot;/&quot;</span>
436
- 316: <span class="ruby-constant">File</span>.<span class="ruby-identifier">umask</span> <span class="ruby-value">0000</span>
437
- 317: <span class="ruby-constant">STDIN</span>.<span class="ruby-identifier">reopen</span> <span class="ruby-value str">&quot;/dev/null&quot;</span>
438
- 318: <span class="ruby-constant">STDOUT</span>.<span class="ruby-identifier">reopen</span> <span class="ruby-value str">&quot;/dev/null&quot;</span>, <span class="ruby-value str">&quot;a&quot;</span>
439
- 319: <span class="ruby-constant">STDERR</span>.<span class="ruby-identifier">reopen</span> <span class="ruby-value str">&quot;/dev/null&quot;</span>, <span class="ruby-value str">&quot;a&quot;</span>
440
- 320: <span class="ruby-keyword kw">else</span>
441
- 321: <span class="ruby-constant">Process</span>.<span class="ruby-identifier">daemon</span>
442
- 322: <span class="ruby-keyword kw">end</span>
443
- 323:
444
- 324: <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:pid</span>], <span class="ruby-value str">'w'</span>){ <span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span> <span class="ruby-identifier">f</span>.<span class="ruby-identifier">write</span>(<span class="ruby-node">&quot;#{Process.pid}&quot;</span>) }
445
- 325: <span class="ruby-identifier">at_exit</span> { <span class="ruby-constant">File</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:pid</span>]) <span class="ruby-keyword kw">if</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">exist?</span>(<span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:pid</span>]) }
446
- 326: <span class="ruby-keyword kw">end</span>
447
- 327:
448
- 328: <span class="ruby-comment cmt"># Start database</span>
449
- 329: <span class="ruby-keyword kw">if</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">methods</span>.<span class="ruby-identifier">include?</span> <span class="ruby-value str">&quot;db_connect&quot;</span>
450
- 330: <span class="ruby-identifier">db_connect</span>( <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:db_config</span>], <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:log</span>] )
451
- 331: <span class="ruby-keyword kw">end</span>
452
- 332:
453
- 333: <span class="ruby-comment cmt"># Start server</span>
454
- 334: <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:server</span>]
455
- 335: <span class="ruby-keyword kw">when</span> <span class="ruby-value str">&quot;mongrel&quot;</span>
456
- 336: <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;** Starting Mongrel on #{conf[:host]}:#{conf[:port]}&quot;</span>
457
- 337: <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Handler</span><span class="ruby-operator">::</span><span class="ruby-constant">Mongrel</span>.<span class="ruby-identifier">run</span>( <span class="ruby-identifier">app</span>, {<span class="ruby-identifier">:Port</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:port</span>], <span class="ruby-identifier">:Host</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:host</span>]} ) { <span class="ruby-operator">|</span><span class="ruby-identifier">server</span><span class="ruby-operator">|</span>
458
- 338: <span class="ruby-identifier">trap</span> <span class="ruby-value str">&quot;SIGINT&quot;</span>, <span class="ruby-identifier">proc</span> { <span class="ruby-identifier">server</span>.<span class="ruby-identifier">stop</span> }
459
- 339: }
460
- 340: <span class="ruby-keyword kw">when</span> <span class="ruby-value str">&quot;webrick&quot;</span>
461
- 341: <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;** Starting WEBrick on #{conf[:host]}:#{conf[:port]}&quot;</span>
462
- 342: <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Handler</span><span class="ruby-operator">::</span><span class="ruby-constant">WEBrick</span>.<span class="ruby-identifier">run</span>( <span class="ruby-identifier">app</span>, {<span class="ruby-identifier">:Port</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:port</span>], <span class="ruby-identifier">:BindAddress</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:host</span>]} ) { <span class="ruby-operator">|</span><span class="ruby-identifier">server</span><span class="ruby-operator">|</span>
463
- 343: <span class="ruby-identifier">trap</span> <span class="ruby-value str">&quot;SIGINT&quot;</span>, <span class="ruby-identifier">proc</span> { <span class="ruby-identifier">server</span>.<span class="ruby-identifier">shutdown</span> }
464
- 344: }
465
- 345: <span class="ruby-keyword kw">end</span>
466
- 346: <span class="ruby-keyword kw">end</span>
383
+ <span class="ruby-comment cmt"># File lib/capcode.rb, line 267</span>
384
+ 267: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">run</span>( <span class="ruby-identifier">args</span> = {} )
385
+ 268: <span class="ruby-identifier">conf</span> = {
386
+ 269: <span class="ruby-identifier">:port</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">args</span>[<span class="ruby-identifier">:port</span>]<span class="ruby-operator">||</span><span class="ruby-value">3000</span>,
387
+ 270: <span class="ruby-identifier">:host</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">args</span>[<span class="ruby-identifier">:host</span>]<span class="ruby-operator">||</span><span class="ruby-value str">&quot;localhost&quot;</span>,
388
+ 271: <span class="ruby-identifier">:server</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">args</span>[<span class="ruby-identifier">:server</span>]<span class="ruby-operator">||</span><span class="ruby-keyword kw">nil</span>,
389
+ 272: <span class="ruby-identifier">:log</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">args</span>[<span class="ruby-identifier">:log</span>]<span class="ruby-operator">||</span><span class="ruby-identifier">$stdout</span>,
390
+ 273: <span class="ruby-identifier">:session</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">args</span>[<span class="ruby-identifier">:session</span>]<span class="ruby-operator">||</span>{},
391
+ 274: <span class="ruby-identifier">:pid</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">args</span>[<span class="ruby-identifier">:pid</span>]<span class="ruby-operator">||</span><span class="ruby-node">&quot;#{$0}.pid&quot;</span>,
392
+ 275: <span class="ruby-identifier">:daemonize</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">args</span>[<span class="ruby-identifier">:daemonize</span>]<span class="ruby-operator">||</span><span class="ruby-keyword kw">false</span>,
393
+ 276: <span class="ruby-identifier">:db_config</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">args</span>[<span class="ruby-identifier">:db_config</span>]<span class="ruby-operator">||</span><span class="ruby-value str">&quot;database.yml&quot;</span>
394
+ 277: }
395
+ 278:
396
+ 279: <span class="ruby-comment cmt"># Check that mongrel exists </span>
397
+ 280: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:server</span>].<span class="ruby-identifier">nil?</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:server</span>] <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;mongrel&quot;</span>
398
+ 281: <span class="ruby-keyword kw">begin</span>
399
+ 282: <span class="ruby-identifier">require</span> <span class="ruby-value str">'mongrel'</span>
400
+ 283: <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:server</span>] = <span class="ruby-value str">&quot;mongrel&quot;</span>
401
+ 284: <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">LoadError</span>
402
+ 285: <span class="ruby-identifier">puts</span> <span class="ruby-value str">&quot;!! could not load mongrel. Falling back to webrick.&quot;</span>
403
+ 286: <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:server</span>] = <span class="ruby-value str">&quot;webrick&quot;</span>
404
+ 287: <span class="ruby-keyword kw">end</span>
405
+ 288: <span class="ruby-keyword kw">end</span>
406
+ 289:
407
+ 290: <span class="ruby-constant">Capcode</span>.<span class="ruby-identifier">constants</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">k</span><span class="ruby-operator">|</span>
408
+ 291: <span class="ruby-keyword kw">begin</span>
409
+ 292: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">eval</span> <span class="ruby-node">&quot;Capcode::#{k}.public_methods(true).include?( '__urls__' )&quot;</span>
410
+ 293: <span class="ruby-identifier">u</span>, <span class="ruby-identifier">m</span>, <span class="ruby-identifier">c</span> = <span class="ruby-identifier">eval</span> <span class="ruby-node">&quot;Capcode::#{k}.__urls__&quot;</span>
411
+ 294: <span class="ruby-identifier">u</span>.<span class="ruby-identifier">keys</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">_u</span><span class="ruby-operator">|</span>
412
+ 295: <span class="ruby-identifier">raise</span> <span class="ruby-constant">Capcode</span><span class="ruby-operator">::</span><span class="ruby-constant">RouteError</span>, <span class="ruby-node">&quot;Route `#{_u}' already define !&quot;</span>, <span class="ruby-identifier">caller</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@@__ROUTES</span>.<span class="ruby-identifier">keys</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">_u</span>)
413
+ 296: <span class="ruby-ivar">@@__ROUTES</span>[<span class="ruby-identifier">_u</span>] = <span class="ruby-identifier">c</span>.<span class="ruby-identifier">new</span>
414
+ 297: <span class="ruby-keyword kw">end</span>
415
+ 298: <span class="ruby-keyword kw">end</span>
416
+ 299: <span class="ruby-keyword kw">rescue</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span>
417
+ 300: <span class="ruby-identifier">raise</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">message</span>
418
+ 301: <span class="ruby-keyword kw">end</span>
419
+ 302: <span class="ruby-keyword kw">end</span>
420
+ 303:
421
+ 304: <span class="ruby-identifier">app</span> = <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">URLMap</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@@__ROUTES</span>)
422
+ 305: <span class="ruby-identifier">app</span> = <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Session</span><span class="ruby-operator">::</span><span class="ruby-constant">Cookie</span>.<span class="ruby-identifier">new</span>( <span class="ruby-identifier">app</span>, <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:session</span>] )
423
+ 306: <span class="ruby-identifier">app</span> = <span class="ruby-constant">Capcode</span><span class="ruby-operator">::</span><span class="ruby-constant">HTTPError</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">app</span>)
424
+ 307: <span class="ruby-identifier">app</span> = <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">ContentLength</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">app</span>)
425
+ 308: <span class="ruby-identifier">app</span> = <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Lint</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">app</span>)
426
+ 309: <span class="ruby-identifier">app</span> = <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">ShowExceptions</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">app</span>)
427
+ 310: <span class="ruby-comment cmt"># app = Rack::Reloader.new(app) ## -- NE RELOAD QUE capcode.rb -- So !!!</span>
428
+ 311: <span class="ruby-identifier">app</span> = <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">CommonLogger</span>.<span class="ruby-identifier">new</span>( <span class="ruby-identifier">app</span>, <span class="ruby-constant">Logger</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:log</span>]) )
429
+ 312:
430
+ 313: <span class="ruby-comment cmt"># From rackup !!!</span>
431
+ 314: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:daemonize</span>]
432
+ 315: <span class="ruby-keyword kw">if</span> <span class="ruby-constant">RUBY_VERSION</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value str">&quot;1.9&quot;</span>
433
+ 316: <span class="ruby-identifier">exit</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">fork</span>
434
+ 317: <span class="ruby-constant">Process</span>.<span class="ruby-identifier">setsid</span>
435
+ 318: <span class="ruby-identifier">exit</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">fork</span>
436
+ 319: <span class="ruby-comment cmt"># Dir.chdir &quot;/&quot;</span>
437
+ 320: <span class="ruby-constant">File</span>.<span class="ruby-identifier">umask</span> <span class="ruby-value">0000</span>
438
+ 321: <span class="ruby-constant">STDIN</span>.<span class="ruby-identifier">reopen</span> <span class="ruby-value str">&quot;/dev/null&quot;</span>
439
+ 322: <span class="ruby-constant">STDOUT</span>.<span class="ruby-identifier">reopen</span> <span class="ruby-value str">&quot;/dev/null&quot;</span>, <span class="ruby-value str">&quot;a&quot;</span>
440
+ 323: <span class="ruby-constant">STDERR</span>.<span class="ruby-identifier">reopen</span> <span class="ruby-value str">&quot;/dev/null&quot;</span>, <span class="ruby-value str">&quot;a&quot;</span>
441
+ 324: <span class="ruby-keyword kw">else</span>
442
+ 325: <span class="ruby-constant">Process</span>.<span class="ruby-identifier">daemon</span>
443
+ 326: <span class="ruby-keyword kw">end</span>
444
+ 327:
445
+ 328: <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:pid</span>], <span class="ruby-value str">'w'</span>){ <span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span> <span class="ruby-identifier">f</span>.<span class="ruby-identifier">write</span>(<span class="ruby-node">&quot;#{Process.pid}&quot;</span>) }
446
+ 329: <span class="ruby-identifier">at_exit</span> { <span class="ruby-constant">File</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:pid</span>]) <span class="ruby-keyword kw">if</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">exist?</span>(<span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:pid</span>]) }
447
+ 330: <span class="ruby-keyword kw">end</span>
448
+ 331:
449
+ 332: <span class="ruby-comment cmt"># Start database</span>
450
+ 333: <span class="ruby-keyword kw">if</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">methods</span>.<span class="ruby-identifier">include?</span> <span class="ruby-value str">&quot;db_connect&quot;</span>
451
+ 334: <span class="ruby-identifier">db_connect</span>( <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:db_config</span>], <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:log</span>] )
452
+ 335: <span class="ruby-keyword kw">end</span>
453
+ 336:
454
+ 337: <span class="ruby-comment cmt"># Start server</span>
455
+ 338: <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:server</span>]
456
+ 339: <span class="ruby-keyword kw">when</span> <span class="ruby-value str">&quot;mongrel&quot;</span>
457
+ 340: <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;** Starting Mongrel on #{conf[:host]}:#{conf[:port]}&quot;</span>
458
+ 341: <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Handler</span><span class="ruby-operator">::</span><span class="ruby-constant">Mongrel</span>.<span class="ruby-identifier">run</span>( <span class="ruby-identifier">app</span>, {<span class="ruby-identifier">:Port</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:port</span>], <span class="ruby-identifier">:Host</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:host</span>]} ) { <span class="ruby-operator">|</span><span class="ruby-identifier">server</span><span class="ruby-operator">|</span>
459
+ 342: <span class="ruby-identifier">trap</span> <span class="ruby-value str">&quot;SIGINT&quot;</span>, <span class="ruby-identifier">proc</span> { <span class="ruby-identifier">server</span>.<span class="ruby-identifier">stop</span> }
460
+ 343: }
461
+ 344: <span class="ruby-keyword kw">when</span> <span class="ruby-value str">&quot;webrick&quot;</span>
462
+ 345: <span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;** Starting WEBrick on #{conf[:host]}:#{conf[:port]}&quot;</span>
463
+ 346: <span class="ruby-constant">Rack</span><span class="ruby-operator">::</span><span class="ruby-constant">Handler</span><span class="ruby-operator">::</span><span class="ruby-constant">WEBrick</span>.<span class="ruby-identifier">run</span>( <span class="ruby-identifier">app</span>, {<span class="ruby-identifier">:Port</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:port</span>], <span class="ruby-identifier">:BindAddress</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">conf</span>[<span class="ruby-identifier">:host</span>]} ) { <span class="ruby-operator">|</span><span class="ruby-identifier">server</span><span class="ruby-operator">|</span>
464
+ 347: <span class="ruby-identifier">trap</span> <span class="ruby-value str">&quot;SIGINT&quot;</span>, <span class="ruby-identifier">proc</span> { <span class="ruby-identifier">server</span>.<span class="ruby-identifier">shutdown</span> }
465
+ 348: }
466
+ 349: <span class="ruby-keyword kw">end</span>
467
+ 350: <span class="ruby-keyword kw">end</span>
467
468
  </pre>
468
469
  </div>
469
470
  </div>
@@ -478,10 +479,10 @@ Hash <a href="Capcode.html#M000004">session</a>
478
479
  <p class="source-link">[ <a href="javascript:toggleSource('M000004_source')" id="l_M000004_source">show source</a> ]</p>
479
480
  <div id="M000004_source" class="dyn-source">
480
481
  <pre>
481
- <span class="ruby-comment cmt"># File lib/capcode.rb, line 164</span>
482
- 164: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">session</span>
483
- 165: <span class="ruby-ivar">@env</span>[<span class="ruby-value str">'rack.session'</span>]
484
- 166: <span class="ruby-keyword kw">end</span>
482
+ <span class="ruby-comment cmt"># File lib/capcode.rb, line 171</span>
483
+ 171: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">session</span>
484
+ 172: <span class="ruby-ivar">@env</span>[<span class="ruby-value str">'rack.session'</span>]
485
+ 173: <span class="ruby-keyword kw">end</span>
485
486
  </pre>
486
487
  </div>
487
488
  </div>