zorglub 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog CHANGED
@@ -16,4 +16,11 @@
16
16
  * optional engine cache
17
17
  * optional engine proc mime-type definition
18
18
  * optional static page generation/cache
19
- *
19
+
20
+ 2012-01-17 Jérémy Zurcher <jeremy@asynk.ch>
21
+ * Node code cleanup
22
+ * class and method level directives are now view!, layout!, no_layout!, ...
23
+ * add view_base_path! and layout_base_path!
24
+ * before_all and after_all are based on inherited_vars
25
+ * add Zorglub::Config.debug
26
+ * Zorglub::App swallows Zorglub::Config
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
1
  # -*- coding: UTF-8 -*-
2
2
  #
3
- require './lib/zorglub.rb'
3
+ $LOAD_PATH << 'lib'
4
+ #
5
+ require 'zorglub'
4
6
  load './tasks/setup.rb'
5
7
  #
6
8
  # Project general information
data/lib/zorglub/app.rb CHANGED
@@ -9,10 +9,33 @@ module Zorglub
9
9
  def initialize map={}, &block
10
10
  super
11
11
  @map = map
12
+ @engines_cache = { }
13
+ @options = {
14
+ :debug => false,
15
+ :root => '.',
16
+ :layout => 'default',
17
+ :view_dir => 'view',
18
+ :layout_dir => 'layout',
19
+ :static_dir => 'static',
20
+ :engine => nil,
21
+ :engines_cache_enabled => true,
22
+ :engines => { },
23
+ :haml_options => {
24
+ :format => :html5,
25
+ :ugly => false,
26
+ :encoding => 'utf-8'
27
+ },
28
+ :session_options => {
29
+ :session_on => false,
30
+ :session_key => 'zorglub.sid',
31
+ :session_secret => 'session-secret-secret',
32
+ :session_sid_len => 64,
33
+ }
34
+ }
12
35
  instance_eval &block if block_given?
13
36
  remap @map
14
- @engines_cache = { }
15
37
  end
38
+ #
16
39
  attr_reader :engines_cache
17
40
  #
18
41
  def map location, object
@@ -40,6 +63,47 @@ module Zorglub
40
63
  @map.dup
41
64
  end
42
65
  #
66
+ # OPTIONS @options
67
+ #
68
+ def opt sym
69
+ @options[sym]
70
+ end
71
+ #
72
+ def opt! sym, val
73
+ @options[sym] = val
74
+ end
75
+ #
76
+ def register_engine! name, ext, proc
77
+ return unless name
78
+ if ext.nil? or ext.empty?
79
+ x = nil
80
+ else
81
+ x = (ext[0]=='.' ? (ext.length==1 ? nil : ext) : '.'+ext)
82
+ end
83
+ @options[:engines][name]=[ proc, x ]
84
+ end
85
+ #
86
+ def engine_proc_ext engine, ext
87
+ p,x = @options[:engines][engine]
88
+ return [nil, ''] if p.nil?
89
+ [ p, ((ext.nil? or ext.empty?) ? x : ext ) ]
90
+ end
91
+ #
92
+ def view_base_path
93
+ p = @options[:view_path]
94
+ ( p.nil? ? File.join(@options[:root], @options[:view_dir]) : p )
95
+ end
96
+ #
97
+ def layout_base_path
98
+ p = @options[:layout_path]
99
+ ( p.nil? ? File.join(@options[:root], @options[:layout_dir]) : p )
100
+ end
101
+ #
102
+ def static_base_path
103
+ p = @options[:static_path]
104
+ ( p.nil? ? File.join(@options[:root], @options[:static_dir]) : p )
105
+ end
106
+ #
43
107
  end
44
108
  #
45
109
  end
@@ -15,6 +15,4 @@ module Zorglub
15
15
  end
16
16
  end
17
17
  #
18
- Zorglub::Config.register_engine :file, nil, Zorglub::Engines::File.method(:proc)
19
- #
20
18
  # EOF
@@ -7,11 +7,11 @@ module Zorglub
7
7
  module Engines
8
8
  module Haml
9
9
  def self.proc path,obj
10
- if Zorglub::Config.engines_cache_enabled
11
- key = path.sub Zorglub::Config.root,''
12
- haml = obj.app.engines_cache[key] ||= ::Haml::Engine.new( File.open(path,'r'){|f| f.read }, Zorglub::Config.haml_options )
10
+ if obj.app.opt(:engines_cache_enabled)
11
+ key = path.sub obj.app.opt(:root),''
12
+ haml = obj.app.engines_cache[key] ||= ::Haml::Engine.new( ::File.open(path,'r'){|f| f.read }, obj.app.opt(:haml_options) )
13
13
  else
14
- haml = ::Haml::Engine.new( File.open(path,'r'){|f| f.read }, Zorglub::Config.haml_options )
14
+ haml = ::Haml::Engine.new( ::File.open(path,'r'){|f| f.read }, obj.app.opt(:haml_options) )
15
15
  end
16
16
  html = haml.render(obj)
17
17
  return html, 'text/html'
