Class: Zorglub::Node

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, options) ⇒ Node

Returns a new instance of Node.



253
254
255
256
257
258
# File 'lib/zorglub/node.rb', line 253

def initialize env, options
    @env = env
    @options = options
    @request = Rack::Request.new env
    @response = Rack::Response.new
end

Class Attribute Details

.appObject

Returns the value of attribute app.



111
112
113
# File 'lib/zorglub/node.rb', line 111

def app
  @app
end

.hooksObject (readonly)

Returns the value of attribute hooks.



191
192
193
# File 'lib/zorglub/node.rb', line 191

def hooks
  @hooks
end

.inherited_varsObject (readonly)

Returns the value of attribute inherited_vars.



163
164
165
# File 'lib/zorglub/node.rb', line 163

def inherited_vars
  @inherited_vars
end

.sessionsObject (readonly)

Returns the value of attribute sessions.



12
13
14
# File 'lib/zorglub/session.rb', line 12

def sessions
  @sessions
end

.staticObject (readonly)

Returns the value of attribute static.



13
14
15
# File 'lib/zorglub/node.rb', line 13

def static
  @static
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



251
252
253
# File 'lib/zorglub/node.rb', line 251

def content
  @content
end

#mimeObject (readonly)

Returns the value of attribute mime.



251
252
253
# File 'lib/zorglub/node.rb', line 251

def mime
  @mime
end

#optionsObject (readonly)

Returns the value of attribute options.



251
252
253
# File 'lib/zorglub/node.rb', line 251

def options
  @options
end

#requestObject (readonly)

Returns the value of attribute request.



251
252
253
# File 'lib/zorglub/node.rb', line 251

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



251
252
253
# File 'lib/zorglub/node.rb', line 251

def response
  @response
end

Class Method Details

.after_all(&blk) ⇒ Object



206
207
208
209
# File 'lib/zorglub/node.rb', line 206

def after_all &blk
    @inherited_vars[:after_all]<< blk
    @inherited_vars[:after_all].uniq!
end

.before_all(&blk) ⇒ Object



197
198
199
200
# File 'lib/zorglub/node.rb', line 197

def before_all &blk
    @inherited_vars[:before_all]<< blk
    @inherited_vars[:before_all].uniq!
end

.call(env) ⇒ Object



224
225
226
227
228
229
230
231
# File 'lib/zorglub/node.rb', line 224

def call env
    meth, *args =  env['PATH_INFO'].sub(/^\//,'').split '/'
    meth||= 'index'
    puts "=> #{meth}(#{args.join ','})" if app.opt :debug
    node = self.new env, {:engine=>engine,:layout=>layout,:view=>r(meth),:method=>meth,:args=>args,:static=>static}
    return error_404 node if not node.respond_to? meth
    node.realize!
end

.call_after_hooks(obj) ⇒ Object



202
203
204
# File 'lib/zorglub/node.rb', line 202

def call_after_hooks obj
    @inherited_vars[:after_all].each do |blk| blk.call obj end
end

.call_before_hooks(obj) ⇒ Object



193
194
195
# File 'lib/zorglub/node.rb', line 193

def call_before_hooks obj
    @inherited_vars[:before_all].each do |blk| blk.call obj end
end

.engineObject



19
20
21
22
# File 'lib/zorglub/node.rb', line 19

def engine
    @engine = @app.opt(:engine) if @engine==UNDEFINED and @app
    @engine
end

.engine!(engine) ⇒ Object



15
16
17
# File 'lib/zorglub/node.rb', line 15

def engine! engine
    @engine = engine
end

.error_404(node) ⇒ Object



240
241
242
243
244
245
246
247
# File 'lib/zorglub/node.rb', line 240

def error_404 node
    puts " !! method not found" 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.r, node.request.env['PATH_INFO'] ]
    resp
end

.inherited(sub) ⇒ Object



217
218
219
220
221
222
# File 'lib/zorglub/node.rb', line 217

