summaryrefslogtreecommitdiffstats
path: root/example/sample.ru
blob: 9dc4d132728d8b5dfb4266a8c6b168e23ffbce6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#! /usr/bin/env ruby
#
$LOAD_PATH << File.join(File.dirname( File.absolute_path(__FILE__)), '..', 'lib')
#
USE_RACK_SESSION=false
#
require 'zorglub'
require 'zorglub/engines/haml'
if USE_RACK_SESSION
    require 'zorglub/rack_session'
else
    require 'zorglub/session'
end
#
class Zorglub::Node
    @count=0
    class << self
        attr_accessor :count
    end
    before_all do |node|
        Zorglub::Node.count +=1
    end
end
#
class Node1 < Zorglub::Node
    #
    def index a1, *a2
        @title='Index'
        @links = LINKS
        # there's a view so the below will be lost !
        "<b>should never be seeen</b>"
    end
    #
    def meth0 *args
        @title='meth0'
        @links = LINKS
        # method level engine
        engine! 'tmp-engine'
        # there's a view so the below will be lost !
        "<b>should never be seeen</b>"
    end
    #
    def meth1 *args
        @title='meth1'
        @links = LINKS
        # method level engine (layout/other.haml)
        layout! 'other'
        # specific method view (view/url1/meth0.haml)
        view! File.join( 'url1','meth0')
        # there's a view so the below will be lost !
        "<b>should never be seeen</b>"
    end
    #
    def jump *args
        redirect r(:index,1,2,3)
    end
    #
end
#
HAML_PROC = Proc.new { |path,obj| Haml::Engine.new( File.open(path,'r').read ).render(obj) }
#
APP = Zorglub::App.new do
    register_engine! :haml, 'haml', Zorglub::Engines::Haml.method(:proc)
    register_engine! 'tmp-engine', 'haml', HAML_PROC
    opt! :debug, true
    opt! :engine, :haml
    opt!:root, File.dirname( File.absolute_path(__FILE__) )
    opt(:session_options)[:enabled] = true
    map '/url1', Node1
end
#
class Node2 < Zorglub::Node
    #
    map APP, '/url2'
    layout! 'css'
    # class level engine
    engine! 'tmp-engine'
    # class level css
    inherited_var :css, 'class_level.css'
    #
    def index *args
        "<title>Node2:index</title><b>START</b>#{html}<a href=#{Node2.r(:meth0)}>next</a><br/><b>END</b>"
    end
    #
    def meth0 *args
        # instance level css
        inherited_var :css, 'instance_level.css'
        "<title>Node2:meth0</title><b>START</b>#{html}<a href=#{Node2.r(:meth1,1,2)}>next</a><br/><b>END</b>"
    end
    #
    def meth1 *args
        more = Node2.partial :meth0, *args
        "<title>Node2:meth1</title><b>partial</b><br/>#{more}<br/><b>done</b><br/><a href=#{Node0.r}>back</a>"
    end
end
#
class Node3 < Zorglub::Node
    #
    map APP, '/url3'
    no_layout!
    #
    def index *args
        @title = "Session tests"
        t = Time.now
        if session[:now].nil?
            session[:now] = t
            @data = "#{t.strftime('%H:%M:%S')} FIRST"
        elsif t-session[:now]>10
            session[:now] = t
            @data = "#{t.strftime('%H:%M:%S')} UPDATE"
        else
            @data = "#{session[:now].strftime('%H:%M:%S')} CURRENT"
        end
    end
    def reset
        session.clear
        redirect :index
    end
    #
end
#
class Node0 < Zorglub::Node
    #
    map APP, '/'
    #
    def index
        html = "<html><body><ul>"
        html << "<li><a href=\"#{Node1.r('index','a',2,'c')}\">Node1</a> engine, layout, view, redirect tests</li>"
        html << "<li><a href=\"#{Node2.r}\">Node2</a> css helper tests</li>"
        html << "<li><a href=\"#{Node3.r}\">Node3</a> session test</li>"
        html << "</ul></body></html>"
        html
    end
    #
end
#
Node1::LINKS= [
            [Node1.r('index','arg1','arg2','arg3'),'index'],
            [Node1.r('meth0'),'meth0'],
            [Node1.r('meth1','one','two'),'meth1 with args'],
            [Node1.r('jump','one','two'),'test redirect'],
            [Node0.r,'back'],
]
#
puts APP.to_hash.inspect
puts "  **** "+( USE_RACK_SESSION ? 'USE Rack Session' : 'USE builtin Session' )
#
map '/' do
    use Rack::Lint
    use Rack::ShowExceptions
    if USE_RACK_SESSION
        use Rack::Session::Cookie,  :key=>APP.opt(:session_options)[:key], :secret=>APP.opt(:session_options)[:secret],
            :path=>'/', :http_only=>true, :expire_after=>30
    end
    run APP
end
#