@@ -20,6 +20,4 @@ module Zorglub
20
20
  end
21
21
  end
22
22
  #
23
- Zorglub::Config.register_engine :haml, 'haml', Zorglub::Engines::Haml.method(:proc)
24
- #
25
23
  # EOF
data/lib/zorglub/node.rb CHANGED
@@ -4,47 +4,109 @@ module Zorglub
4
4
  #
5
5
  class Node
6
6
  #
7
- @hooks = {
8
- :before_all => [],
9
- :after_all => [],
10
- }
7
+ UNDEFINED=-1
11
8
  #
12
- @inherited_vars = { }
9
+ # class level engine, layout, static, layout_base_path, view_base_path configuration
13
10
  #
14
11
  class << self
15
12
  #
16
- attr_reader :hooks, :inherited_vars
13
+ attr_reader :static
17
14
  #
18
- def inherited sub
19
- sub.layout layout
20
- sub.engine engine
21
- sub.instance_variable_set :@inherited_vars, {}
22
- @inherited_vars.each do |s,v| sub.inherited_var s, *v end
15
+ def engine! engine
16
+ @engine = engine
23
17
  end
24
18
  #
25
- def engine engine=nil
26
- @engine = engine unless engine.nil? or engine.empty?
27
- @engine ||= Config.engine
19
+ def engine
20
+ @engine = @app.opt(:engine) if @engine==UNDEFINED and @app
21
+ @engine
28
22
  end
29
23
  #
30
- def layout layout=nil
31
- @layout = layout unless layout.nil? or layout.empty?
32
- @layout ||= Config.layout
24
+ def no_layout!
25
+ layout! nil
33
26
  end
34
27
  #
35
- def static val=nil
36
- @static = val if (val==true or val==false)
37
- @static ||=false
28
+ def layout! layout
29
+ @layout = layout
38
30
  end
39
31
  #
40
- def inherited_var sym, *args
41
- var = @inherited_vars[sym] ||=[]
42
- unless args.empty?
43
- var.concat args
44
- var.uniq!
45
- end
46
- var
32
+ def layout
33
+ @layout = @app.opt(:layout) if @layout==UNDEFINED and @app
34
+ @layout
47
35
  end
36
+ #
37
+ def static! val
38
+ @static = ( (val==true or val==false) ? val : false )
39
+ end
40
+ #
41
+ def layout_base_path! path
42
+ @layout_base_path = path
43
+ end
44
+ #
45
+ def layout_base_path
46
+ @layout_base_path ||= @app.layout_base_path
47
+ end
48
+ #
49
+ def view_base_path! path
50
+ @view_base_path = path
51
+ end
52
+ #
53
+ def view_base_path
54
+ @view_base_path ||= @app.view_base_path
55
+ end
56
+ end
57
+ #
58
+ # instance level engine, layout, view, static configuration
59
+ #
60
+ def engine! engine
61
+ @options[:engine] = engine
62
+ end
63
+ #
64
+ def engine
65
+ @options[:engine]
66
+ end
67
+ #
68
+ def no_layout!
69
+ layout! nil
70
+ end
71
+ #
72
+ def layout! layout
73
+ @options[:layout] = layout
74
+ end
75
+ #
76
+ def layout
77
+ return '' if @options[:layout].nil?
78
+ File.join(self.class.layout_base_path, @options[:layout])+ext
79
+ end
80
+ #
81
+ def view! view
82
+ @options[:view] = view
83
+ end
84
+ #
85
+ def view
86
+ return '' if @options[:view].nil?
87
+ File.join(self.class.view_base_path, @options[:view])+ext
88
+ end
89
+ #
90
+ def static! val
91
+ @options[:static] = ((val==true or val==false) ? val : false )
92
+ end
93
+ #
94
+ def static
95
+ return nil if not @options[:static] or @options[:view].nil?
96
+ File.join(app.static_base_path, @options[:view])+ext
97
+ end
98
+ #
99
+ def ext! ext
100
+ @options[:ext]= ( (ext.nil? or ext.empty?) ? nil : (ext[0]=='.' ? (ext.length==1 ? nil : ext) : '.'+ext) )
101
+ end
102
+ #
103
+ def ext
104
+ @options[:ext]||''
105
+ end
106
+ #
107
+ # class level basic node functions
108
+ #
109
+ class << self
48
110
  #
49
111
  attr_accessor :app
50
112
  def map app, location
@@ -57,40 +119,126 @@ module Zorglub
57
119
  (args.empty? ? @r : File.join( @r, args.map { |x| x.to_s } ) )
58
120
  end
59
121
  #
