Capcode 0.6.2 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -111,10 +111,10 @@ controllers
111
111
  <p class="source-link">[ <a href="javascript:toggleSource('M000009_source')" id="l_M000009_source">show source</a> ]</p>
112
112
  <div id="M000009_source" class="dyn-source">
113
113
  <pre>
114
- <span class="ruby-comment cmt"># File lib/capcode.rb, line 27</span>
115
- 27: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">root=</span>(<span class="ruby-identifier">p</span>)
116
- 28: <span class="ruby-ivar">@@__ROOT_DIRECTORY</span>=<span class="ruby-identifier">p</span>
117
- 29: <span class="ruby-keyword kw">end</span>
114
+ <span class="ruby-comment cmt"># File lib/capcode.rb, line 35</span>
115
+ 35: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">root=</span>(<span class="ruby-identifier">p</span>)
116
+ 36: <span class="ruby-ivar">@@__ROOT_DIRECTORY</span>=<span class="ruby-identifier">p</span>
117
+ 37: <span class="ruby-keyword kw">end</span>
118
118
  </pre>
119
119
  </div>
120
120
  </div>
@@ -146,21 +146,21 @@ then
146
146
  <p class="source-link">[ <a href="javascript:toggleSource('M000013_source')" id="l_M000013_source">show source</a> ]</p>
147
147
  <div id="M000013_source" class="dyn-source">
148
148
  <pre>
149
- <span class="ruby-comment cmt"># File lib/capcode.rb, line 120</span>
150
- 120: <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> )
151
- 121: <span class="ruby-identifier">path</span> = <span class="ruby-keyword kw">nil</span>
152
- 122: <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> }
153
- 123:
154
- 124: <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>
155
- 125: <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>
156
- 126: <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>
157
- 127: <span class="ruby-keyword kw">end</span>
158
- 128: <span class="ruby-keyword kw">else</span>
159
- 129: <span class="ruby-identifier">path</span> = <span class="ruby-identifier">klass</span>
160
- 130: <span class="ruby-keyword kw">end</span>
149
+ <span class="ruby-comment cmt"># File lib/capcode.rb, line 128</span>
150
+ 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> )
151
+ 129: <span class="ruby-identifier">path</span> = <span class="ruby-keyword kw">nil</span>
152
+ 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> }
161
153
  131:
162
- 132: <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>))
163
- 133: <span class="ruby-keyword kw">end</span>
154
+ 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>
155
+ 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>
156
+ 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>
157
+ 135: <span class="ruby-keyword kw">end</span>
158
+ 136: <span class="ruby-keyword kw">else</span>
159
+ 137: <span class="ruby-identifier">path</span> = <span class="ruby-identifier">klass</span>
160
+ 138: <span class="ruby-keyword kw">end</span>
161
+ 139:
162
+ 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>))
163
+ 141: <span class="ruby-keyword kw">end</span>
164
164
  </pre>
165
165
  </div>
166
166
  </div>
@@ -176,12 +176,12 @@ markup in an identifier.
176
176
  <p class="source-link">[ <a href="javascript:toggleSource('M000014_source')" id="l_M000014_source">show source</a> ]</p>
177
177
  <div id="M000014_source" class="dyn-source">
178
178
  <pre>
179
- <span class="ruby-comment cmt"># File lib/capcode.rb, line 136</span>
180
- 136: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">content_for</span>( <span class="ruby-identifier">x</span> )
181
- 137: <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>)
182
- 138: <span class="ruby-keyword kw">yield</span>
183
- 139: <span class="ruby-keyword kw">end</span>
184
- 140: <span class="ruby-keyword kw">end</span>
179
+ <span class="ruby-comment cmt"># File lib/capcode.rb, line 144</span>
180
+ 144: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">content_for</span>( <span class="ruby-identifier">x</span> )
181
+ 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>)
182
+ 146: <span class="ruby-keyword kw">yield</span>
183
+ 147: <span class="ruby-keyword kw">end</span>
184
+ 148: <span class="ruby-keyword kw">end</span>
185
185
  </pre>
186
186
  </div>
187
187
  </div>
@@ -205,12 +205,12 @@ Help you to return a JSON response
205
205
  <p class="source-link">[ <a href="javascript:toggleSource('M000011_source')" id="l_M000011_source">show source</a> ]</p>
206
206
  <div id="M000011_source" class="dyn-source">
207
207
  <pre>