def inherited sub
    sub.engine! engine||(self==Zorglub::Node ? UNDEFINED : nil )
    sub.layout! layout||(self==Zorglub::Node ? UNDEFINED : nil )
    sub.instance_variable_set :@inherited_vars, {}
    @inherited_vars.each do |s,v| sub.inherited_var s, *v end
end

.inherited_var(sym, *args) ⇒ Object



165
166
167
168
169
170
171
172
# File 'lib/zorglub/node.rb', line 165

def inherited_var sym, *args
    var = @inherited_vars[sym] ||=[]
    unless args.empty?
        var.concat args
        var.uniq!
    end
    var
end

.layoutObject



32
33
34
35
# File 'lib/zorglub/node.rb', line 32

def layout
    @layout = @app.opt(:layout) if @layout==UNDEFINED and @app
    @layout
end

.layout!(layout) ⇒ Object



28
29
30
# File 'lib/zorglub/node.rb', line 28

def layout! layout
    @layout = layout
end

.layout_base_pathObject



45
46
47
# File 'lib/zorglub/node.rb', line 45

def layout_base_path
    @layout_base_path ||= @app.layout_base_path
end

.layout_base_path!(path) ⇒ Object



41
42
43
# File 'lib/zorglub/node.rb', line 41

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



24
25
26
# File 'lib/zorglub/node.rb', line 24

def no_layout!
    layout! nil
end

.partial(meth, *args) ⇒ Object



233
234
235
236
237
238
# File 'lib/zorglub/node.rb', line 233

def partial meth, *args
    node = self.new nil, {:engine=>engine,:layout=>nil,:view=>r(meth),:method=>meth.to_s,:args=>args,:static=>static}
    return error_404 node if not node.respond_to? meth
    node.feed!
    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) ⇒ Object



37
38
39
# File 'lib/zorglub/node.rb', line 37

def static! val
    @static = ( (val==true or val==false) ? val : false )
end

.view_base_pathObject



53
54
55
# File 'lib/zorglub/node.rb', line 53

def view_base_path
    @view_base_path ||= @app.view_base_path
end

.view_base_path!(path) ⇒ Object



49
50
51
# File 'lib/zorglub/node.rb', line 49

def view_base_path! path
    @view_base_path = path
end

Instance Method Details

#appObject

instance level basic node functions



126
127
128
# File 'lib/zorglub/node.rb', line 126

def app
    self.class.app
end

#argsObject



130
131
132
# File 'lib/zorglub/node.rb', line 130

def args
    @options[:args]
end

#compile_page!Object



307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/zorglub/node.rb', line 307

def compile_page!
    e, @options[:ext] = app.engine_proc_ext @options[:engine], @options[:ext]
    v, l, debug = view, layout, app.opt(:debug)
    puts " * "+(File.exists?(l) ? 'use layout' : 'not found layout')+" : "+l if debug
    puts " * "+(File.exists?(v) ? 'use view  ' : 'not found view  ')+" : "+v if debug
    state (@options[:layout].nil? ? :partial : :view)
    @content, mime = e.call v, self if e and File.exists? v
    @mime = mime unless mime.nil?
    state :layout
    @content, mime = e.call l, self if e and File.exists? l
    @mime = mime unless mime.nil?
end

#engineObject



64
65
66
# File 'lib/zorglub/node.rb', line 64

def engine
    @options[:engine]
end

#engine!(engine) ⇒ Object

instance level engine, layout, view, static configuration



60
61
62
# File 'lib/zorglub/node.rb', line 60

def engine! engine
    @options[:engine] = engine
end

#extObject



103
104
105
# File 'lib/zorglub/node.rb', line 103

def ext
    @options[:ext]||''
end

#ext!(ext) ⇒ Object



99
100
101
# File 'lib/zorglub/node.rb', line 99

def ext! ext
    @options[:ext]= ( (ext.nil? or ext.empty?) ? nil : (ext[0]=='.' ? (ext.length==1 ? nil : ext) : '.'+ext) )