60
- def call env
61
- meth, *args = env['PATH_INFO'].sub(/^\//,'').split '/'
62
- meth||= 'index'
63
- node = self.new env, {:engine=>engine,:layout=>layout,:view=>r(meth),:method=>meth,:args=>args,:static=>static}
64
- return error_404 node if not node.respond_to? meth
65
- node.realize!
122
+ end
123
+ #
124
+ # instance level basic node functions
125
+ #
126
+ def app
127
+ self.class.app
128
+ end
129
+ #
130
+ def args
131
+ @options[:args]
132
+ end
133
+ #
134
+ def map
135
+ self.class.r
136
+ end
137
+ #
138
+ def r *args
139
+ File.join map, (args.empty? ? @options[:method] : args.map { |x| x.to_s } )
140
+ end
141
+ #
142
+ def html
143
+ [ :map, :r, :args, :engine, :layout, :view ].inject('') { |s,sym| s+="<p>#{sym} => #{self.send sym}</p>"; s }
144
+ end
145
+ #
146
+ def redirect target, options={}, &block
147
+ status = options[:status] || 302
148
+ body = options[:body] || redirect_body(target)
149
+ header = response.header.merge('Location' => target.to_s)
150
+ throw :stop_realize, Rack::Response.new(body, status, header, &block)
151
+ end
152
+ #
153
+ def redirect_body target
154
+ "You are being redirected, please follow this link to: <a href='#{target}'>#{target}</a>!"
155
+ end
156
+ #
157
+ # inherited vars, they can be modified at class level only
158
+ #
159
+ @inherited_vars = { }
160
+ #
161
+ class << self
162
+ #
163
+ attr_reader :inherited_vars
164
+ #
165
+ def inherited_var sym, *args
166
+ var = @inherited_vars[sym] ||=[]
167
+ unless args.empty?
168
+ var.concat args
169
+ var.uniq!
170
+ end
171
+ var
66
172
  end
67
173
  #
68
- def partial meth, *args
69
- node = self.new nil, {:engine=>engine,:layout=>nil,:view=>r(meth),:method=>meth.to_s,:args=>args,:static=>static}
70
- return error_404 node if not node.respond_to? meth
71
- node.feed!
72
- node.content
174
+ end
175
+ #
176
+ def inherited_var sym, *args
177
+ d = self.class.inherited_vars[sym].clone || []
178
+ unless args.empty?
179
+ d.concat args
180
+ d.uniq!
73
181
  end
182
+ d
183
+ end
184
+ #
185
+ # before_all and after_all hooks
186
+ #
187
+ @inherited_vars[:before_all] = []
188
+ @inherited_vars[:after_all] = []
189
+ class << self
190
+ #
191
+ attr_reader :hooks
74
192
  #
75
193
  def call_before_hooks obj
76
- Node.hooks[:before_all].each do |blk| blk.call obj end
194
+ @inherited_vars[:before_all].each do |blk| blk.call obj end
77
195
  end
78
196
  #
79
197
  def before_all &blk
80
- Node.hooks[:before_all]<< blk
81
- Node.hooks[:before_all].uniq!
198
+ @inherited_vars[:before_all]<< blk
199
+ @inherited_vars[:before_all].uniq!
82
200
  end
83
201
  #
84
202
  def call_after_hooks obj
85
- Node.hooks[:after_all].each do |blk| blk.call obj end
203
+ @inherited_vars[:after_all].each do |blk| blk.call obj end
86
204
  end
87
205
  #
88
206
  def after_all &blk
89
- Node.hooks[:after_all]<< blk
90
- Node.hooks[:after_all].uniq!
207
+ @inherited_vars[:after_all]<< blk
208
+ @inherited_vars[:after_all].uniq!
209
+ end
210
+ #
211
+ end
212
+ #
213
+ # rack entry point, page computation methods
214
+ #
215
+ class << self
216
+ #
217
+ def inherited sub
218
+ sub.engine! engine||(self==Zorglub::Node ? UNDEFINED : nil )
219
+ sub.layout! layout||(self==Zorglub::Node ? UNDEFINED : nil )
220
+ sub.instance_variable_set :@inherited_vars, {}
221
+ @inherited_vars.each do |s,v| sub.inherited_var s, *v end
222
+ end
223
+ #
224
+ def call env
225
+ meth, *args = env['PATH_INFO'].sub(/^\//,'').split '/'
226
+ meth||= 'index'
227
+ puts "=> #{meth}(#{args.join ','})" if app.opt :debug
228
+ node = self.new env, {:engine=>engine,:layout=>layout,:view=>r(meth),:method=>meth,:args=>args,:static=>static}
229
+ return error_404 node if not node.respond_to? meth
230
+ node.realize!
231
+ end
232
+ #
233
+ def partial meth, *args
234
+ node = self.new nil, {:engine=>engine,:layout=>nil,:view=>r(meth),:method=>meth.to_s,:args=>args,:static=>static}
235
+ return error_404 node if not node.respond_to? meth
236
+ node.feed!
237
+ node.content
91
238
  end
92
239
  #
93
240
  def error_404 node
241
+ puts " !! method not found" if app.opt :debug
94
242
  resp = node.response
95
243
  resp.status = 404
96
244
  resp['Content-Type'] = 'text/plain'
@@ -109,6 +257,11 @@ module Zorglub
109
257
  @response = Rack::Response.new
110
258
  end
111
259
  #
260
+ def state state=nil
261
+ @options[:state] = state unless state.nil?
262
+ @options[:state]
263
+ end
264
+ #
112
265
  def realize!
113
266
  catch(:stop_realize) {
114
267
  feed!
@@ -121,37 +274,41 @@ module Zorglub
121
274
  #
122
275
  def feed!
123
276
  state :pre_cb
124
- Node.call_before_hooks self
277
+ self.class.call_before_hooks self
125
278
  state :meth
126
279
  @content = self.send @options[:method], *@options[:args]
127
280
  static_path = static
128
281
  if static_path.nil?
129
- compile!
282
+ compile_page!
130
283
  else
131
- static! static_path
284
+ static_page! static_path
132
285
  end
133
286
  state :post_cb
134
- Node.call_after_hooks self
287
+ self.class.call_after_hooks self
135
288
  state :finished
136
289
  return @content, @mime
137
290
  end
138
291
  #
139
- def static! path
292
+ def static_page! path
140
293
  if not File.exists? path
141
- compile!
142
- Dir.mkdir Config.static_base_path
294
+ compile_page!
295
+ Dir.mkdir app.static_base_path
143
296
  Dir.mkdir File.dirname path
144
- File.open(path, 'w') {|f| f.write(@content); f.write("\n@mime:"+@mime) }
297
+ File.open(path, 'w') {|f| f.write("@mime:"+@mime+"\n"); f.write(@content); }
298
+ puts " * cache file created : #{path}" if app.opt :debug
145
299
  else
300
+ puts " * use cache file : #{path}" if app.opt :debug
146
301
  content = File.open(path, 'r') {|f| f.read }
147
- @content = content.sub /\n@mime:(.*)$/,''
302
+ @content = content.sub /^@mime:(.*)\n/,''
148
303
  @mime = $1
149
304
  end
150
305
  end
151
306
  #
152
- def compile!
153
- e, @options[:ext] = Config.engine_proc_ext @options[:engine], @options[:ext]
154
- v, l = view, layout
307
+ def compile_page!
308
+ e, @options[:ext] = app.engine_proc_ext @options[:engine], @options[:ext]
309
+ v, l, debug = view, layout, app.opt(:debug)
310
+ puts " * "+(File.exists?(l) ? 'use layout' : 'not found layout')+" : "+l if debug
311
+ puts " * "+(File.exists?(v) ? 'use view ' : 'not found view ')+" : "+v if debug
155
312
  state (@options[:layout].nil? ? :partial : :view)
156
313
  @content, mime = e.call v, self if e and File.exists? v
157
314
  @mime = mime unless mime.nil?
@@ -160,86 +317,6 @@ module Zorglub
160
317
  @mime = mime unless mime.nil?
161
318
  end
162
319
  #
163
- def redirect target, options={}, &block
164
- status = options[:status] || 302
165
- body = options[:body] || redirect_body(target)
166
- header = response.header.merge('Location' => target.to_s)
167
- throw :stop_realize, Rack::Response.new(body, status, header, &block)
168
- end
169
- #
170
- def redirect_body target
171
- "You are being redirected, please follow this link to: <a href='#{target}'>#{target}</a>!"
172
- end
173
- #
174
- def state state=nil
175
- @options[:state] = state unless state.nil?
176
- @options[:state]
177
- end
178
- #
179
- def engine engine=nil
180
- @options[:engine] = engine unless engine.nil? or engine.empty?
181
- @options[:engine]
182
- end
183
- #
184
- def layout layout=nil
185
- @options[:layout] = layout unless layout.nil? or layout.empty?
186
- return '' if @options[:layout].nil?
187
- File.join(Config.layout_base_path, @options[:layout])+ext
188
- end
189
- #
190
- def no_layout
191
- @options[:layout] = nil
192
- end
193
- #
194
- def static val=nil
195
- @options[:static] = val if (val==true or val==false)
196
- return nil if not @options[:static] or @options[:view].nil?
197
- File.join(Config.static_base_path, @options[:view])+ext
198
- end
199
- #
200
- def view view=nil
201
- @options[:view] = view unless view.nil? or view.empty?
202
- return '' if @options[:view].nil?
203
- File.join(Config.view_base_path, @options[:view])+ext
204
- end
205
- #
206
- def ext ext=nil
207
- if ext.nil? or ext.empty?
208
- @options[:ext]||''
209
- else
210
- @options[:ext] = (ext[0]=='.' ? (ext.length==1 ? nil : ext) : '.'+ext)
211
- end
212
- end
213
- #
214
- def inherited_var sym, *args
215
- d = self.class.inherited_vars[sym].clone || []
216
- unless args.empty?
217
- d.concat args
218
- d.uniq!
219
- end
220
- d
221
- end
222
- #
223
- def app
224
- self.class.app
225
- end
226
- #
227
- def args
228
- @options[:args]
229
- end
230
- #
231
- def map
232
- self.class.r
233
- end
234
- #
235
- def r *args
236
- File.join map, (args.empty? ? @options[:method] : args.map { |x| x.to_s } )
237
- end
238
- #
239
- def html
240
- [ :map, :r, :args, :engine, :layout, :view ].inject('') { |s,sym| s+="<p>#{sym} => #{self.send sym}</p>"; s }
241
- end
242
- #
243
320
  end
244
321
  #
245
322
  end
@@ -13,17 +13,18 @@ module Zorglub
13
13
  end
14
14
  #
15
15
  def session
16
- @session ||= SessionHash.new @request, @response, Node.sessions
16
+ @session ||= SessionHash.new @request, @response, Node.sessions, app.opt(:session_options)
17
17
  end
18
18
  end
19
19
  #
20
20
  class SessionHash < Hash
21
21
  #
22
- def initialize req, resp, sessions
22
+ def initialize req, resp, sessions, options
23
23
  @request = req
24
24
  @response = resp
25
25
  @sessions = sessions
26
26
  @sid = nil
27
+ @options = options
27
28
  super()
28
29
  end
29
30
  #
@@ -46,7 +47,7 @@ module Zorglub
46
47
  #
47
48
  def clear
48
49
  load_data!
49
- # @response.delete_cookie Zorglub::Config.session_key
50
+ # @response.delete_cookie @options[:session_key]
50
51
  # @sessions.delete @sid
51
52
  # @sid = nil
52
53
  super
@@ -94,11 +95,11 @@ module Zorglub
94
95
  #
95
96
  def load_data!
96
97
  return if loaded?
97
- if Config.session_on
98
- sid = @request.cookies[Zorglub::Config.session_key]
98
+ if @options[:session_on]
99
+ sid = @request.cookies[@options[:session_key]]
99
100
  if sid.nil?
100
101
  sid = generate_sid!
101
- @response.set_cookie Zorglub::Config.session_key, sid
102
+ @response.set_cookie @options[:session_key], sid
102
103
  end
103
104
  replace @sessions[sid] ||={}
104
105
  @sessions[sid] = self
@@ -125,7 +126,7 @@ module Zorglub
125
126
  # SecureRandom is available since Ruby 1.8.7.
126
127
  # For Ruby versions earlier than that, you can require the uuidtools gem,
127
128
  # which has a drop-in replacement for SecureRandom.
128
- def sid_algorithm; SecureRandom.hex(Zorglub::Config.session_sid_len); end
129
+ def sid_algorithm; SecureRandom.hex(@options[:session_sid_len]); end
129
130
  rescue LoadError
130
131
  require 'openssl'
131
132
  # Using OpenSSL::Random for generation, this is comparable in performance
@@ -133,7 +134,7 @@ module Zorglub
133
134
  # have the same behaviour as the SecureRandom::hex method of the
134
135
  # uuidtools gem.
135
136
  def sid_algorithm
136
- OpenSSL::Random.random_bytes(Zorglub::Config.session_sid_len / 2).unpack('H*')[0]
137
+ OpenSSL::Random.random_bytes(@options[:session_sid_len] / 2).unpack('H*')[0]
137
138
  end
138
139
  rescue LoadError
139
140
  # Digest::SHA2::hexdigest produces a string of length 64, although
data/lib/zorglub.rb CHANGED
@@ -1,13 +1,12 @@
1
1
  #! /usr/bin/env ruby
2
2
  # -*- coding: UTF-8 -*-
3
3
  #
4
- require './lib/zorglub/config'
5
- require './lib/zorglub/node'
6
- require './lib/zorglub/app'
4
+ require 'zorglub/node'
5
+ require 'zorglub/app'
7
6
  #
8
7
  module Zorglub
9
8
  #
10
- VERSION = '0.0.4'
9
+ VERSION = '0.0.5'
11
10
  #
12
11
  end
13
12
  #
File without changes
File without changes
@@ -0,0 +1 @@
1
+ %h1="Hello world"
data/spec/node_spec.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  require 'spec_helper'
4
4
  #
5
5
  def clean_static_path
6
- static_base_path = Zorglub::Config.static_base_path
6
+ static_base_path = Node0.app.static_base_path
7
7
  Dir.glob( File.join(static_base_path,'**','*') ).each do |f| File.unlink f if File.file? f end
8
8
  Dir.glob( File.join(static_base_path,'*') ).each do |d| Dir.rmdir d end
9
9
  Dir.rmdir static_base_path if File.directory? static_base_path
@@ -22,12 +22,11 @@ describe Zorglub do
22
22
  end
23
23
  #
24
24
  it "engine should return default Node's engine" do
25
- Node0.engine.should == Zorglub::Config.engine
26
- Node0.engine.should == Zorglub::Config[:engine]
25
+ Node0.engine.should == Node0.app.opt(:engine)
27
26
  end
28
27
  #
29
28
  it "layout should return default Node's layout" do
30
- Node0.layout.should == Zorglub::Config.layout
29
+ Node0.layout.should == Node0.app.opt(:layout)
31
30
  end
32
31
  #
33
32
  it "engine should return class defined Node's engine" do
@@ -85,8 +84,8 @@ describe Zorglub do
85
84
  r = Node0.my_call '/index'
86
85
  r.status.should == 200
87
86
  h = YAML.load r.body[0]
88
- ly = File.join Zorglub::Config.root, Zorglub::Config.layout_dir, Node0.layout
89
- vu = File.join Zorglub::Config.root, Zorglub::Config.view_dir, Node0.r, 'index'
87
+ ly = File.join Node0.app.layout_base_path, Node0.layout
88
+ vu = File.join Node0.app.view_base_path, Node0.r, 'index'
90
89
  h[:path].should == ly
91
90
  h[:layout].should == ly
92
91
  h[:view].should == vu
@@ -96,8 +95,8 @@ describe Zorglub do
96
95
  r = Node1.my_call '/index'
97
96
  r.status.should == 200
98
97
  h = YAML.load r.body[0]
99
- ly = File.join Zorglub::Config.root, Zorglub::Config.layout_dir, 'main.spec'
100
- vu = File.join Zorglub::Config.root, Zorglub::Config.view_dir, Node1.r, 'index.spec'
98
+ ly = File.join Node1.app.layout_base_path, 'main.spec'
99
+ vu = File.join Node1.app.view_base_path, Node1.r, 'index.spec'
101
100
  h[:path].should == ly
102
101
  h[:layout].should == ly
103
102
  h[:view].should == vu
@@ -127,6 +126,30 @@ describe Zorglub do
127
126
  Node3.after.should == 3
128
127
  end
129
128
  #
129
+ it "inherited before_all hook should work" do
130
+ Node3.before = 0
131
+ Node3.after = 0
132
+ Node3.before.should == 0
133
+ Node8.my_call '/index'
134
+ Node3.before.should == 1
135
+ Node8.my_call '/index'
136
+ Node3.before.should == 2
137
+ Node8.my_call '/index'
138
+ Node3.before.should == 3
139
+ end
140
+ #
141
+ it "inherited after_all hook should work" do
142
+ Node3.before = 0
143
+ Node3.after = 0
144
+ Node3.after.should == 0
145
+ Node8.my_call '/index'
146
+ Node3.after.should == 1
147
+ Node8.my_call '/index'
148
+ Node3.after.should == 2
149
+ Node8.my_call '/index'
150
+ Node3.after.should == 3
151
+ end
152
+ #
130
153
  it "should find view and layout and render them" do
131
154
  r = Node0.my_call '/do_render'
132
155
  r.status.should == 200
@@ -172,6 +195,10 @@ describe Zorglub do
172
195
  r.header['location'].should == Node0.r(:do_partial,1,2,3)
173
196
  end
174
197
  #
198
+ it "no_layout! should be inherited" do
199
+ Node5.layout.should be_nil
200
+ end
201
+ #
175
202
  it "inherited_vars should be inherited and extended" do
176
203
  r = Node5.my_call '/index'
177
204
  vars = YAML.load r.body[0]
@@ -195,10 +222,28 @@ describe Zorglub do
195
222
  #
196
223
  it "ext definition and file engine should work" do
197
224
  r = Node0.my_call '/xml_file'
198
- r.body[0]='<xml>file</xml>'
225
+ r.body[0].should == "<xml>file<\/xml>\n"
199
226
  r = Node0.my_call '/plain_file'
200
- r.body[0]='plain text'
227
+ r.body[0].should == "plain file\n"
201
228
  end
229
+ #
230
+ it "haml engine should work" do
231
+ r = Node0.my_call '/engines/haml'
232
+ r.body[0].should == "<h1>Hello world</h1>\n"
233
+ end
234
+ #
235
+ it "view_base_path! should work" do
236
+ r = Node7.my_call '/view_path'
237
+ h = YAML.load r.body[0]
238
+ h[:view].should == File.join(Node7.app.opt(:root), 'alt','do_render')
239
+ end
240
+ #
241
+ it "layout_base_path! should work" do
242
+ r = Node7.my_call '/view_path'
243
+ h = YAML.load r.body[0]
244
+ h[:layout].should == File.join(Node7.app.opt(:root), 'alt','layout','default')
245
+ end
246
+ #
202
247
  end
203
248
  #
204
249
  end
data/spec/spec_helper.rb CHANGED
@@ -12,6 +12,7 @@ require 'yaml'
12
12
  #
13
13
  require 'zorglub'
14
14
  require 'zorglub/engines/file'
15
+ require 'zorglub/engines/haml'
15
16
  #
16
17
  HASH_PROC = Proc.new { |path,obj| {:path=>path,:layout=>obj.layout,:view=>obj.view,:args=>obj.args,:map=>obj.map}.to_yaml }
17
18
  STATIC_PROC = Proc.new { |path,obj| ["VAL #{obj.value}",'text/static'] }
@@ -27,14 +28,7 @@ RENDER_PROC = Proc.new { |path,obj|
27
28
  raise Exception.new
28
29
  end
29
30
  }
30
- Zorglub::Config.register_engine 'default', nil, HASH_PROC
31
- Zorglub::Config.register_engine 'engine-1', 'spec', HASH_PROC
32
- Zorglub::Config.register_engine 'engine-2', 'spec', HASH_PROC
33
- Zorglub::Config.register_engine 'real', nil, RENDER_PROC
34
- Zorglub::Config.register_engine 'static', nil, STATIC_PROC
35
- #
36
- Zorglub::Config[:engine] = 'default'
37
- Zorglub::Config.root = File.join Dir.pwd, 'spec', 'data'
31
+ APP_ROOT = File.join Dir.pwd, 'spec', 'data'
38
32
  #
39
33
  class Zorglub::Node
40
34
  def self.my_call uri
@@ -51,42 +45,49 @@ class Node0 < Zorglub::Node
51
45
  html
52
46
  end
53
47
  def hello
54
- no_layout
48
+ no_layout!
55
49
  'world'
56
50
  end
57
51
  def with_2args a1, a2
58
52
  end
59
53
  def do_render
60
- engine 'real'
54
+ engine! 'real'
61
55
  end
62
56
  def do_partial a1, a2
63
- engine 'real'
57
+ engine! 'real'
64
58
  end
65
59
  def other_view
66
- engine 'real'
67
- view r('do_partial')
60
+ engine! 'real'
61
+ view! r('do_partial')
68
62
  end
69
63
  def do_redirect
70
64
  redirect r(:do_partial,1,2,3)
71
65
  end
72
66
  def xml_file
73
- no_layout
74
- engine :file
75
- ext 'xml'
67
+ no_layout!
68
+ engine! :file
69
+ ext! 'xml'
76
70
  end
77
71
  def plain_file
78
- no_layout
79
- engine :file
80
- ext 'txt'
72
+ no_layout!
73
+ engine! :file
74
+ ext! 'txt'
75
+ end
76
+ def engines name
77
+ no_layout!
78
+ case name
79
+ when 'haml'
80
+ engine! :haml
81
+ end
81
82
  end
82
83
  end
83
84
  #
84
85
  class Node1 < Zorglub::Node
85
- layout 'layout-1'
86
- engine 'engine-1'
86
+ layout! 'layout-1'
87
+ engine! 'engine-1'
87
88
  def index
88
- layout 'main'
89
- engine 'engine-2'
89
+ layout! 'main'
90
+ engine! 'engine-2'
90
91
  end
91
92
  end
92
93
  #
@@ -106,21 +107,23 @@ class Node3 < Zorglub::Node
106
107
  after_all do |node|
107
108
  Node3.after +=1
108
109
  end
109
- layout 'layout-2'
110
- engine 'engine-2'
110
+ layout! 'layout-2'
111
+ engine! 'engine-2'
111
112
  def index
112
- (self.class.before-self.class.after).should == 1
113
+ (Node3.before-Node3.after).should == 1
113
114
  end
114
115
  end
115
116
  #
117
+ class Node8 < Node3
118
+ end
119
+ #
116
120
  class Node4 < Zorglub::Node
121
+ no_layout!
117
122
  inherited_var :js,'js0','js1'
118
123
  def index
119
- no_layout
120
124
  inherited_var(:js).to_yaml
121
125
  end
122
126
  def more
123
- no_layout
124
127
  inherited_var(:js,'js2').to_yaml
125
128
  end
126
129
  end
@@ -128,8 +131,8 @@ end
128
131
  class Node5 < Node4
129
132
  inherited_var :js, 'js3'
130
133
  inherited_var :css, 'css0', 'css1'
134
+ # no_layout! inherited from Node4
131
135
  def index
132
- no_layout
133
136
  js = inherited_var(:js,'jsx')
134
137
  css = inherited_var(:css, 'css0', 'css1','css2')
135
138
  js.concat(css).to_yaml
@@ -142,30 +145,50 @@ class Node6 < Zorglub::Node
142
145
  attr_accessor :static_cpt
143
146
  end
144
147
  attr_reader :value
145
- static true
148
+ static! true
146
149
  def no_static
147
- static false
148
- engine 'static'
149
- view Node0.r('do_render')
150
+ static! false
151
+ engine! 'static'
152
+ view! Node0.r('do_render')
150
153
  Node6.static_cpt+=1
151
154
  @value = Node6.static_cpt
152
155
  end
153
156
  def do_static
154
- engine 'static'
155
- view Node0.r('do_render')
157
+ engine! 'static'
158
+ view! Node0.r('do_render')
156
159
  Node6.static_cpt+=1
157
160
  @value = Node6.static_cpt
158
161
  end
159
162
  end
160
163
  #
164
+ class Node7 < Zorglub::Node
165
+ layout_base_path! File.join APP_ROOT, 'alt','layout'
166
+ view_base_path! File.join APP_ROOT, 'alt'
167
+ def view_path
168
+ view! 'do_render'
169
+ end
170
+ end
171
+ #
161
172
  APP = Zorglub::App.new do
173
+ register_engine! :file, nil, Zorglub::Engines::File.method(:proc)
174
+ register_engine! :haml, 'haml', Zorglub::Engines::Haml.method(:proc)
175
+ register_engine! 'default', nil, HASH_PROC
176
+ register_engine! 'engine-1', 'spec', HASH_PROC
177
+ register_engine! 'engine-2', 'spec', HASH_PROC
178
+ register_engine! 'real', nil, RENDER_PROC
179
+ register_engine! 'static', nil, STATIC_PROC
180
+ opt! :root, APP_ROOT
181
+ opt! :engine, 'default'
162
182
  map '/node0', Node0
163
183
  map '/node1', Node1
164
184
  map '/node3', Node3
165
185
  map '/node4', Node4
166
186
  map '/node5', Node5
167
187
  map '/node6', Node6
188
+ map '/node7', Node7
189
+ map '/node8', Node8
168
190
  end
191
+ #
169
192
  class Node2
170
193
  map APP, '/node2'
171
194
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zorglub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-05 00:00:00.000000000 Z
12
+ date: 2012-01-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
16
- requirement: &20000040 !ruby/object:Gem::Requirement
16
+ requirement: &23057180 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.4.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *20000040
24
+ version_requirements: *23057180
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &19999520 !ruby/object:Gem::Requirement
27
+ requirement: &23056680 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.8.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *19999520
35
+ version_requirements: *23056680
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &19998860 !ruby/object:Gem::Requirement
38
+ requirement: &23055880 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 0.8.7
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *19998860
46
+ version_requirements: *23055880
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bones
49
- requirement: &19998120 !ruby/object:Gem::Requirement
49
+ requirement: &23055180 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: 3.7.3
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *19998120
57
+ version_requirements: *23055180
58
58
  description: a nano web application framework based on rack[http://rack.rubyforge.org/]
59
59
  email: jeremy@asynk.ch
60
60
  executables: []
@@ -69,17 +69,19 @@ files:
69
69
  - Rakefile
70
70
  - lib/zorglub.rb
71
71
  - lib/zorglub/app.rb
72
- - lib/zorglub/config.rb
73
72
  - lib/zorglub/engines/file.rb
74
73
  - lib/zorglub/engines/haml.rb
75
74
  - lib/zorglub/node.rb
76
75
  - lib/zorglub/rack_session.rb
77
76
  - lib/zorglub/session.rb
78
77
  - spec/app_spec.rb
78
+ - spec/data/alt/do_render
79
+ - spec/data/alt/layout/default
79
80
  - spec/data/layout/default
80
81
  - spec/data/layout/main.spec
81
82
  - spec/data/view/node0/do_partial
82
83
  - spec/data/view/node0/do_render
84
+ - spec/data/view/node0/engines.haml
83
85
  - spec/data/view/node0/plain_file.txt
84
86
  - spec/data/view/node0/xml_file.xml
85
87
  - spec/node_spec.rb
@@ -1,81 +0,0 @@
1
- # -*- coding: UTF-8 -*-
2
- #
3
- module Zorglub
4
- #
5
- class Config
6
- @options = {
7
- :root => '.',
8
- :engine => nil,
9
- :layout => 'default',
10
- :view_dir => 'view',
11
- :layout_dir => 'layout',
12
- :static_dir => 'static',
13
- :session_on => false,
14
- :session_key => 'zorglub.sid',
15
- :session_secret => 'session-secret-secret',
16
- :session_sid_len => 64,
17
- :engines_cache_enabled => true,
18
- :haml_options => {
19
- :format => :html5,
20
- :ugly => false,
21
- :encoding => 'utf-8'
22
- }
23
- #
24
- }
25
- @engines = { }
26
- class << self
27
- #
28
- def [] k
29
- @options[k]
30
- end
31
- #
32
- def []= k, v
33
- @options[k]=v
34
- end
35
- #
36
- def view_base_path
37
- p = @options[:view_path]
38
- ( p.nil? ? File.join(@options[:root], @options[:view_dir]) : p )
39
- end
40
- #
41
- def layout_base_path
42
- p = @options[:layout_path]
43
- ( p.nil? ? File.join(@options[:root], @options[:layout_dir]) : p )
44
- end
45
- #
46
- def static_base_path
47
- p = @options[:static_path]
48
- ( p.nil? ? File.join(@options[:root], @options[:static_dir]) : p )
49
- end
50
- #
51
- def register_engine name, ext, proc
52
- return unless name
53
- if ext.nil? or ext.empty?
54
- x = nil
55
- else
56
- x = (ext[0]=='.' ? (ext.length==1 ? nil : ext) : '.'+ext)
57
- end
58
- @engines[name]=[ proc, x ]
59
- end
60
- #
61
- def engine_proc_ext engine, ext
62
- p,x = @engines[engine]
63
- return [nil, ''] if p.nil?
64
- [ p, (x.nil? ? ext : x ) ]
65
- end
66
- #
67
- end
68
- #
69
- def self.method_missing m, *args, &block
70
- if m=~/(.*)=$/
71
- @options[$1.to_sym]=args[0]
72
- else
73
- @options[m.to_sym]
74
- end
75
- end
76
- #
77
- end
78
- #
79
- end
80
- #
81
- # EOF