208
- <span class="ruby-comment cmt"># File lib/capcode.rb, line 84</span>
209
- 84: <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>
210
- 85: <span class="ruby-identifier">warn</span>( <span class="ruby-value str">&quot;json is deprecated, please use `render( :json =&gt; ... )'&quot;</span> )
211
- 86: <span class="ruby-ivar">@response</span>[<span class="ruby-value str">'Content-Type'</span>] = <span class="ruby-value str">'application/json'</span>
212
- 87: <span class="ruby-identifier">d</span>.<span class="ruby-identifier">to_json</span>
213
- 88: <span class="ruby-keyword kw">end</span>
208
+ <span class="ruby-comment cmt"># File lib/capcode.rb, line 92</span>
209
+ 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>
210
+ 93: <span class="ruby-identifier">warn</span>( <span class="ruby-value str">&quot;json is deprecated, please use `render( :json =&gt; ... )'&quot;</span> )
211
+ 94: <span class="ruby-ivar">@response</span>[<span class="ruby-value str">'Content-Type'</span>] = <span class="ruby-value str">'application/json'</span>
212
+ 95: <span class="ruby-identifier">d</span>.<span class="ruby-identifier">to_json</span>
213
+ 96: <span class="ruby-keyword kw">end</span>
214
214
  </pre>
215
215
  </div>
216
216
  </div>
@@ -238,10 +238,10 @@ Send a <a href="Helpers.html#M000012">redirect</a> response
238
238
  <p class="source-link">[ <a href="javascript:toggleSource('M000012_source')" id="l_M000012_source">show source</a> ]</p>
239
239
  <div id="M000012_source" class="dyn-source">
240
240
  <pre>
241
- <span class="ruby-comment cmt"># File lib/capcode.rb, line 103</span>
242
- 103: <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> )
243
- 104: [<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>]
244
- 105: <span class="ruby-keyword kw">end</span>
241
+ <span class="ruby-comment cmt"># File lib/capcode.rb, line 111</span>
242
+ 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> )
243
+ 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>]
244
+ 113: <span class="ruby-keyword kw">end</span>
245
245
  </pre>
246
246
  </div>
247
247
  </div>
@@ -293,35 +293,35 @@ If you want to use a specific layout, you can specify it with option
293
293
  <p class="source-link">[ <a href="javascript:toggleSource('M000010_source')" id="l_M000010_source">show source</a> ]</p>
294
294
  <div id="M000010_source" class="dyn-source">
295
295
  <pre>
296
- <span class="ruby-comment cmt"># File lib/capcode.rb, line 46</span>
297
- 46: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">render</span>( <span class="ruby-identifier">h</span> )
298
- 47: <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>
299
- 48: <span class="ruby-identifier">render_type</span> = <span class="ruby-keyword kw">nil</span>
300
- 49:
301
- 50: <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>
302
- 51: <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>)
303
- 52: <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">render_type</span>.<span class="ruby-identifier">nil?</span>
304
- 53: <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>
305
- 54: <span class="ruby-keyword kw">end</span>
306
- 55: <span class="ruby-identifier">render_type</span> = <span class="ruby-identifier">k</span>
307
- 56: <span class="ruby-keyword kw">end</span>
308
- 57: <span class="ruby-keyword kw">end</span>
309
- 58:
310
- 59: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">render_type</span>.<span class="ruby-identifier">nil?</span>
311
- 60: <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>
312
- 61: <span class="ruby-keyword kw">end</span>
313
- 62:
314
- 63: <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>)
315
- 64:
316
- 65: <span class="ruby-keyword kw">begin</span>
317
- 66: <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> )
318
- 67: <span class="ruby-keyword kw">rescue</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span>
319
- 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-node">&quot;Error rendering `#{render_type.to_s}' : #{e.message}&quot;</span>, <span class="ruby-identifier">caller</span>
296
+ <span class="ruby-comment cmt"># File lib/capcode.rb, line 54</span>
297
+ 54: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">render</span>( <span class="ruby-identifier">h</span> )
298
+ 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>
299
+ 56: <span class="ruby-identifier">render_type</span> = <span class="ruby-keyword kw">nil</span>
300
+ 57:
301
+ 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>
302
+ 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>)
303
+ 60: <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">render_type</span>.<span class="ruby-identifier">nil?</span>
304
+ 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>
305
+ 62: <span class="ruby-keyword kw">end</span>
306
+ 63: <span class="ruby-identifier">render_type</span> = <span class="ruby-identifier">k</span>
307
+ 64: <span class="ruby-keyword kw">end</span>
308
+ 65: <span class="ruby-keyword kw">end</span>
309
+ 66:
310
+ 67: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">render_type</span>.<span class="ruby-identifier">nil?</span>
311
+ 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>
320
312
  69: <span class="ruby-keyword kw">end</span>
