Class: Zorglub::Node
- Inherits:
-
Object
- Object
- Zorglub::Node
- Defined in:
- lib/zorglub/node.rb,
lib/zorglub/session.rb,
lib/zorglub/rack_session.rb
Constant Summary collapse
- UNDEFINED =
-1 # # class level engine, layout, static, layout_base_path, view_base_path configuration #
Class Attribute Summary collapse
-
.app ⇒ Object
Returns the value of attribute app.
-
.cache_lifetime ⇒ Object
readonly
Returns the value of attribute cache_lifetime.
-
.cli_vals ⇒ Object
readonly
Returns the value of attribute cli_vals.
-
.sessions ⇒ Object
readonly
Returns the value of attribute sessions.
-
.static ⇒ Object
readonly
Returns the value of attribute static.
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#engine ⇒ Object
readonly
Returns the value of attribute engine.
-
#meth ⇒ Object
readonly
Returns the value of attribute meth.
-
#mime ⇒ Object
readonly
Returns the value of attribute mime.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Class Method Summary collapse
- .after_all(meth = nil, &blk) ⇒ Object
- .before_all(meth = nil, &blk) ⇒ Object
- .call(env) ⇒ Object
- .call_after_hooks(obj) ⇒ Object
- .call_before_hooks(obj) ⇒ Object
- .cli_val(sym, *args) ⇒ Object
- .engine ⇒ Object
- .engine!(engine) ⇒ Object
- .error_404(node) ⇒ Object
- .inherited(sub) ⇒ Object
- .layout ⇒ Object
- .layout!(layout) ⇒ Object
- .layout_base_path ⇒ Object
- .layout_base_path!(path) ⇒ Object
- .map(app, location) ⇒ Object
- .no_layout! ⇒ Object
- .partial(env, meth, *args) ⇒ Object
- .r(*args) ⇒ Object
- .static!(val, lifetime = 0) ⇒ Object
- .view_base_path ⇒ Object
- .view_base_path!(path) ⇒ Object
Instance Method Summary collapse
-
#app ⇒ Object
instance level basic node functions.
- #cli_val(sym, *args) ⇒ Object
- #compile_page! ⇒ Object
-
#engine!(engine) ⇒ Object
instance level engine, layout, view, static configuration.
- #ext ⇒ Object
- #ext!(ext) ⇒ Object
- #feed!(no_hooks = false) ⇒ Object
- #html ⇒ Object
-
#initialize(env, meth, args, partial = false) ⇒ Node
constructor
A new instance of Node.
- #layout ⇒ Object
- #layout!(layout) ⇒ Object
- #map ⇒ Object
- #no_layout! ⇒ Object
- #r(*args) ⇒ Object
- #realize! ⇒ Object
- #redirect(target, options = {}, &block) ⇒ Object
- #redirect_body(target) ⇒ Object
- #session ⇒ Object
- #static ⇒ Object
- #static!(val, lifetime = 0) ⇒ Object
- #static_page!(path) ⇒ Object
- #view ⇒ Object
- #view!(view) ⇒ Object
Constructor Details
#initialize(env, meth, args, partial = false) ⇒ Node
Returns a new instance of Node.
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/zorglub/node.rb', line 251 def initialize env, meth, args, partial=false @meth = meth @args = args @partial = partial @request = Rack::Request.new env @response = Rack::Response.new @cli_vals ={} @debug = app.opt :debug @engine = self.class.engine @layout = ( partial ? nil : self.class.layout ) @view = r(meth) @static = self.class.static @cache_lifetime = self.class.cache_lifetime self.class.cli_vals.each do |s,v| cli_val s, *v end end |
Class Attribute Details
.app ⇒ Object
Returns the value of attribute app.
111 112 113 |
# File 'lib/zorglub/node.rb', line 111 def app @app end |
.cache_lifetime ⇒ Object (readonly)
Returns the value of attribute cache_lifetime.
15 16 17 |
# File 'lib/zorglub/node.rb', line 15 def cache_lifetime @cache_lifetime end |
.cli_vals ⇒ Object (readonly)
Returns the value of attribute cli_vals.
163 164 165 |
# File 'lib/zorglub/node.rb', line 163 def cli_vals @cli_vals end |
.sessions ⇒ Object (readonly)
Returns the value of attribute sessions.
12 13 14 |
# File 'lib/zorglub/session.rb', line 12 def sessions @sessions end |
.static ⇒ Object (readonly)
Returns the value of attribute static.
15 16 17 |
# File 'lib/zorglub/node.rb', line 15 def static @static end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
249 250 251 |
# File 'lib/zorglub/node.rb', line 249 def args @args end |
#content ⇒ Object (readonly)
Returns the value of attribute content.
249 250 251 |
# File 'lib/zorglub/node.rb', line 249 def content @content end |
#engine ⇒ Object (readonly)
Returns the value of attribute engine.
249 250 251 |
# File 'lib/zorglub/node.rb', line 249 def engine @engine end |
#meth ⇒ Object (readonly)
Returns the value of attribute meth.
249 250 251 |
# File 'lib/zorglub/node.rb', line 249 def meth @meth end |
#mime ⇒ Object (readonly)
Returns the value of attribute mime.
249 250 251 |
# File 'lib/zorglub/node.rb', line 249 def mime @mime end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
249 250 251 |
# File 'lib/zorglub/node.rb', line 249 def request @request end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
249 250 251 |
# File 'lib/zorglub/node.rb', line 249 def response @response end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
249 250 251 |
# File 'lib/zorglub/node.rb', line 249 def state @state end |
Class Method Details
.after_all(meth = nil, &blk) ⇒ Object
204 205 206 207 |
# File 'lib/zorglub/node.rb', line 204 def after_all meth=nil, &blk @cli_vals[:after_all]<< ( meth.nil? ? blk : meth ) @cli_vals[:after_all].uniq! end |
.before_all(meth = nil, &blk) ⇒ Object
195 196 197 198 |
# File 'lib/zorglub/node.rb', line 195 def before_all meth=nil, &blk @cli_vals[:before_all]<< ( meth.nil? ? blk : meth ) @cli_vals[:before_all].uniq! end |
.call(env) ⇒ Object
222 223 224 225 226 227 228 229 |
# File 'lib/zorglub/node.rb', line 222 def call env meth, *args = env['PATH_INFO'].sub(/^\/+/,'').split(/\//) meth ||= 'index' $stderr << "=> #{meth}(#{args.join ','})\n" if app.opt :debug node = self.new env, meth, args return error_404 node if not node.respond_to? meth node.realize! end |
.call_after_hooks(obj) ⇒ Object
200 201 202 |
# File 'lib/zorglub/node.rb', line 200 def call_after_hooks obj @cli_vals[:after_all].each do |blk| blk.call obj end end |
.call_before_hooks(obj) ⇒ Object
191 192 193 |
# File 'lib/zorglub/node.rb', line 191 def call_before_hooks obj @cli_vals[:before_all].each do |blk| blk.call obj end end |
.cli_val(sym, *args) ⇒ Object
165 166 167 168 169 170 171 172 |
# File 'lib/zorglub/node.rb', line 165 def cli_val sym, *args vals = @cli_vals[sym] ||= [] unless args.empty? vals.concat args vals.uniq! end vals end |
.engine ⇒ Object
21 22 23 24 |
# File 'lib/zorglub/node.rb', line 21 def engine @engine = @app.opt(:engine) if @engine==UNDEFINED and @app @engine end |
.engine!(engine) ⇒ Object
17 18 19 |
# File 'lib/zorglub/node.rb', line 17 def engine! engine @engine = engine end |
.error_404(node) ⇒ Object
238 239 240 241 242 243 244 245 |
# File 'lib/zorglub/node.rb', line 238 def error_404 node $stderr << " !! method not found\n" if app.opt :debug resp = node.response resp.status = 404 resp['Content-Type'] = 'text/plain' resp.write "%s mapped at %p can't respond to : %p" % [ node.class.name, node.map, node.meth ] resp end |
.inherited(sub) ⇒ Object
215 216 217 218 219 220 |
# File 'lib/zorglub/node.rb', line 215 def inherited sub sub.engine! ( engine || (self==Zorglub::Node ? UNDEFINED : nil ) ) sub.layout! ( layout || (self==Zorglub::Node ? UNDEFINED : nil ) ) sub.instance_variable_set :@cli_vals, {} @cli_vals.each do |s,v| sub.cli_val s, *v end end |
.layout ⇒ Object
34 35 36 37 |
# File 'lib/zorglub/node.rb', line 34 def layout @layout = @app.opt(:layout) if @layout==UNDEFINED and @app @layout end |
.layout!(layout) ⇒ Object
30 31 32 |
# File 'lib/zorglub/node.rb', line 30 def layout! layout @layout = layout end |
.layout_base_path ⇒ Object
48 49 50 |
# File 'lib/zorglub/node.rb', line 48 def layout_base_path @layout_base_path ||= @app.layout_base_path end |
.layout_base_path!(path) ⇒ Object
44 45 46 |
# File 'lib/zorglub/node.rb', line 44 def layout_base_path! path @layout_base_path = path end |
.map(app, location) ⇒ Object
112 113 114 115 |
# File 'lib/zorglub/node.rb', line 112 def map app, location @app = app @app.map location, self end |
.no_layout! ⇒ Object
26 27 28 |
# File 'lib/zorglub/node.rb', line 26 def no_layout! layout! nil end |
.partial(env, meth, *args) ⇒ Object
231 232 233 234 235 236 |
# File 'lib/zorglub/node.rb', line 231 def partial env, meth, *args node = self.new env, meth.to_s, args, true return error_404 node if not node.respond_to? meth node.feed! env[:no_hooks] node.content end |
.r(*args) ⇒ Object
117 118 119 120 |
# File 'lib/zorglub/node.rb', line 117 def r *args @r ||= @app.to self (args.empty? ? @r : File.join( @r, args.map { |x| x.to_s } ) ) end |
.static!(val, lifetime = 0) ⇒ Object
39 40 41 42 |
# File 'lib/zorglub/node.rb', line 39 def static! val, lifetime=0 @static = ( (val==true or val==false) ? val : false ) @cache_lifetime = lifetime end |
.view_base_path ⇒ Object
56 57 58 |
# File 'lib/zorglub/node.rb', line 56 def view_base_path @view_base_path ||= @app.view_base_path end |
.view_base_path!(path) ⇒ Object
52 53 54 |
# File 'lib/zorglub/node.rb', line 52 def view_base_path! path @view_base_path = path end |
Instance Method Details
#app ⇒ Object
instance level basic node functions
126 127 128 |
# File 'lib/zorglub/node.rb', line 126 def app self.class.app end |
#cli_val(sym, *args) ⇒ Object
176 177 178 179 180 181 182 183 |
# File 'lib/zorglub/node.rb', line 176 def cli_val sym, *args vals = @cli_vals[sym] ||= [] unless args.empty? vals.concat args vals.uniq! end vals end |
#compile_page! ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/zorglub/node.rb', line 308 def compile_page! e, @ext = app.engine_proc_ext @engine, @ext v, l = view, layout if @debug $stderr << " * "+(e ? 'use engine' : 'no engine ')+" : "+(e ? e.to_s : '')+"\n" $stderr << " * "+((l and File.exists?(l)) ? 'use layout' : 'no layout ')+" : "+(l ? l : '')+"\n" $stderr << " * "+((v and File.exists?(v)) ? 'use view ' : 'no view ')+" : "+(v ? v : '')+"\n" end @state = ( @partial ? :partial : :view ) @content, mime = e.call v, self if e and v and File.exists? v @mime = mime unless mime.nil? @state = :layout @content, mime = e.call l, self if e and l and File.exists? l @mime = mime unless mime.nil? end |
#engine!(engine) ⇒ Object
instance level engine, layout, view, static configuration
63 64 65 |
# File 'lib/zorglub/node.rb', line 63 def engine! engine @engine = engine end |
#ext ⇒ Object
103 104 105 |
# File 'lib/zorglub/node.rb', line 103 def ext @ext || '' end |
#ext!(ext) ⇒ Object
99 100 101 |
# File 'lib/zorglub/node.rb', line 99 def ext! ext @ext = ( (ext.nil? or ext.empty?) ? nil : (ext[0]=='.' ? (ext.length==1 ? nil : ext) : '.'+ext) ) end |
#feed!(no_hooks = false) ⇒ Object
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/zorglub/node.rb', line 277 def feed! no_hooks=false @state = :pre_cb self.class.call_before_hooks self unless no_hooks @state = :meth @content = self.send @meth, *@args static_path = static if static_path.nil? compile_page! else static_page! static_path end @state = :post_cb self.class.call_after_hooks self unless no_hooks @state = :finished return @content, @mime end |
#html ⇒ Object
138 139 140 |
# File 'lib/zorglub/node.rb', line 138 def html [ :map, :r, :args, :engine, :layout, :view ].inject('') { |s,sym| s+="<p>#{sym} => #{self.send sym}</p>"; s } end |
#layout ⇒ Object
75 76 77 78 |
# File 'lib/zorglub/node.rb', line 75 def layout return nil if @layout.nil? File.join(self.class.layout_base_path, @layout)+ext end |
#layout!(layout) ⇒ Object
71 72 73 |
# File 'lib/zorglub/node.rb', line 71 def layout! layout @layout = layout end |
#map ⇒ Object
130 131 132 |
# File 'lib/zorglub/node.rb', line 130 def map self.class.r end |
#no_layout! ⇒ Object
67 68 69 |
# File 'lib/zorglub/node.rb', line 67 def no_layout! layout! nil end |
#r(*args) ⇒ Object
134 135 136 |
# File 'lib/zorglub/node.rb', line 134 def r *args File.join map, (args.empty? ? meth : args.map { |x| x.to_s } ) end |
#realize! ⇒ Object
267 268 269 270 271 272 273 274 275 |
# File 'lib/zorglub/node.rb', line 267 def realize! catch(:stop_realize) { feed! response.write @content response.header['Content-Type'] = ( @mime || 'text/html' ) response.finish response } end |
#redirect(target, options = {}, &block) ⇒ Object
142 143 144 145 146 147 |
# File 'lib/zorglub/node.rb', line 142 def redirect target, ={}, &block status = [:status] || 302 body = [:body] || redirect_body(target) header = response.header.merge('Location' => target.to_s) throw :stop_realize, Rack::Response.new(body, status, header, &block) end |
#redirect_body(target) ⇒ Object
149 150 151 |
# File 'lib/zorglub/node.rb', line 149 def redirect_body target "You are being redirected, please follow this link to: <a href='#{target}'>#{target}</a>!" end |
#session ⇒ Object
15 16 17 |
# File 'lib/zorglub/session.rb', line 15 def session @session ||= SessionHash.new @request, @response, Node.sessions, app.opt(:session_options) end |
#static ⇒ Object
94 95 96 97 |
# File 'lib/zorglub/node.rb', line 94 def static return nil if not @static or @view.nil? File.join(app.static_base_path, @view)+ext end |
#static!(val, lifetime = 0) ⇒ Object
89 90 91 92 |
# File 'lib/zorglub/node.rb', line 89 def static! val, lifetime=0 @static = ( (val==true or val==false) ? val : false ) @cache_lifetime = lifetime end |
#static_page!(path) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/zorglub/node.rb', line 294 def static_page! path if File.exists?(path) and ( @cache_lifetime.nil? or @cache_lifetime==0 or ( (Time.now-File.stat(path).mtime) < @cache_lifetime ) ) $stderr << " * use cache file : #{path}\n" if @debug content = File.open(path, 'r') {|f| f.read } @content = content.sub /^@mime:(.*)\n/,'' @mime = $1 else compile_page! FileUtils.mkdir_p File.dirname(path) File.open(path, 'w') {|f| f.write("@mime:"+@mime+"\n"); f.write(@content); } $stderr << " * cache file created : #{path}\n" if @debug end end |
#view ⇒ Object
84 85 86 87 |
# File 'lib/zorglub/node.rb', line 84 def view return nil if @view.nil? File.join(self.class.view_base_path, @view)+ext end |
#view!(view) ⇒ Object
80 81 82 |
# File 'lib/zorglub/node.rb', line 80 def view! view @view = view end |