zorglub 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog CHANGED
@@ -1,2 +1,18 @@
1
- FIME-date Jérémy Zurcher <jeremy@asynk.ch>
1
+ 2011-05-26 Jérémy Zurcher <jeremy@asynk.ch>
2
2
  * Project creation
3
+
4
+ 2011-09-13 Jérémy Zurcher <jeremy@asynk.ch>
5
+ * usable WIP
6
+
7
+ 2012-01-04 Jérémy Zurcher <jeremy@asynk.ch>
8
+ * lots of code cleanup
9
+ * replace Helpers Module with Node#inherited_var Node@inherited_vars
10
+ * rewrite session so it is compatible with Rack SessionHash
11
+ * update test application and write specs
12
+
13
+ 2012-01-05 Jérémy Zurcher <jeremy@asynk.ch>
14
+ * haml engine
15
+ * optional engine cache
16
+ * optional engine proc mime-type definition
17
+ * optional static page generation/cache
18
+ *
data/lib/zorglub/app.rb CHANGED
@@ -11,7 +11,9 @@ module Zorglub
11
11
  @map = map
12
12
  instance_eval &block if block_given?
13
13
  remap @map
14
+ @engines_cache = { }
14
15
  end
16
+ attr_reader :engines_cache
15
17
  #
16
18
  def map location, object
17
19
  return unless location and object
@@ -9,10 +9,17 @@ module Zorglub
9
9
  :layout => 'default',
10
10
  :view_dir => 'view',
11
11
  :layout_dir => 'layout',
12
+ :static_dir => 'static',
12
13
  :session_on => false,
13
14
  :session_key => 'zorglub.sid',
14
15
  :session_secret => 'session-secret-secret',
15
- :session_sid_len => 64
16
+ :session_sid_len => 64,
17
+ :engines_cache_enabled => true,
18
+ :haml_options => {
19
+ :format => :html5,
20
+ :ugly => false,
21
+ :encoding => 'utf-8'
22
+ }
16
23
  #
17
24
  }
18
25
  @engines = { }
@@ -36,6 +43,11 @@ module Zorglub
36
43
  ( p.nil? ? File.join(@options[:root], @options[:layout_dir]) : p )
37
44
  end
38
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
+ #
39
51
  def register_engine name, ext, proc
40
52
  return unless name
41
53
  @engines[name]=[ ext, proc ]
@@ -0,0 +1,25 @@
1
+ # -*- coding: UTF-8 -*-
2
+ #
3
+ require 'haml/util'
4
+ require 'haml/engine'
5
+ #
6
+ module Zorglub
7
+ module Engines
8
+ module Haml
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 )
13
+ else
14
+ haml = ::Haml::Engine.new( File.open(path,'r'){|f| f.read }, Zorglub::Config.haml_options )
15
+ end
16
+ html = haml.render(obj)
17
+ return html, 'text/html'
18
+ end
19
+ end
20
+ end
21
+ end
22
+ #
23
+ Zorglub::Config.register_engine :haml, 'haml', Zorglub::Engines::Haml.method(:proc)
24
+ #
25
+ # EOF
data/lib/zorglub/node.rb CHANGED
@@ -32,6 +32,11 @@ module Zorglub
32
32
  @layout ||= Config.layout
33
33
  end
34
34
  #
35
+ def static val=nil
36
+ @static = val if (val==true or val==false)
37
+ @static ||=false
38
+ end
39
+ #
35
40
  def inherited_var sym, *args
36
41
  var = @inherited_vars[sym] ||=[]
37
42
  unless args.empty?
@@ -41,7 +46,7 @@ module Zorglub
41
46
  var
42
47
  end
43
48
  #
44
- attr_writer :app
49
+ attr_accessor :app
45
50
  def map app, location
46
51
  @app = app
47
52
  @app.map location, self
@@ -55,13 +60,13 @@ module Zorglub
55
60
  def call env