321
- 70: <span class="ruby-keyword kw">else</span>
322
- 71: <span class="ruby-identifier">render</span>( <span class="ruby-identifier">:text</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">h</span> )
323
- 72: <span class="ruby-keyword kw">end</span>
324
- 73: <span class="ruby-keyword kw">end</span>
313
+ 70:
314
+ 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>)
315
+ 72:
316
+ 73: <span class="ruby-keyword kw">begin</span>
317
+ 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> )
318
+ 75: <span class="ruby-keyword kw">rescue</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span>
319
+ 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>
320
+ 77: <span class="ruby-keyword kw">end</span>
321
+ 78: <span class="ruby-keyword kw">else</span>
322
+ 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> )
323
+ 80: <span class="ruby-keyword kw">end</span>
324
+ 81: <span class="ruby-keyword kw">end</span>
325
325
  </pre>
326
326
  </div>
327
327
  </div>
@@ -1 +1 @@
1
- Mon, 29 Jun 2009 17:37:38 +0200
1
+ Wed, 01 Jul 2009 18:49:06 +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 / Mon Jun 29 17:32:18 +0200 2009</div>
84
+ <div class="path">README / Wed Jul 01 12:10:01 +0200 2009</div>
85
85
  </div>
86
86
 
87
87
  <h1><a href="../classes/Capcode.html">Capcode</a></h1>
@@ -98,6 +98,20 @@ 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.7.0</h3>
102
+ <ul>
103
+ <li>You no more need to include Capcode::Resource in your models
104
+
105
+ </li>
106
+ <li><a href="../classes/Capcode.html#M000008">Capcode.run</a> now accept a
107
+ block. The content of the block is executed just before the server start.
108
+
109
+ </li>
110
+ <li>Add options support : you can now change the defaults port and host,
111
+ daemonize or not and run in console mode.
112
+
113
+ </li>
114
+ </ul>
101
115
  <h3>0.6.2</h3>
102
116
  <ul>
103
117
  <li>Add Markaby, Erb and Haml layout see Capcode::Helpers.render for more
@@ -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 / Mon Jun 29 17:30:35 +0200 2009</div>
84
+ <div class="path">lib/capcode.rb / Wed Jul 01 17:54:22 +0200 2009</div>
85
85
  </div>
86
86
 
87
87
  <pre>
@@ -519,6 +519,8 @@ License instead of this License.
519
519
  <li>rack</li>
520
520
  <li>json</li>
521
521
  <li>logger</li>
522
+ <li>optparse</li>
523
+ <li>irb</li>
522
524
  <li>mime/types</li>
523
525
  <li>capcode/version</li>
524
526
  <li>capcode/core_ext</li>
@@ -4,13 +4,21 @@ require 'capcode'
4
4
  require 'capcode/base/couchdb'
5
5
  require 'capcode/render/markaby'
6
6
 
7
- class Story < Capcode::Base
8
- include Capcode::Resource
7
+ class User < Capcode::Base
8
+ property :login, String
9
+ property :passwd, String
9
10
 
11
+ has_many :stories
12
+ end
13
+
14
+ class Story < Capcode::Base
10
15
  property :title, String
11
16
  property :body, String
12
17
  property :create_at, String
13
18
 
19
+ property :user_id, String
20
+ belongs_to :user
21
+
14
22
  default_sort :create_at
15
23
  end
16
24
 
@@ -47,10 +55,62 @@ module Capcode
47
55
 
48
56
  class Add < Route '/add'
49
57
  def get
50
- render( :markaby => :add, :layout => :my_layout )
58
+ if session[:user]
59
+ @story = Story.new()
60
+ render( :markaby => :add, :layout => :my_layout )
61
+ else
62
+ redirect( Login )
63
+ end
51
64
  end
52
65
  def post
