Capcode 0.7.1 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -131,21 +131,21 @@ then
131
131
  <p class="source-link">[ <a href="javascript:toggleSource('M000012_source')" id="l_M000012_source">show source</a> ]</p>
132
132
  <div id="M000012_source" class="dyn-source">
133
133
  <pre>
134
- <span class="ruby-comment cmt"># File lib/capcode.rb, line 128</span>
135
- 128: <span class="ruby-keyword kw">def</span> <span class="ruby-constant">URL</span>( <span class="ruby-identifier">klass</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">a</span> )
136
- 129: <span class="ruby-identifier">path</span> = <span class="ruby-keyword kw">nil</span>
137
- 130: <span class="ruby-identifier">a</span> = <span class="ruby-identifier">a</span>.<span class="ruby-identifier">delete_if</span>{ <span class="ruby-operator">|</span><span class="ruby-identifier">x</span><span class="ruby-operator">|</span> <span class="ruby-identifier">x</span>.<span class="ruby-identifier">nil?</span> }
138
- 131:
139
- 132: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">klass</span>.<span class="ruby-identifier">class</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Class</span>
140
- 133: <span class="ruby-constant">Capcode</span>.<span class="ruby-identifier">routes</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">k</span><span class="ruby-operator">|</span>
141
- 134: <span class="ruby-identifier">path</span> = <span class="ruby-identifier">p</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">k</span>.<span class="ruby-identifier">class</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">klass</span>
142
- 135: <span class="ruby-keyword kw">end</span>
143
- 136: <span class="ruby-keyword kw">else</span>
144
- 137: <span class="ruby-identifier">path</span> = <span class="ruby-identifier">klass</span>
145
- 138: <span class="ruby-keyword kw">end</span>
146
- 139:
147
- 140: <span class="ruby-identifier">path</span><span class="ruby-operator">+</span>((<span class="ruby-identifier">a</span>.<span class="ruby-identifier">size</span><span class="ruby-operator">&gt;</span><span class="ruby-value">0</span>)<span class="ruby-operator">?</span>(<span class="ruby-value str">&quot;/&quot;</span><span class="ruby-operator">+</span><span class="ruby-identifier">a</span>.<span class="ruby-identifier">join</span>(<span class="ruby-value str">&quot;/&quot;</span>))<span class="ruby-operator">:</span>(<span class="ruby-value str">&quot;&quot;</span>))
148
- 141: <span class="ruby-keyword kw">end</span>
134
+ <span class="ruby-comment cmt"># File lib/capcode.rb, line 126</span>
135
+ 126: <span class="ruby-keyword kw">def</span> <span class="ruby-constant">URL</span>( <span class="ruby-identifier">klass</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">a</span> )
136
+ 127: <span class="ruby-identifier">path</span> = <span class="ruby-keyword kw">nil</span>
137
+ 128: <span class="ruby-identifier">a</span> = <span class="ruby-identifier">a</span>.<span class="ruby-identifier">delete_if</span>{ <span class="ruby-operator">|</span><span class="ruby-identifier">x</span><span class="ruby-operator">|</span> <span class="ruby-identifier">x</span>.<span class="ruby-identifier">nil?</span> }
138
+ 129:
139
+ 130: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">klass</span>.<span class="ruby-identifier">class</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Class</span>
140
+ 131: <span class="ruby-constant">Capcode</span>.<span class="ruby-identifier">routes</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">k</span><span class="ruby-operator">|</span>
141
+ 132: <span class="ruby-identifier">path</span> = <span class="ruby-identifier">p</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">k</span>.<span class="ruby-identifier">class</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">klass</span>
142
+ 133: <span class="ruby-keyword kw">end</span>
143
+ 134: <span class="ruby-keyword kw">else</span>
144
+ 135: <span class="ruby-identifier">path</span> = <span class="ruby-identifier">klass</span>
145
+ 136: <span class="ruby-keyword kw">end</span>
146
+ 137:
147
+ 138: <span class="ruby-identifier">path</span><span class="ruby-operator">+</span>((<span class="ruby-identifier">a</span>.<span class="ruby-identifier">size</span><span class="ruby-operator">&gt;</span><span class="ruby-value">0</span>)<span class="ruby-operator">?</span>(<span class="ruby-value str">&quot;/&quot;</span><span class="ruby-operator">+</span><span class="ruby-identifier">a</span>.<span class="ruby-identifier">join</span>(<span class="ruby-value str">&quot;/&quot;</span>))<span class="ruby-operator">:</span>(<span class="ruby-value str">&quot;&quot;</span>))
148
+ 139: <span class="ruby-keyword kw">end</span>
149
149
  </pre>
150
150
  </div>
151
151
  </div>
@@ -161,12 +161,12 @@ markup in an identifier.
161
161
  <p class="source-link">[ <a href="javascript:toggleSource('M000013_source')" id="l_M000013_source">show source</a> ]</p>
162
162
  <div id="M000013_source" class="dyn-source">
163
163
  <pre>
164
- <span class="ruby-comment cmt"># File lib/capcode.rb, line 144</span>
165
- 144: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">content_for</span>( <span class="ruby-identifier">x</span> )
166
- 145: <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@@__ARGS__</span>.<span class="ruby-identifier">map</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">_</span><span class="ruby-operator">|</span> <span class="ruby-identifier">_</span>.<span class="ruby-identifier">to_s</span> }.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">x</span>.<span class="ruby-identifier">to_s</span>)
167
- 146: <span class="ruby-keyword kw">yield</span>
168
- 147: <span class="ruby-keyword kw">end</span>
169
- 148: <span class="ruby-keyword kw">end</span>
164
+ <span class="ruby-comment cmt"># File lib/capcode.rb, line 142</span>
165
+ 142: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">content_for</span>( <span class="ruby-identifier">x</span> )
166
+ 143: <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@@__ARGS__</span>.<span class="ruby-identifier">map</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">_</span><span class="ruby-operator">|</span> <span class="ruby-identifier">_</span>.<span class="ruby-identifier">to_s</span> }.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">x</span>.<span class="ruby-identifier">to_s</span>)
167
+ 144: <span class="ruby-keyword kw">yield</span>
168
+ 145: <span class="ruby-keyword kw">end</span>
169
+ 146: <span class="ruby-keyword kw">end</span>
170
170
  </pre>
