deas 0.29.0 → 0.30.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/Gemfile +0 -2
  2. data/deas.gemspec +0 -1
  3. data/lib/deas/deas_runner.rb +16 -0
  4. data/lib/deas/runner.rb +10 -8
  5. data/lib/deas/server.rb +0 -7
  6. data/lib/deas/sinatra_app.rb +0 -1
  7. data/lib/deas/sinatra_runner.rb +2 -23
  8. data/lib/deas/template_source.rb +20 -11
  9. data/lib/deas/test_runner.rb +14 -4
  10. data/lib/deas/version.rb +1 -1
  11. data/lib/deas/view_handler.rb +5 -3
  12. data/test/support/fake_sinatra_call.rb +0 -11
  13. data/test/support/routes.rb +4 -59
  14. data/test/support/template.erb +1 -1
  15. data/test/support/template.json +1 -1
  16. data/test/support/view_handlers.rb +21 -2
  17. data/test/system/rack_tests.rb +13 -43
  18. data/test/unit/deas_runner_tests.rb +87 -0
  19. data/test/unit/runner_tests.rb +4 -1
  20. data/test/unit/server_configuration_tests.rb +1 -10
  21. data/test/unit/sinatra_runner_tests.rb +0 -78
  22. data/test/unit/template_source_tests.rb +20 -16
  23. data/test/unit/test_runner_tests.rb +54 -14
  24. data/test/unit/view_handler_tests.rb +21 -6
  25. metadata +4 -49
  26. data/lib/deas/template.rb +0 -86
  27. data/test/support/views/_info.erb +0 -1
  28. data/test/support/views/haml_layout1.haml +0 -2
  29. data/test/support/views/haml_with_layout.haml +0 -1
  30. data/test/support/views/layout1.erb +0 -2
  31. data/test/support/views/layout2.erb +0 -2
  32. data/test/support/views/layout3.erb +0 -2
  33. data/test/support/views/show.erb +0 -2
  34. data/test/support/views/show.html.erb +0 -0
  35. data/test/support/views/show.json.erb +0 -0
  36. data/test/support/views/show_json.erb +0 -0
  37. data/test/support/views/some.html.file.other +0 -0
  38. data/test/support/views/some_file.engine +0 -0
  39. data/test/support/views/some_no_engine_extension +0 -0
  40. data/test/support/views/with_layout.erb +0 -1
  41. data/test/unit/template_tests.rb +0 -150
data/Gemfile CHANGED
@@ -4,6 +4,4 @@ gemspec
4
4
 
5
5
  gem 'rake'
6
6
  gem 'pry', "~> 0.9.0"
7
- gem 'multi_json'
8
- gem 'yajl-ruby'
9
7
  gem 'rack-test'
data/deas.gemspec CHANGED
@@ -24,6 +24,5 @@ Gem::Specification.new do |gem|
24
24
 
25
25
  gem.add_development_dependency("assert", ["~> 2.12"])
26
26
  gem.add_development_dependency("assert-rack-test")
27
- gem.add_development_dependency('haml')
28
27
 
29
28
  end
@@ -18,6 +18,22 @@ module Deas
18
18
  response_data
19
19
  end
20
20
 
21
+ def render(template_name, locals = nil)
22
+ source_render(self.template_source, template_name, locals)
23
+ end
24
+
25
+ def source_render(source, template_name, locals = nil)
26
+ source.render(template_name, self.handler, locals || {})
27
+ end
28
+
29
+ def partial(template_name, locals = nil)
30
+ source_partial(self.template_source, template_name, locals)
31
+ end
32
+
33
+ def source_partial(source, template_name, locals = nil)
34
+ source.partial(template_name, locals || {})
35
+ end
36
+
21
37
  private
22
38
 
23
39
  def run_callbacks(callbacks)
data/lib/deas/runner.rb CHANGED
@@ -30,14 +30,16 @@ module Deas
30
30
  @template_source = a[:template_source] || Deas::NullTemplateSource.new
31
31
  end
32
32
 