53
- Story.create( :title => params['title'], :body => params['body'], :create_at => Time.now )
66
+ if session[:user]
67
+ s = Story.create( :title => params['title'], :body => params['body'], :create_at => Time.now, :user_id => session[:user] )
68
+ redirect( Index )
69
+ else
70
+ redirect( Login )
71
+ end
72
+ end
73
+ end
74
+
75
+ class Edit < Route '/edit/(.*)'
76
+ def get( id )
77
+ if session[:user]
78
+ @story = Story.find( id )
79
+ render( :markaby => :add, :layout => :my_layout )
80
+ else
81
+ redirect( Index )
82
+ end
83
+ end
84
+ def post
85
+ story = Story.find( params['id'] )
86
+ story.title = params['title']
87
+ story.body = params['body']
88
+ story.save
89
+
90
+ redirect( Index )
91
+ end
92
+ end
93
+
94
+ class Login < Route '/login'
95
+ def get
96
+ if session[:user]
97
+ redirect( Index )
98
+ else
99
+ render( :markaby => :login, :layout => :my_layout )
100
+ end
101
+ end
102
+ def post
103
+ u = User.find_by_login_and_passwd( params['login'], params['passwd'] )
104
+ unless u.nil?
105
+ session[:user] = u.id
106
+ end
107
+ redirect( Index )
108
+ end
109
+ end
110
+
111
+ class Logout < Route '/logout'
112
+ def get
113
+ session[:user] = nil
54
114
  redirect( Index )
55
115
  end
56
116
  end
@@ -66,7 +126,8 @@ module Capcode::Views
66
126
  h2 s.title
67
127
  p.info do
68
128
  _post_menu(s)
69
- text s.create_at #.strftime('%B %M, %Y @ %H:%M ')
129
+ text " #{s.create_at} by " #.strftime('%B %M, %Y @ %H:%M ')
130
+ i s.user.login
70
131
  end
71
132
  text s.body
72
133
  end
@@ -75,9 +136,26 @@ module Capcode::Views
75
136
  def add
76
137
  form :method => "POST" do
77
138
  text "Titre :"
78
- input :type => "text", :name => "title"; br
79
- textarea :name => "body" do; end; br
139
+ input :type => "text", :name => "title", :value => @story.title; br
140
+ textarea :name => "body" do; @story.body; end; br
80
141
  input :type => "submit"
142
+ input :type => "hidden", :name => "id", :value => @story.id
143
+ end
144
+ end
145
+
146
+ def login
147
+ form :method => "POST" do
148
+ table do
149
+ tr do
150
+ td "Login :"
151
+ td {input :type => "text", :name => "login"}
152
+ end
153
+ tr do
154
+ td "Password :"
155
+ td {input :type => "text", :name => "passwd"}
156
+ end
157
+ end
158
+ input :type => "submit", :value => "Login"
81
159
  end
82
160
  end
83
161
 
@@ -95,7 +173,13 @@ module Capcode::Views
95
173
  end
96
174
 
97
175
  p.footer! do
98
- a 'New', :href => URL(Capcode::Add)
176
+ if session[:user]
177
+ a 'New', :href => URL(Capcode::Add)
178
+ text "|"
179
+ a "Logout", :href => URL(Capcode::Logout)
180
+ else
181
+ a 'Login', :href => URL(Capcode::Login)
182
+ end
99
183
  text ' &ndash; Powered by '
100
184
  a 'Capcode', :href => 'http://capcode.rubyforge.org'
101
185
  end
@@ -104,14 +188,25 @@ module Capcode::Views
104
188
  end
105
189
 
106
190
  def _post_menu(post)
107
- text '('
108
- a "Del", :href => URL( Capcode::Remove, post.id, post.rev )
109
- text ')'
191
+ if session[:user]
192
+ text '['
193
+ a "Del", :href => URL( Capcode::Remove, post.id, post.rev )
194
+ text '|'
195
+ a "Edit", :href => URL( Capcode::Edit, post.id )
196
+ text ']'
197
+ end
110
198
  end
111
199
 
112
200
  end
113
201
 
114
- Capcode.run( :port => 3001, :host => "localhost", :db_config => "blog-couchdb.yml" )
202
+ Capcode.run( :port => 3001, :host => "localhost", :db_config => "blog-couchdb.yml" ) do |c|
203
+ admin = User.find_by_login( "admin" )
204
+ if admin.nil?
205
+ puts "Create admin user..."
206
+ admin = User.create( :login => "admin", :passwd => "admin" )
207
+ end
208
+ puts "Admin user : \n\tlogin = #{admin.login}\n\tpassword = #{admin.passwd}"
209
+ end
115
210
 
116
211
  __END__