171
171
  </div>
172
172
  </div>
@@ -190,12 +190,12 @@ Help you to return a JSON response
190
190
  <p class="source-link">[ <a href="javascript:toggleSource('M000010_source')" id="l_M000010_source">show source</a> ]</p>
191
191
  <div id="M000010_source" class="dyn-source">
192
192
  <pre>
193
- <span class="ruby-comment cmt"># File lib/capcode.rb, line 92</span>
194
- 92: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">json</span>( <span class="ruby-identifier">d</span> ) <span class="ruby-comment cmt">## DELETE THIS IN 1.0.0</span>
195
- 93: <span class="ruby-identifier">warn</span>( <span class="ruby-value str">&quot;json is deprecated, please use `render( :json =&gt; ... )'&quot;</span> )
196
- 94: <span class="ruby-ivar">@response</span>[<span class="ruby-value str">'Content-Type'</span>] = <span class="ruby-value str">'application/json'</span>
197
- 95: <span class="ruby-identifier">d</span>.<span class="ruby-identifier">to_json</span>
198
- 96: <span class="ruby-keyword kw">end</span>
193
+ <span class="ruby-comment cmt"># File lib/capcode.rb, line 90</span>
194
+ 90: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">json</span>( <span class="ruby-identifier">d</span> ) <span class="ruby-comment cmt">## DELETE THIS IN 1.0.0</span>
195
+ 91: <span class="ruby-identifier">warn</span>( <span class="ruby-value str">&quot;json is deprecated, please use `render( :json =&gt; ... )'&quot;</span> )
196
+ 92: <span class="ruby-ivar">@response</span>[<span class="ruby-value str">'Content-Type'</span>] = <span class="ruby-value str">'application/json'</span>
197
+ 93: <span class="ruby-identifier">d</span>.<span class="ruby-identifier">to_json</span>
198
+ 94: <span class="ruby-keyword kw">end</span>
199
199
  </pre>
200
200
  </div>
201
201
  </div>
@@ -223,10 +223,10 @@ Send a <a href="Helpers.html#M000011">redirect</a> response
223
223
  <p class="source-link">[ <a href="javascript:toggleSource('M000011_source')" id="l_M000011_source">show source</a> ]</p>
224
224
  <div id="M000011_source" class="dyn-source">
225
225
  <pre>
226
- <span class="ruby-comment cmt"># File lib/capcode.rb, line 111</span>
227
- 111: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">redirect</span>( <span class="ruby-identifier">klass</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">a</span> )
228
- 112: [<span class="ruby-value">302</span>, {<span class="ruby-value str">'Location'</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-constant">URL</span>(<span class="ruby-identifier">klass</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">a</span>)}, <span class="ruby-value str">''</span>]
229
- 113: <span class="ruby-keyword kw">end</span>
226
+ <span class="ruby-comment cmt"># File lib/capcode.rb, line 109</span>
227
+ 109: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">redirect</span>( <span class="ruby-identifier">klass</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">a</span> )
228
+ 110: [<span class="ruby-value">302</span>, {<span class="ruby-value str">'Location'</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-constant">URL</span>(<span class="ruby-identifier">klass</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">a</span>)}, <span class="ruby-value str">''</span>]
229
+ 111: <span class="ruby-keyword kw">end</span>
230
230
  </pre>