33
- def halt(*args); raise NotImplementedError; end
34
- def redirect(*args); raise NotImplementedError; end
35
- def content_type(*args); raise NotImplementedError; end
36
- def status(*args); raise NotImplementedError; end
37
- def headers(*args); raise NotImplementedError; end
38
- def render(*args); raise NotImplementedError; end
39
- def partial(*args); raise NotImplementedError; end
40
- def send_file(*args); raise NotImplementedError; end
33
+ def halt(*args); raise NotImplementedError; end
34
+ def redirect(*args); raise NotImplementedError; end
35
+ def content_type(*args); raise NotImplementedError; end
36
+ def status(*args); raise NotImplementedError; end
37
+ def headers(*args); raise NotImplementedError; end
38
+ def render(*args); raise NotImplementedError; end
39
+ def source_render(*args); raise NotImplementedError; end
40
+ def partial(*args); raise NotImplementedError; end
41
+ def source_partial(*args); raise NotImplementedError; end
42
+ def send_file(*args); raise NotImplementedError; end
41
43
 
42
44
  class NormalizedParams
43
45
 
data/lib/deas/server.rb CHANGED
@@ -7,7 +7,6 @@ require 'deas/logging'
7
7
  require 'deas/router'
8
8
  require 'deas/show_exceptions'
9
9
  require 'deas/sinatra_app'
10
- require 'deas/template'
11
10
  require 'deas/template_source'
12
11
 
13
12
  module Deas; end
@@ -98,12 +97,6 @@ module Deas::Server
98
97
  self.router.routes
99
98
  end
100
99
 
101
- def template_scope
102
- Class.new(Deas::Template::Scope).tap do |klass|
103
- klass.send(:include, *self.template_helpers)
104
- end
105
- end
106
-
107
100
  end
108
101
 
109
102
  def self.included(receiver)
@@ -30,7 +30,6 @@ module Deas
30
30
  set :show_exceptions, false
31
31
 
32
32
  # custom settings
33
- set :deas_template_scope, server_config.template_scope
34
33
  set :deas_error_procs, server_config.error_procs
35
34
  set :deas_default_charset, server_config.default_charset
36
35
  set :logger, server_config.logger
@@ -1,5 +1,4 @@
1
1
  require 'deas/deas_runner'
2
- require 'deas/template'
3
2
 
4
3
  module Deas
5
4
 
@@ -40,29 +39,9 @@ module Deas
40
39
  @sinatra_call.headers(*args)
41
40
  end
42
41
 
43
- def render(template_name, opts = nil)
42
+ def source_render(source, template_name, locals = nil)
44
43
  self.content_type(get_content_type(template_name)) if self.content_type.nil?
45
-
46
- options = opts || {}
47
- options[:locals] = {
48
- :view => self.handler,
49
- :logger => self.logger
50
- }.merge(options[:locals] || {})
51
- options[:layout] = self.handler_class.layouts if !options.key?(:layout)
52
-
53
- if self.template_source.engine_for?(template_name)
54
- self.template_source.render(template_name, self.handler, options[:locals])
55
- else
56
- Deas::Template.new(@sinatra_call, template_name, options).render
57
- end
58
- end
59
-
60
- def partial(template_name, locals = nil)
61
- if self.template_source.engine_for?(template_name)
62
- self.template_source.partial(template_name, locals || {})
63
- else
64
- Deas::Template::Partial.new(@sinatra_call, template_name, locals).render
65
- end
44
+ super
66
45
  end
67
46
 
68
47
  def send_file(*args, &block)
@@ -13,12 +13,19 @@ module Deas
13
13
 
14
14
  def initialize(path, logger = nil)
15
15
  @path = path.to_s