117
212
  * {
@@ -4,8 +4,6 @@ require 'capcode'
4
4
  require 'capcode/base/dm'
5
5
 
6
6
  class Story < Capcode::Base
7
- include Capcode::Resource
8
-
9
7
  property :id, Integer, :serial => true
10
8
  property :title, String
11
9
  property :body, String
Binary file
@@ -2,6 +2,8 @@ require 'rubygems'
2
2
  require 'rack'
3
3
  require 'json' ## DELETE THIS IN 1.0.0
4
4
  require 'logger'
5
+ require 'optparse'
6
+ require 'irb'
5
7
  require 'mime/types'
6
8
  require 'capcode/version'
7
9
  require 'capcode/core_ext'
@@ -9,6 +11,12 @@ require 'capcode/core_ext'
9
11
  module Capcode
10
12
  @@__ROUTES = {}
11
13
 
14
+ # @@__FILTERS = []
15
+ # def self.before_filter( opts, &b )
16
+ # opts[:action] = b
17
+ # @@__FILTERS << opts
18
+ # end
19
+
12
20
  class ParameterError < ArgumentError #:nodoc:
13
21
  end
14
22
 
@@ -290,6 +298,21 @@ module Capcode
290
298
  @response = Rack::Response.new
291
299
  @request = Rack::Request.new(@env)
292
300
 
301
+ # __k = self.class.to_s.split( /::/ )[-1].downcase.to_sym
302
+ # @@__FILTERS.each do |f|
303
+ # proc = f.delete(:action)
304
+ # __run = true
305
+ # if f[:only]
306
+ # __run = f[:only].include?(__k)
307
+ # end
308
+ # if f[:except]
309
+ # __run = !f[:except].include?(__k)
310
+ # end
311
+ #
312
+ # # proc.call(self) if __run
313
+ # puts "call #{proc} for #{__k}"
314
+ # end
315
+
293
316
  r = case @env["REQUEST_METHOD"]
294
317
  when "GET"
295
318
  finalPath = nil
@@ -323,7 +346,7 @@ module Capcode
323
346
  while args.size < nargs
324
347
  args << nil
325
348
  end
326
-
349
+
327
350
  get( *args )
328
351
  when "POST"
329
352
  post
@@ -379,9 +402,47 @@ module Capcode
379
402
  :daemonize => args[:daemonize]||false,
380
403
  :db_config => args[:db_config]||"database.yml",
381
404
  :root => args[:root]||".",
382
- :working_directory => args[:working_directory]||File.expand_path(File.dirname($0))
405
+ :working_directory => args[:working_directory]||File.expand_path(File.dirname($0)),
406
+
407
+ :console => false
383
408
  }
384
409
 
410
+ # Parse options
411
+ opts = OptionParser.new do |opts|
412
+ opts.banner = "Usage: #{File.basename($0)} [options]"
413
+ opts.separator ""
414
+ opts.separator "Specific options:"
415
+
416
+ opts.on( "-C", "--console", "Run in console mode with IRB (default: false)" ) {
417
+ conf[:console] = true
418
+ }
419
+ opts.on( "-h", "--host HOSTNAME", "Host for web server to bind to (default: #{conf[:host]})" ) { |h|
420
+ conf[:host] = h
421
+ }
422
+ opts.on( "-p", "--port NUM", "Port for web server (default: #{conf[:port]})" ) { |p|
423
+ conf[:port] = p
424
+ }
425
+ opts.on( "-d", "--daemonize [true|false]", "Daemonize (default: #{conf[:daemonize]})" ) { |d|
426
+ conf[:daemonize] = d
427
+ }
428
+
429
+ opts.separator ""
430
+ opts.separator "Common options:"
431
+
432
+ opts.on_tail("-?", "--help", "Show this message") do
433
+ puts opts
434
+ exit
435
+ end
436
+ end
437
+
438
+ begin
439
+ opts.parse! ARGV
440
+ rescue OptionParser::ParseError => ex
441
+ STDERR.puts "!! #{ex.message}"
442
+ puts "** use `#{File.basename($0)} --help` for more details..."
443
+ exit 1
444
+ end
445
+
385
446
  # Run in the Working directory
386
447
  Dir.chdir( conf[:working_directory] ) do
387
448
  # Set root directory for helpers
@@ -445,6 +506,16 @@ module Capcode
445
506
  db_connect( conf[:db_config], conf[:log] )
446
507
  end
447
508
 
509
+ if block_given?
510
+ yield( self )
511
+ end
512
+
513
+ if conf[:console]
514
+ puts "Run console..."
515
+ IRB.start
516
+ exit
517
+ end
518
+
448
519
  # Start server
449
520
  case conf[:server]
450
521
  when "mongrel"