end

#feed!Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/zorglub/node.rb', line 275

def feed!
    state :pre_cb
    self.class.call_before_hooks self
    state :meth
    @content = self.send @options[:method], *@options[: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
    state :finished
    return @content, @mime
end

#htmlObject



142
143
144
# File 'lib/zorglub/node.rb', line 142

def html
    [ :map, :r, :args, :engine, :layout, :view ].inject('') { |s,sym| s+="<p>#{sym} => #{self.send sym}</p>"; s }
end

#inherited_var(sym, *args) ⇒ Object



176
177
178
179
180
181
182
183
# File 'lib/zorglub/node.rb', line 176

def inherited_var sym, *args
    d = self.class.inherited_vars[sym].clone || []
    unless args.empty?
        d.concat args
        d.uniq!
    end
    d
end

#layoutObject



76
77
78
79
# File 'lib/zorglub/node.rb', line 76

def layout
    return '' if @options[:layout].nil?
    File.join(self.class.layout_base_path, @options[:layout])+ext
end

#layout!(layout) ⇒ Object



72
73
74
# File 'lib/zorglub/node.rb', line 72

def layout! layout
    @options[:layout] = layout
end

#mapObject



134
135
136
# File 'lib/zorglub/node.rb', line 134

def map
    self.class.r
end

#no_layout!Object



68
69
70
# File 'lib/zorglub/node.rb', line 68

def no_layout!
    layout! nil
end

#r(*args) ⇒ Object



138
139
140
# File 'lib/zorglub/node.rb', line 138

def r *args
    File.join map, (args.empty? ? @options[:method] : args.map { |x| x.to_s } )
end

#realize!Object



265
266
267
268
269
270
271
272
273
# File 'lib/zorglub/node.rb', line 265

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



146
147
148
149
150
151
# File 'lib/zorglub/node.rb', line 146

def redirect target, options={}, &block
    status = options[:status] || 302
    body   = options[: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



153
154
155
# File 'lib/zorglub/node.rb', line 153

def redirect_body target
    "You are being redirected, please follow this link to: <a href='#{target}'>#{target}</a>!"
end

#sessionObject



15
16
17
# File 'lib/zorglub/session.rb', line 15

def session
    @session ||= SessionHash.new @request, @response, Node.sessions, app.opt(:session_options)
end

#state(state = nil) ⇒ Object



260
261
262
263
# File 'lib/zorglub/node.rb', line 260

def state state=nil
    @options[:state] = state unless state.nil?
    @options[:state]
end

#staticObject



94
95
96
97
# File 'lib/zorglub/node.rb', line 94

def static
    return nil if not @options[:static] or @options[:view].nil?
    File.join(app.static_base_path, @options[:view])+ext
end

#static!(val) ⇒ Object



90
91
92
# File 'lib/zorglub/node.rb', line 90

def static! val
    @options[:static] = ((val==true or val==false) ? val : false )
end

#static_page!(path) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/zorglub/node.rb', line 292

def static_page! path
    if not File.exists? path
        compile_page!
        Dir.mkdir app.static_base_path
        Dir.mkdir File.dirname path
        File.open(path, 'w') {|f| f.write("@mime:"+@mime+"\n"); f.write(@content); }
        puts " * cache file created : #{path}" if app.opt :debug
    else
        puts " * use cache file : #{path}" if app.opt :debug
        content = File.open(path, 'r') {|f| f.read }
        @content = content.sub /^@mime:(.*)\n/,''
        @mime = $1
    end
end

#viewObject



85
86
87
88
# File 'lib/zorglub/node.rb', line 85

def view
    return '' if @options[:view].nil?
    File.join(self.class.view_base_path, @options[:view])+ext
end

#view!(view) ⇒ Object



81
82
83
# File 'lib/zorglub/node.rb', line 81

def view! view
    @options[:view] = view
end