16
- @default_opts = {
17
- 'source_path' => @path,
18
- 'logger' => logger || Deas::NullLogger.new,
19
- 'deas_template_source' => self
16
+ @default_engine_opts = {
17
+ 'source_path' => @path,
18
+ 'logger' => logger || Deas::NullLogger.new,
19
+ 'default_template_source' => self
20
20
  }
21
- @engines = Hash.new{ |h,k| Deas::NullTemplateEngine.new(@default_opts) }
21
+ @engines = Hash.new{ |h, k| Deas::NullTemplateEngine.new(@default_engine_opts) }
22
+ @ext_cache = Hash.new do |hash, template_name|
23
+ paths = Dir.glob("#{File.join(@path, template_name.to_s)}.*")
24
+ paths = paths.reject{ |p| !@engines.keys.include?(parse_ext(p)) }
25
+ if !(ext = parse_ext(paths.first.to_s)).nil?
26
+ hash[template_name] = ext
27
+ end
28
+ end
22
29
  end
23
30
 
24
31
  def engine(input_ext, engine_class, registered_opts = nil)
@@ -26,12 +33,16 @@ module Deas
26
33
  raise DisallowedEngineExtError, "`#{input_ext}` is disallowed as an"\
27
34
  " engine extension."
28
35
  end
29
- engine_opts = @default_opts.merge(registered_opts || {})
36
+ engine_opts = @default_engine_opts.merge(registered_opts || {})
30
37
  @engines[input_ext.to_s] = engine_class.new(engine_opts)
31
38
  end
32
39
 
33
- def engine_for?(template_name)
34
- @engines.keys.include?(get_template_ext(template_name))
40
+ def engine_for?(ext)
41
+ @engines.keys.include?(ext)
42
+ end
43
+
44
+ def engine_for_template?(template_name)
45
+ self.engine_for?(get_template_ext(template_name))
35
46
  end
36
47
 
37
48
  def render(template_name, view_handler, locals, &content)
@@ -53,9 +64,7 @@ module Deas
53
64
  end
54
65
 
55
66
  def get_template_ext(template_name)
56
- files = Dir.glob("#{File.join(@path, template_name.to_s)}.*")
57
- files = files.reject{ |p| !@engines.keys.include?(parse_ext(p)) }
58
- parse_ext(files.first.to_s || '')
67
+ @ext_cache[template_name]
59
68
  end
60
69
 
61
70
  def parse_ext(template_name)
@@ -75,15 +75,25 @@ module Deas
75
75
  end
76
76
  HeadersArgs = Struct.new(:value)
77
77
 
78
- def render(template_name, options = nil, &block)
79
- RenderArgs.new(template_name, options, block)
78
+ def render(template_name, locals = nil)
79
+ RenderArgs.new(template_name, locals)
80
80
  end
81
- RenderArgs = Struct.new(:template_name, :options, :block)
81
+ RenderArgs = Struct.new(:template_name, :locals)
82
+
83
+ def source_render(source, template_name, locals = nil)
84
+ SourceRenderArgs.new(source, template_name, locals)
85
+ end
86
+ SourceRenderArgs = Struct.new(:source, :template_name, :locals)
82
87
 
83
88
  def partial(template_name, locals = nil)
84
89
  PartialArgs.new(template_name, locals)
85
90
  end
86
- PartialArgs = Struct.new(:template_name, :locals)
91
+ PartialArgs = RenderArgs
92
+
93
+ def source_partial(source, template_name, locals = nil)
94
+ SourcePartialArgs.new(source, template_name, locals)
95
+ end
96
+ SourcePartialArgs = SourceRenderArgs
87
97
 
88
98
  def send_file(file_path, options = nil, &block)
89
99
  SendFileArgs.new(file_path, options, block)
data/lib/deas/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Deas
2
- VERSION = "0.29.0"
2
+ VERSION = "0.30.0"
3
3
  end
@@ -56,9 +56,11 @@ module Deas
56
56
  def status(*args); @deas_runner.status(*args); end
57
57
  def headers(*args); @deas_runner.headers(*args); end
58
58
 
59
- def render(*args, &block); @deas_runner.render(*args, &block); end
60
- def partial(*args, &block); @deas_runner.partial(*args, &block); end
61
- def send_file(*args, &block); @deas_runner.send_file(*args, &block); end
59
+ def render(*args, &block); @deas_runner.render(*args, &block); end
60
+ def source_render(*args, &block); @deas_runner.source_render(*args, &block); end
61
+ def partial(*args, &block); @deas_runner.partial(*args, &block); end
62
+ def source_partial(*args, &block); @deas_runner.source_partial(*args, &block); end
63
+ def send_file(*args, &block); @deas_runner.send_file(*args, &block); end
62
64
 
63
65
  def logger; @deas_runner.logger; end
64
66
  def router; @deas_runner.router; end
@@ -21,7 +21,6 @@ class FakeSinatraCall
21
21
  @template_source = Deas::NullTemplateSource.new
22
22
 
23
23
  @settings = OpenStruct.new({
24
- :deas_template_scope => Deas::Template::Scope,
25
24
  :deas_default_charset => 'utf-8',
26
25
  :logger => @logger,
27
26
  :router => @router,
@@ -41,16 +40,6 @@ class FakeSinatraCall
41
40
  def status(*args); args; end
42
41
  def headers(*args); args; end
43
42
 
44
- # return the template name for each nested calls
45
- def erb(template_name, opts, &block)
46
- if block
47
- RenderArgs.new(template_name, opts, block.call)
48
- else
49
- RenderArgs.new(template_name, opts, nil)
50
- end
51
- end
52
- RenderArgs = Struct.new(:template_name, :opts, :block_call_result)
53
-
54
43
  def send_file(file_path, opts, &block)
55
44
  if block
56
45
  SendFileArgs.new(file_path, opts, block.call)
@@ -32,13 +32,7 @@ class DeasTestServer
32
32
  post '/session', 'SetSessionHandler'
33
33
  get '/session', 'UseSessionHandler'
34
34
 
35
- get '/with_layout', 'WithLayoutHandler'
36
- get '/haml_with_layout', 'HamlWithLayoutHandler'
37
- get '/with_haml_layout', 'WithHamlLayoutHandler'
38
- get '/haml_with_haml_layout', 'HamlWithHamlLayoutHandler'
39
- get '/partial.html', 'PartialHandler'
40
-
41
- get '/handler/tests.json', 'HandlerTestsHandler'
35
+ get '/handler/tests', 'HandlerTestsHandler'
42
36
 
43
37
  redirect '/route_redirect', '/somewhere'
44
38
  redirect('/:prefix/redirect'){ "/#{params['prefix']}/somewhere" }
@@ -72,7 +66,7 @@ class ShowHandler
72
66
  end
73
67
 
74
68
  def run!
75
- render 'show'
69
+ @message
76
70
  end
77
71
 
78
72
  end
@@ -96,7 +90,6 @@ class ShowLatinJsonHandler
96
90
 
97
91
  def run!
98
92
  content_type :json, :charset => 'latin1'
99
- render 'show_json'
100
93
  end
101
94
 
102
95
  end
@@ -106,7 +99,7 @@ class ShowTextHandler
106
99
 
107
100
  def run!
108
101
  hdrs = {'Content-Type' => 'text/plain'}
109
- halt 200, hdrs, render('show.json')
102
+ halt 200, hdrs, ''
110
103
  end
111
104
 
112
105
  end
@@ -139,53 +132,6 @@ class ErrorHandler
139
132
 
140
133
  end
141
134
 
142
- class WithLayoutHandler
143
- include Deas::ViewHandler
144
- layouts 'layout1', 'layout2', 'layout3'
145
-
146
- def run!
147
- render 'with_layout'
148
- end
149
-
150
- end
151
-
152
- class HamlWithLayoutHandler
153
- include Deas::ViewHandler
154
- layouts 'layout1'
155
-
156
- def run!
157
- render 'haml_with_layout'
158
- end
159
-
160
- end
161
-
162
- class WithHamlLayoutHandler
163
- include Deas::ViewHandler
164
- layouts 'haml_layout1'
165
-
166
- def run!
167
- render 'with_layout'
168
- end
169
-
170
- end
171
-
172
- class HamlWithHamlLayoutHandler
173
- include Deas::ViewHandler
174
- layouts 'haml_layout1'
175
-
176
- def run!
177
- render 'haml_with_layout'
178
- end
179
-
180
- end
181
-
182
- class PartialHandler
183
- include Deas::ViewHandler
184
-
185
- def run!; partial '_info', :info => 'some-info'; end
186
-
187
- end
188
-
189
135
  class RedirectHandler
190
136
  include Deas::ViewHandler
191
137
 
@@ -234,8 +180,7 @@ class HandlerTestsHandler
234
180
  end
235
181
 
236
182
  def run!
237
- require 'multi_json'
238
- [200, {}, MultiJson.encode(@data)]
183
+ [200, {}, @data.inspect]
239
184
  end
240
185
 
241
186
  end
@@ -1 +1 @@
1
- ERB Template Message: <%= view.message %>
1
+ This is an erb template for use in template source/engine tests.
@@ -1 +1 @@
1
- This is a json template for use in template engine tests.
1
+ This is a json template for use in template source/engine tests.
@@ -1,3 +1,4 @@
1
+ require 'deas/template_source'
1
2
  require 'deas/view_handler'
2
3
 
3
4
  class EmptyViewHandler
@@ -38,7 +39,16 @@ class RenderViewHandler
38
39
  include Deas::ViewHandler
39
40
 
40
41
  def run!
41
- render "my_template", :some => :option
42
+ render "my_template", :some => 'local'
43
+ end
44
+ end
45
+
46
+ class SourceRenderViewHandler
47
+ include Deas::ViewHandler
48
+
49
+ def run!
50
+ source = Deas::TemplateSource.new(Factory.path)
51
+ source_render source, "my_template", :some => 'local'
42
52
  end
43
53
  end
44
54
 
@@ -46,7 +56,16 @@ class PartialViewHandler
46
56
  include Deas::ViewHandler
47
57
 
48
58
  def run!
49
- partial "my_partial", :some => 'locals'
59
+ partial "my_partial", :some => 'local'
60
+ end
61
+ end
62
+
63
+ class SourcePartialViewHandler
64
+ include Deas::ViewHandler
65
+
66
+ def run!
67
+ source = Deas::TemplateSource.new(Factory.path)
68
+ source_partial source, "my_partial", :some => 'local'
50
69
  end
51
70
  end
52
71
 
@@ -19,10 +19,9 @@ module Deas
19
19
  should "return a 200 response with a GET to '/show'" do
20
20
  get '/show', 'message' => 'this is a test'
21
21
 
22
- expected_body = "show page: this is a test\n" \
23
- "Stuff: Show Info\n"
24
- assert_equal 200, last_response.status
25
- assert_equal expected_body, last_response.body
22
+ assert_equal 200, last_response.status
23
+ exp = "this is a test"
24
+ assert_equal exp, last_response.body
26
25
  end
27
26
 
28
27
  should "set the content type appropriately" do
@@ -67,37 +66,6 @@ module Deas
67
66
  assert_equal "Oops, something went wrong", last_response.body
68
67
  end
69
68
 
70
- should "render erb templates using layouts" do
71
- get '/with_layout'
72
- expected_body = "Layout 1\nLayout 2\nLayout 3\nWith Layouts View: WithLayoutHandler\n"
73
- assert_equal 200, last_response.status
74
- assert_equal expected_body, last_response.body
75
- end
76
-
77
- should "render mixed (erb and other) templates using layouts" do
78
- get '/haml_with_layout'
79
- expected_body = "Layout 1\n<span>HamlWithLayoutHandler</span>\n"
80
- assert_equal 200, last_response.status
81
- assert_equal expected_body, last_response.body
82
-
83
- get '/with_haml_layout'
84
- expected_body = "Layout 1\nWith Layouts View: WithHamlLayoutHandler\n"
85
- assert_equal 200, last_response.status
86
- assert_equal expected_body, last_response.body
87
-
88
- get '/haml_with_haml_layout'
89
- expected_body = "Layout 1\n<span>HamlWithHamlLayoutHandler</span>\n"
90
- assert_equal 200, last_response.status
91
- assert_equal expected_body, last_response.body
92
- end
93
-
94
- should "render partial templates" do
95
- get '/partial.html'
96
- expected_body = "Stuff: some-info\n"
97
- assert_equal 200, last_response.status
98
- assert_equal expected_body, last_response.body
99
- end
100
-
101
69
  should "return a 302 redirecting to the expected locations" do
102
70
  get '/redirect'
103
71
  expected_location = 'http://google.com'
@@ -146,18 +114,20 @@ module Deas
146
114
  class HandlerTests < RackTests
147
115
  desc "handler"
148
116
  setup do
149
- get 'handler/tests.json?a-param=something'
117
+ get 'handler/tests?a-param=something'
150
118
 
151
- require 'multi_json'
152
- @data = MultiJson.decode(last_response.body || "")
119
+ @data_inspect = last_response.body
153
120
  end
154
121
 
155
122
  should "be able to access sinatra call data" do
156
- assert_equal 'Logger', @data['logger_class_name']
157
- assert_equal 'GET', @data['request_method']
158
- assert_equal 'Content-Type', @data['response_firstheaderval']
159
- assert_equal 'something', @data['params_a_param']
160
- assert_equal '{}', @data['session_inspect']
123
+ exp = {
124
+ 'logger_class_name' => 'Logger',
125
+ 'request_method' => 'GET',
126
+ 'response_firstheaderval' => 'Content-Type',
127
+ 'params_a_param' => 'something',
128
+ 'session_inspect' => '{}'
129
+ }
130
+ assert_equal exp.inspect, @data_inspect
161
131
  end
162
132
 
163
133
  end