231
231
  </div>
232
232
  </div>
@@ -278,35 +278,35 @@ If you want to use a specific layout, you can specify it with option
278
278
  <p class="source-link">[ <a href="javascript:toggleSource('M000009_source')" id="l_M000009_source">show source</a> ]</p>
279
279
  <div id="M000009_source" class="dyn-source">
280
280
  <pre>
281
- <span class="ruby-comment cmt"># File lib/capcode.rb, line 54</span>
282
- 54: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">render</span>( <span class="ruby-identifier">h</span> )
283
- 55: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">h</span>.<span class="ruby-identifier">class</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Hash</span>
284
- 56: <span class="ruby-identifier">render_type</span> = <span class="ruby-keyword kw">nil</span>
285
- 57:
286
- 58: <span class="ruby-identifier">h</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">k</span><span class="ruby-operator">|</span>
287
- 59: <span class="ruby-keyword kw">if</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-node">&quot;render_#{k.to_s}&quot;</span>)
288
- 60: <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">render_type</span>.<span class="ruby-identifier">nil?</span>
289
- 61: <span class="ruby-identifier">raise</span> <span class="ruby-constant">Capcode</span><span class="ruby-operator">::</span><span class="ruby-constant">RenderError</span>, <span class="ruby-node">&quot;Can't use multiple renderer (`#{render_type}' and `#{k}') !&quot;</span>, <span class="ruby-identifier">caller</span>
290
- 62: <span class="ruby-keyword kw">end</span>
291
- 63: <span class="ruby-identifier">render_type</span> = <span class="ruby-identifier">k</span>
292
- 64: <span class="ruby-keyword kw">end</span>
293
- 65: <span class="ruby-keyword kw">end</span>
294
- 66:
295
- 67: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">render_type</span>.<span class="ruby-identifier">nil?</span>
296
- 68: <span class="ruby-identifier">raise</span> <span class="ruby-constant">Capcode</span><span class="ruby-operator">::</span><span class="ruby-constant">RenderError</span>, <span class="ruby-value str">&quot;Renderer type not specified!&quot;</span>, <span class="ruby-identifier">caller</span>
297
- 69: <span class="ruby-keyword kw">end</span>
281
+ <span class="ruby-comment cmt"># File lib/capcode.rb, line 52</span>
282
+ 52: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">render</span>( <span class="ruby-identifier">h</span> )
283
+ 53: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">h</span>.<span class="ruby-identifier">class</span> <span class="ruby-operator">==</span> <span class="ruby-constant">Hash</span>
284
+ 54: <span class="ruby-identifier">render_type</span> = <span class="ruby-keyword kw">nil</span>
285
+ 55:
286
+ 56: <span class="ruby-identifier">h</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">k</span><span class="ruby-operator">|</span>
287
+ 57: <span class="ruby-keyword kw">if</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-node">&quot;render_#{k.to_s}&quot;</span>)
288
+ 58: <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">render_type</span>.<span class="ruby-identifier">nil?</span>
289
+ 59: <span class="ruby-identifier">raise</span> <span class="ruby-constant">Capcode</span><span class="ruby-operator">::</span><span class="ruby-constant">RenderError</span>, <span class="ruby-node">&quot;Can't use multiple renderer (`#{render_type}' and `#{k}') !&quot;</span>, <span class="ruby-identifier">caller</span>
290
+ 60: <span class="ruby-keyword kw">end</span>
291
+ 61: <span class="ruby-identifier">render_type</span> = <span class="ruby-identifier">k</span>
292
+ 62: <span class="ruby-keyword kw">end</span>
293
+ 63: <span class="ruby-keyword kw">end</span>
294
+ 64:
295
+ 65: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">render_type</span>.<span class="ruby-identifier">nil?</span>
296
+ 66: <span class="ruby-identifier">raise</span> <span class="ruby-constant">Capcode</span><span class="ruby-operator">::</span><span class="ruby-constant">RenderError</span>, <span class="ruby-value str">&quot;Renderer type not specified!&quot;</span>, <span class="ruby-identifier">caller</span>
297
+ 67: <span class="ruby-keyword kw">end</span>
298
+ 68:
299
+ 69: <span class="ruby-identifier">render_name</span> = <span class="ruby-identifier">h</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">render_type</span>)
298
300
  70:
299
- 71: <span class="ruby-identifier">render_name</span> = <span class="ruby-identifier">h</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">render_type</span>)
300
- 72:
301
- 73: <span class="ruby-keyword kw">begin</span>
302
- 74: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">send</span>( <span class="ruby-node">&quot;render_#{render_type.to_s}&quot;</span>, <span class="ruby-identifier">render_name</span>, <span class="ruby-identifier">h</span> )
303
- 75: <span class="ruby-keyword kw">rescue</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span>
304
- 76: <span class="ruby-identifier">raise</span> <span class="ruby-constant">Capcode</span><span class="ruby-operator">::</span><span class="ruby-constant">RenderError</span>, <span class="ruby-node">&quot;Error rendering `#{render_type.to_s}' : #{e.message}&quot;</span>, <span class="ruby-identifier">caller</span>
305
- 77: <span class="ruby-keyword kw">end</span>
306
- 78: <span class="ruby-keyword kw">else</span>
307
- 79: <span class="ruby-identifier">render</span>( <span class="ruby-identifier">:text</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">h</span> )
308
- 80: <span class="ruby-keyword kw">end</span>
309
- 81: <span class="ruby-keyword kw">end</span>
301
+ 71: <span class="ruby-keyword kw">begin</span>
302
+ 72: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">send</span>( <span class="ruby-node">&quot;render_#{render_type.to_s}&quot;</span>, <span class="ruby-identifier">render_name</span>, <span class="ruby-identifier">h</span> )
303
+ 73: <span class="ruby-keyword kw">rescue</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span>
304
+ 74: <span class="ruby-identifier">raise</span> <span class="ruby-constant">Capcode</span><span class="ruby-operator">::</span><span class="ruby-constant">RenderError</span>, <span class="ruby-node">&quot;Error rendering `#{render_type.to_s}' : #{e.message}&quot;</span>, <span class="ruby-identifier">caller</span>
305
+ 75: <span class="ruby-keyword kw">end</span>
306
+ 76: <span class="ruby-keyword kw">else</span>
307
+ 77: <span class="ruby-identifier">render</span>( <span class="ruby-identifier">:text</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">h</span> )
308
+ 78: <span class="ruby-keyword kw">end</span>
309
+ 79: <span class="ruby-keyword kw">end</span>
310
310
  </pre>
311
311
  </div>
312
312
  </div>
data/doc/rdoc/created.rid CHANGED
@@ -1 +1 @@
1
- Thu, 02 Jul 2009 17:58:19 +0200
1
+ Wed, 08 Jul 2009 01:50:53 +0200
@@ -81,7 +81,7 @@ end</strong>
81
81
  <div id="README" class="page_shade">
82
82
  <div class="page">
83
83
  <div class="header">
84
- <div class="path">README / Thu Jul 02 17:56:23 +0200 2009</div>
84
+ <div class="path">README / Wed Jul 08 01:50:29 +0200 2009</div>
85
85
  </div>
86
86
 
87
87
  <h1><a href="../classes/Capcode.html">Capcode</a></h1>
@@ -98,6 +98,33 @@ Copyright (C) 2009 Gregoire Lejeune
98
98
  <a href="../classes/Capcode.html">Capcode</a> is a web microframework
99
99
  </p>
100
100
  <h2>FEATURES/PROBLEMS:</h2>
101
+ <h3>0.8.0</h3>
102
+ <ul>
103
+ <li>Bugs corrections in haml and text renderer
104
+
105
+ </li>
106
+ <li>Text renderer is now automaticaly included
107
+
108
+ </li>
109
+ <li>Add sass renderer
110
+
111
+ </li>
112
+ <li>:working_directory is no more available
113
+
114
+ </li>
115
+ <li>:root is now the real root directory
116
+
117
+ </li>
118
+ <li>Add :static option to set the directory for static files
119
+
120
+ </li>
121
+ <li>Add -r and -s (for root and static directories) options
122
+
123
+ </li>
124
+ <li>Add static renderer
125
+
126
+ </li>
127
+ </ul>
101
128
  <h3>0.7.1</h3>
102
129
  <ul>
103
130
  <li>You need to include Capcode::Resource in your models !!!!
@@ -81,7 +81,7 @@ end</strong>
81
81
  <div id="lib/capcode.rb" class="page_shade">
82
82
  <div class="page">
83
83
  <div class="header">
84
- <div class="path">lib/capcode.rb / Thu Jul 02 17:56:13 +0200 2009</div>
84
+ <div class="path">lib/capcode.rb / Wed Jul 08 01:46:27 +0200 2009</div>
85
85
  </div>
86
86
 
87
87
  <pre>
@@ -524,6 +524,7 @@ License instead of this License.
524
524
  <li>mime/types</li>
525
525
  <li>capcode/version</li>
526
526
  <li>capcode/core_ext</li>
527
+ <li>capcode/render/text</li>
527
528
  <li>mongrel</li>
528
529
  </ul>
529
530
 
data/examples/sample.rb CHANGED
@@ -3,10 +3,10 @@ require 'rubygems'
3
3
  require 'capcode'
4
4
  require 'capcode/render/markaby'
5
5
  require 'capcode/render/haml'
6
- Capcode::Helpers.haml_path = "./haml"
6
+ Capcode::Helpers.haml_path = "haml"
7
7
  require 'capcode/render/json'
8
8
  require 'capcode/render/erb'
9
- Capcode::Helpers.erb_path = "./erb"
9
+ Capcode::Helpers.erb_path = "erb"
10
10
 
11
11
  module Capcode
12
12
  module Helpers
@@ -110,4 +110,4 @@ module Capcode::Views
110
110
  end
111
111
  end
112
112
 
113
- Capcode.run( :port => 3001, :host => "localhost", :root => "." )
113
+ Capcode.run( :port => 3001, :host => "localhost", :static => "static" )
File without changes
data/lib/capcode.rb CHANGED
@@ -7,9 +7,11 @@ require 'irb'
7
7
  require 'mime/types'
8
8
  require 'capcode/version'
9
9
  require 'capcode/core_ext'
10
+ require 'capcode/render/text'
10
11
 
11
12
  module Capcode
12
13
  @@__ROUTES = {}
14
+ @@__STATIC_DIR = nil
13
15
 
14
16
  # @@__FILTERS = []
15
17
  # def self.before_filter( opts, &b )
@@ -30,11 +32,7 @@ module Capcode
30
32
 
31
33
  # Helpers contains methods available in your controllers
32
34
  module Helpers
33
- @@__ROOT_DIRECTORY = ""
34
35
  @@__ARGS__ = nil
35
- def self.root=(p) #:nodoc:
36
- @@__ROOT_DIRECTORY=p
37
- end
38
36
 
39
37
  # Render a view
40
38
  #
@@ -165,66 +163,22 @@ module Capcode
165
163
  #
166
164
  # Do the same (r500, r501, r403) to customize 500, 501, 403 errors
167
165
  class HTTPError
168
- def initialize(app, root)#:nodoc:
166
+ def initialize(app)#:nodoc:
169
167
  @app = app
170
- @root = root
171
168
  end
172
169
 
173
170
  def call(env) #:nodoc:
174
- @env = env
175
- @status, @headers, @body = @app.call(@env)
176
-
177
- if @status == 404
178
- if @env["PATH_INFO"].include? ".."
179
- @status = 403
180
- @headers = {}
181
- @body = '403 - Invalid path'
182
- error
183
- end
184
-
185
- @path = File.join(@root, @env["PATH_INFO"])
186
- begin
187
- if File.file?(@path) && File.readable?(@path)
188
- serving
189
- else
190
- tmpPath = File.join(@root, @env["PATH_INFO"], "index.html")
191
- if File.file?(tmpPath) && File.readable?(tmpPath) # @env["PATH_INFO"][-1].chr == "/" &&
192
- @path = tmpPath
193
- serving
194
- else
195
- raise Errno::EPERM
196
- end
197
- end
198
- rescue SystemCallError
199
- error
200
- end
201
- else
202
- error
203
- end
204
- end
205
-
206
- def serving #:nodoc:
207
- body = open(@path).read
208
- size = body.size
171
+ status, headers, body = @app.call(env)
209
172
 
210
- [200, {
211
- "Last-Modified" => File.mtime(@path).httpdate,
212
- "Content-Type" => (MIME::Types.type_for(@path)[0] || 'text/plain').to_s,
213
- "Content-Length" => size.to_s
214
- }, body]
215
- end
216
-
217
- def error #:nodoc:
218
- if self.methods.include? "r#{@status}"
219
- @body = self.send( "r#{@status}", @env['REQUEST_PATH'] )
220
- @headers['Content-Length'] = @body.length.to_s
173
+ if self.methods.include? "r#{status}"
174
+ body = self.send( "r#{status}", env['REQUEST_PATH'] )
175
+ headers['Content-Length'] = body.length.to_s
221
176
  end
222
177
 
223
- [@status, @headers, @body]
178
+ [status, headers, body]
224
179
  end
225
-
226
180
  end
227
-
181
+
228
182
  class << self
229
183
  attr :__args__, true
230
184
 
@@ -389,9 +343,11 @@ module Capcode
389
343
  # * <tt>:pid</tt> = PID file (default: $0.pid)
390
344
  # * <tt>:daemonize</tt> = Daemonize application (default: false)
391
345
  # * <tt>:db_config</tt> = database configuration file (default: database.yml)
392
- # * <tt>:root</tt> = root directory (default: "." -- relative to the working directory)
393
- # * <tt>:working_directory</tt> = Working directory (default: directory of the main.rb)
346
+ # * <tt>:static</tt> = Static directory (default: none -- relative to the working directory)
347
+ # * <tt>:root</tt> = Root directory (default: directory of the main.rb) -- This is also the working directory !
394
348
  def run( args = {} )
349
+ __VERBOSE = false
350
+
395
351
  conf = {
396
352
  :port => args[:port]||3000,
397
353
  :host => args[:host]||"localhost",
@@ -401,8 +357,8 @@ module Capcode
401
357
  :pid => args[:pid]||"#{$0}.pid",
402
358
  :daemonize => args[:daemonize]||false,
403
359
  :db_config => args[:db_config]||"database.yml",
404
- :root => args[:root]||".",
405
- :working_directory => args[:working_directory]||File.expand_path(File.dirname($0)),
360
+ :static => args[:static]||nil,
361
+ :root => args[:root]||File.expand_path(File.dirname($0)),
406
362
 
407
363
  :console => false
408
364
  }
@@ -425,18 +381,27 @@ module Capcode
425
381
  opts.on( "-d", "--daemonize [true|false]", "Daemonize (default: #{conf[:daemonize]})" ) { |d|
426
382
  conf[:daemonize] = d
427
383
  }
384
+ opts.on( "-r", "--root PATH", "Working directory (default: #{conf[:root]})" ) { |w|
385
+ conf[:root] = w
386
+ }
387
+ opts.on( "-s", "--static PATH", "Static directory -- relative to the root directory (default: #{conf[:static]})" ) { |r|
388
+ conf[:static] = r
389
+ }
428
390
 
429
391
  opts.separator ""
430
392
  opts.separator "Common options:"
431
393
 
432
- opts.on_tail("-?", "--help", "Show this message") do
394
+ opts.on("-?", "--help", "Show this message") do
433
395
  puts opts
434
396
  exit
435
397
  end
436
- opts.on_tail("-v", "--version", "Show versions") do
398
+ opts.on("-v", "--version", "Show versions") do
437
399
  puts "Capcode version #{Capcode::CAPCOD_VERION} (ruby v#{RUBY_VERSION})"
438
400
  exit
439
401
  end
402
+ opts.on_tail( "-V", "--verbose", "Run in verbose mode" ) do
403
+ __VERBOSE = true
404
+ end
440
405
  end
441
406
 
442
407
  begin
@@ -448,9 +413,8 @@ module Capcode
448
413
  end
449
414
 
450
415
  # Run in the Working directory
451
- Dir.chdir( conf[:working_directory] ) do
452
- # Set root directory for helpers
453
- Capcode::Helpers.root = File.expand_path( conf[:root] )
416
+ puts "** Go on root directory (#{File.expand_path(conf[:root])})" if __VERBOSE
417
+ Dir.chdir( conf[:root] ) do
454
418
 
455
419
  # Check that mongrel exists
456
420
  if conf[:server].nil? || conf[:server] == "mongrel"
@@ -477,9 +441,21 @@ module Capcode
477
441
  end
478
442
  end
479
443
 
444
+ # Set Static directory
445
+ @@__STATIC_DIR = File.expand_path(File.join("/", conf[:static]))
446
+
447
+ # Initialize Rack App
448
+ puts "** Map routes." if __VERBOSE
480
449
  app = Rack::URLMap.new(@@__ROUTES)
450
+ puts "** Initialize static directory (#{conf[:static]})" if __VERBOSE
451
+ app = Rack::Static.new(
452
+ app,
453
+ :urls => [@@__STATIC_DIR],
454
+ :root => File.expand_path(conf[:root])
455
+ ) unless conf[:static].nil?
456
+ puts "** Initialize session" if __VERBOSE
481
457
  app = Rack::Session::Cookie.new( app, conf[:session] )
482
- app = Capcode::HTTPError.new(app, conf[:root])
458
+ app = Capcode::HTTPError.new(app)
483
459
  app = Rack::ContentLength.new(app)
484
460
  app = Rack::Lint.new(app)
485
461
  app = Rack::ShowExceptions.new(app)
@@ -488,17 +464,21 @@ module Capcode
488
464
 
489
465
  # From rackup !!!
490
466
  if conf[:daemonize]
491
- if RUBY_VERSION < "1.9"
492
- exit if fork
493
- Process.setsid
494
- exit if fork
495
- # Dir.chdir "/"
496
- File.umask 0000
497
- STDIN.reopen "/dev/null"
498
- STDOUT.reopen "/dev/null", "a"
499
- STDERR.reopen "/dev/null", "a"
467
+ if /java/.match(RUBY_PLATFORM).nil?
468
+ if RUBY_VERSION < "1.9"
469
+ exit if fork
470
+ Process.setsid
471
+ exit if fork
472
+ # Dir.chdir "/"
473
+ File.umask 0000
474
+ STDIN.reopen "/dev/null"
475
+ STDOUT.reopen "/dev/null", "a"
476
+ STDERR.reopen "/dev/null", "a"
477
+ else
478
+ Process.daemon
479
+ end
500
480
  else
501
- Process.daemon
481
+ puts "!! daemonize option unavailable on #{RUBY_PLATFORM} platform."
502
482
  end
503
483
 
504
484
  File.open(conf[:pid], 'w'){ |f| f.write("#{Process.pid}") }
@@ -539,5 +519,10 @@ module Capcode
539
519
  def routes #:nodoc:
540
520
  @@__ROUTES
541
521
  end
522
+
523
+ def static #:nodoc:
524
+ puts "-------------------- #{@@__STATIC_DIR}"
525
+ @@__STATIC_DIR
526
+ end
542
527
  end
543
528
  end