56
61
  meth, *args = env['PATH_INFO'].sub(/^\//,'').split '/'
57
62
  meth||= 'index'
58
- node = self.new env, {:engine=>engine,:layout=>layout,:view=>r(meth),:method=>meth,:args=>args}
63
+ node = self.new env, {:engine=>engine,:layout=>layout,:view=>r(meth),:method=>meth,:args=>args,:static=>static}
59
64
  return error_404 node if not node.respond_to? meth
60
65
  node.realize!
61
66
  end
62
67
  #
63
68
  def partial meth, *args
64
- node = self.new nil, {:engine=>engine,:layout=>nil,:view=>r(meth),:method=>meth.to_s,:args=>args}
69
+ node = self.new nil, {:engine=>engine,:layout=>nil,:view=>r(meth),:method=>meth.to_s,:args=>args,:static=>static}
65
70
  return error_404 node if not node.respond_to? meth
66
71
  node.feed!
67
72
  node.content
@@ -95,11 +100,11 @@ module Zorglub
95
100
  #
96
101
  end
97
102
  #
98
- attr_reader :action, :request, :response, :content
103
+ attr_reader :options, :request, :response, :content, :mime
99
104
  #
100
- def initialize env, action
105
+ def initialize env, options
101
106
  @env = env
102
- @action = action
107
+ @options = options
103
108
  @request = Rack::Request.new env
104
109
  @response = Rack::Response.new
105
110
  end
@@ -108,23 +113,50 @@ module Zorglub
108
113
  catch(:stop_realize) {
109
114
  feed!
110
115
  response.write @content
116
+ response.header['Content-Type'] = @mime||'text/html'
111
117
  response.finish
112
118
  response
113
119
  }
114
120
  end
115
121
  #
116
122
  def feed!
123
+ state :pre_cb
117
124
  Node.call_before_hooks self
118
125
  state :meth
119
- @content = self.send @action[:method], *@action[:args]
120
- v, l, e = view, layout, Config.engine_proc(@action[:engine])
121
- # TODO compile and cache
122
- state (@action[:layout].nil? ? :partial : :view)
123
- @content = e.call v, self if e and File.exists? v
124
- state :layout
125
- @content = e.call l, self if e and File.exists? l
126
+ @content = self.send @options[:method], *@options[:args]
127
+ static_path = static
128
+ if static_path.nil?
129
+ compile!
130
+ else
131
+ static! static_path
132
+ end
133
+ state :post_cb
126
134
  Node.call_after_hooks self
127
- @content
135
+ state :finished
136
+ return @content, @mime
137
+ end
138
+ #
139
+ def static! path
140
+ if not File.exists? path
141
+ compile!
142
+ Dir.mkdir Config.static_base_path
143
+ Dir.mkdir File.dirname path
144
+ File.open(path, 'w') {|f| f.write(@content); f.write("\n@mime:"+@mime) }
145
+ else
146
+ content = File.open(path, 'r') {|f| f.read }
147
+ @content = content.sub /\n@mime:(.*)$/,''
148
+ @mime = $1
149
+ end
150
+ end
151
+ #
152
+ def compile!
153
+ v, l, e = view, layout, Config.engine_proc(@options[:engine])
154
+ state (@options[:layout].nil? ? :partial : :view)
155
+ @content, mime = e.call v, self if e and File.exists? v
156
+ @mime = mime unless mime.nil?
157
+ state :layout
158
+ @content, mime = e.call l, self if e and File.exists? l
159
+ @mime = mime unless mime.nil?
128
160
  end
129
161
  #
130
162
  def redirect target, options={}, &block
@@ -139,29 +171,35 @@ module Zorglub
139
171
  end
140
172
  #
141
173
  def state state=nil
142
- @action[:state] = state unless state.nil?
143
- @action[:state]
174
+ @options[:state] = state unless state.nil?
175
+ @options[:state]
144
176
  end
145
177
  #
146
178
  def engine engine=nil
147
- @action[:engine] = engine unless engine.nil? or engine.empty?
148
- @action[:engine]
179
+ @options[:engine] = engine unless engine.nil? or engine.empty?
180
+ @options[:engine]
149
181
  end
150
182
  #
151
183
  def layout layout=nil
152
- @action[:layout] = layout unless layout.nil? or layout.empty?
153
- return '' if @action[:layout].nil?
154
- File.join(Config.layout_base_path, @action[:layout])+ Config.engine_ext(@action[:engine])
184
+ @options[:layout] = layout unless layout.nil? or layout.empty?
185
+ return '' if @options[:layout].nil?
186
+ File.join(Config.layout_base_path, @options[:layout])+ Config.engine_ext(@options[:engine])
155
187
  end
156
188
  #
157
189
  def no_layout
158
- @action[:layout] = nil
190
+ @options[:layout] = nil
191
+ end
192
+ #
193
+ def static val=nil
194
+ @options[:static] = val if (val==true or val==false)
195
+ return nil if not @options[:static] or @options[:view].nil?
196
+ File.join(Config.static_base_path, @options[:view])+Config.engine_ext(@options[:engine])
159
197
  end
160
198
  #
161
199
  def view view=nil
162
- @action[:view] = view unless view.nil? or view.empty?
163
- return '' if @action[:view].nil?
164
- File.join(Config.view_base_path, @action[:view])+Config.engine_ext(@action[:engine])
200
+ @options[:view] = view unless view.nil? or view.empty?
201
+ return '' if @options[:view].nil?
202
+ File.join(Config.view_base_path, @options[:view])+Config.engine_ext(@options[:engine])
165
203
  end
166
204
  #
167
205
  def inherited_var sym, *args
@@ -173,8 +211,12 @@ module Zorglub
173
211
  d
174
212
  end
175
213
  #
214
+ def app
215
+ self.class.app
216
+ end
217
+ #
176
218
  def args
177
- @action[:args]
219
+ @options[:args]
178
220
  end
179
221
  #
180
222
  def map
@@ -182,7 +224,7 @@ module Zorglub
182
224
  end
183
225
  #
184
226
  def r *args
185
- File.join map, (args.empty? ? @action[:method] : args.map { |x| x.to_s } )
227
+ File.join map, (args.empty? ? @options[:method] : args.map { |x| x.to_s } )
186
228
  end
187
229
  #
188
230
  def html
data/lib/zorglub.rb CHANGED
@@ -7,7 +7,7 @@ require './lib/zorglub/app'
7
7
  #
8
8
  module Zorglub
9
9
  #
10
- VERSION = '0.0.2'
10
+ VERSION = '0.0.3'
11
11
  #
12
12
  end
13
13
  #
data/spec/node_spec.rb CHANGED
@@ -2,9 +2,24 @@
2
2
  #
3
3
  require 'spec_helper'
4
4
  #
5
+ def clean_static_path
6
+ static_base_path = Zorglub::Config.static_base_path
7
+ Dir.glob( File.join(static_base_path,'**','*') ).each do |f| File.unlink f if File.file? f end
8
+ Dir.glob( File.join(static_base_path,'*') ).each do |d| Dir.rmdir d end
9
+ Dir.rmdir static_base_path if File.directory? static_base_path
10
+ end
11
+ #
5
12
  describe Zorglub do
6
13
  #
7
14
  describe Zorglub::Node do
15
+ #
16
+ before(:all) do
17
+ clean_static_path
18
+ end
19
+ #
20
+ after(:all) do
21
+ clean_static_path
22
+ end
8
23
  #
9
24
  it "engine should return default Node's engine" do
10
25
  Node0.engine.should == Zorglub::Config.engine
@@ -118,6 +133,16 @@ describe Zorglub do
118
133
  r.body[0].should == "layout_start view_content layout_end"
119
134
  end
120
135
  #
136
+ it "default mime-type should be text/html" do
137
+ r = Node0.my_call '/index'
138
+ r.header['Content-type'].should == 'text/html'
139
+ end
140
+ #
141
+ it "should be able to override mime-type" do
142
+ r = Node0.my_call '/do_render'
143
+ r.header['Content-type'].should == 'text/view'
144
+ end
145
+ #
121
146
  it "partial should render correctly" do
122
147
  Node0.partial(:do_partial, 1, 2).should == 'partial_content'
123
148
  end
@@ -126,6 +151,21 @@ describe Zorglub do
126
151
  Node0.partial(:other_view).should == 'partial_content'
127
152
  end
128
153
  #
154
+ it "static pages should be generated" do
155
+ r = Node0.my_call '/do_static'
156
+ r.body[0].should == 'VAL 1'
157
+ r.header['Content-type'].should == 'text/static'
158
+ r = Node0.my_call '/do_static'
159
+ r.body[0].should == 'VAL 1'
160
+ r.header['Content-type'].should == 'text/static'
161
+ r = Node0.my_call '/do_static'
162
+ r.body[0].should == 'VAL 1'
163
+ r.header['Content-type'].should == 'text/static'
164
+ r = Node0.my_call '/no_static'
165
+ r.body[0].should == 'VAL 4'
166
+ r.header['Content-type'].should == 'text/static'
167
+ end
168
+ #
129
169
  it "redirect should work" do
130
170
  r = Node0.my_call '/do_redirect'
131
171
  r.status.should == 302
data/spec/spec_helper.rb CHANGED
@@ -13,14 +13,15 @@ require 'yaml'
13
13
  require 'zorglub'
14
14
  #
15
15
  HASH_PROC = Proc.new { |path,obj| {:path=>path,:layout=>obj.layout,:view=>obj.view,:args=>obj.args,:map=>obj.map}.to_yaml }
16
+ STATIC_PROC = Proc.new { |path,obj| ["VAL #{obj.value}",'text/static'] }
16
17
  RENDER_PROC = Proc.new { |path,obj|
17
18
  case obj.state
18
19
  when :layout
19
20
  "layout_start #{obj.content} layout_end"
20
21
  when :view
21
- "view_content"
22
+ ["view_content", 'text/view']
22
23
  when :partial
23
- 'partial_content'
24
+ ['partial_content','text/partial']
24
25
  else
25
26
  raise Exception.new
26
27
  end
@@ -29,6 +30,7 @@ Zorglub::Config.register_engine 'default', nil, HASH_PROC
29
30
  Zorglub::Config.register_engine 'engine-1', 'spec', HASH_PROC
30
31
  Zorglub::Config.register_engine 'engine-2', 'spec', HASH_PROC
31
32
  Zorglub::Config.register_engine 'real', nil, RENDER_PROC
33
+ Zorglub::Config.register_engine 'static', nil, STATIC_PROC
32
34
  #
33
35
  Zorglub::Config[:engine] = 'default'
34
36
  Zorglub::Config.root = File.join Dir.pwd, 'spec', 'data'
@@ -43,6 +45,10 @@ class Temp < Zorglub::Node
43
45
  end
44
46
  #
45
47
  class Node0 < Zorglub::Node
48
+ @static_cpt=0
49
+ class << self
50
+ attr_accessor :static_cpt
51
+ end
46
52
  # default
47
53
  def index
48
54
  html
@@ -66,6 +72,21 @@ class Node0 < Zorglub::Node
66
72
  def do_redirect
67
73
  redirect r(:do_partial,1,2,3)
68
74
  end
75
+ attr_reader :value
76
+ def no_static
77
+ static false
78
+ engine 'static'
79
+ view r('do_render')
80
+ Node0.static_cpt+=1
81
+ @value = Node0.static_cpt
82
+ end
83
+ def do_static
84
+ static true
85
+ engine 'static'
86
+ view r('do_render')
87
+ Node0.static_cpt+=1
88
+ @value = Node0.static_cpt
89
+ end
69
90
  end
70
91
  #
71
92
  class Node1 < Zorglub::Node
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.2
4
+ version: 0.0.3
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-04 00:00:00.000000000 Z
12
+ date: 2012-01-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
16
- requirement: &15670960 !ruby/object:Gem::Requirement
16
+ requirement: &23307900 !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: *15670960
24
+ version_requirements: *23307900
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &15670060 !ruby/object:Gem::Requirement
27
+ requirement: &23323560 !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: *15670060
35
+ version_requirements: *23323560
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &15669580 !ruby/object:Gem::Requirement
38
+ requirement: &23322980 !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: *15669580
46
+ version_requirements: *23322980
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bones
49
- requirement: &15668980 !ruby/object:Gem::Requirement
49
+ requirement: &23322340 !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: *15668980
57
+ version_requirements: *23322340
58
58
  description: a nano web application framework based on rack[http://rack.rubyforge.org/]
59
59
  email: jeremy@asynk.ch
60
60
  executables: []
@@ -67,31 +67,32 @@ files:
67
67
  - README.rdoc
68
68
  - Rakefile
69
69
  - lib/zorglub.rb
70
- - lib/zorglub/session.rb
71
70
  - lib/zorglub/app.rb
72
- - lib/zorglub/rack_session.rb
73
- - lib/zorglub/node.rb
74
71
  - lib/zorglub/config.rb
72
+ - lib/zorglub/engines/haml.rb
73
+ - lib/zorglub/node.rb
74
+ - lib/zorglub/rack_session.rb
75
+ - lib/zorglub/session.rb
76
+ - spec/app_spec.rb
77
+ - spec/data/layout/default
78
+ - spec/data/layout/main.spec
75
79
  - spec/data/view/node0/do_partial
76
80
  - spec/data/view/node0/do_render
77
- - spec/data/layout/main.spec
78
- - spec/data/layout/default
79
- - spec/spec_helper.rb
80
81
  - spec/node_spec.rb
81
- - spec/app_spec.rb
82
- - tasks/spec.rake
83
- - tasks/helpers.rb
84
- - tasks/git.rake
85
- - tasks/test.rake
82
+ - spec/spec_helper.rb
83
+ - tasks/ann.rake
86
84
  - tasks/constants.rb
87
- - tasks/svn.rake
85
+ - tasks/gem.rake
86
+ - tasks/git.rake
87
+ - tasks/helpers.rb
88
+ - tasks/notes.rake
88
89
  - tasks/post_load.rake
90
+ - tasks/rdoc.rake
89
91
  - tasks/rubyforge.rake
90
- - tasks/notes.rake
91
92
  - tasks/setup.rb
92
- - tasks/gem.rake
93
- - tasks/ann.rake
94
- - tasks/rdoc.rake
93
+ - tasks/spec.rake
94
+ - tasks/svn.rake
95
+ - tasks/test.rake
95
96
  homepage: http://cgit.asynk.ch/cgi-bin/cgit/zorglub
96
97
  licenses: []
97
98
  post_install_message: