scorched 0.8 → 0.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9e853e2cdf6c23a10cebb3ada3d26cc90a0535b7
4
- data.tar.gz: 2cfe230e619c9ad1f0304c35699ed55cdcc9e94b
3
+ metadata.gz: 295ef9e64b632d7c0d594d7faa770abe1a276306
4
+ data.tar.gz: 77d884750ab7dd5946e124963a0f818141074a6c
5
5
  SHA512:
6
- metadata.gz: 236012f2a5d523733f2fe8d81389d545d9bbb01a421f2840371e31f7f4b81ccf608cd7ba06838f0291a5bb0796271bd6e1e10de6b0a61301168b4c2865c52dbf
7
- data.tar.gz: 2e531bf2da9a00ea4efedf2eaa18d2718d6c7bebc72cf47176f48ac6f4b35c2722f5974b4d72346e5d4d8375cfc4f091fc34af46d2d1165b9c762318ce941f0c
6
+ metadata.gz: a22df1cc5accdf2ef58b0a9135726d5379b74b55ecd5ed6d301c5e7adf55e14f5905c400411d12263f65552cbf32792d0f1400397fc6be260831536033f52f09
7
+ data.tar.gz: 426055e514e3ec9a4a7a13c2dccb692a914a38d054630a4f39b9df0a1d973520850571491741d3dc3f2a8aab363d62c283b4fac0a76140e1b9433bd825564eff
@@ -3,6 +3,15 @@ Milestones
3
3
 
4
4
  Changelog
5
5
  ---------
6
+ ### v0.9
7
+ * Refactored `render` method:
8
+ * All Scorched options are now keyword arguments, including `:locals` which was added as a proper render option.
9
+ * Scorched options are no longer passed through to Tilt.
10
+ * `:tilt` option added to allow options to be passed directly to Tilt, such as `:engine`
11
+ * Unrecognised options are still passed through to Tilt for convenience.
12
+ * Added template caching using Tilt::Cache.
13
+ * Added `:cache_templates` config option. Defaults to true except for development.
14
+
6
15
  ### v0.8
7
16
  * Changed `controller` method signature to accept an optional URL pattern as the first argument.
8
17
  * Implemented a pass mechanism to short-circuit out of the current match and invoke the next match.
@@ -18,7 +27,7 @@ Changelog
18
27
  * `absolute`ethod now returns forward slash if script name is empty.
19
28
 
20
29
  ### v0.6
21
- * `view_config`ptions hash renamed to ` `render_defaults`ch better reflects its function.
30
+ * `view_config` options hash renamed to ` `render_defaults`ch better reflects its function.
22
31
 
23
32
  ### v0.5.2
24
33
  * Minor modification to routing to make it behave as a documented regarding matching at the directly before or on a path.
@@ -64,8 +73,8 @@ To Do
64
73
  -----
65
74
  Some of these remaining features may be reconsidered and either left out, or put into some kind of contrib library.
66
75
 
76
+ * If one or more matches are found, but their conditions don't pass, a 403 should be returned instead of a 404.
67
77
  * Make specs for Collection and Options classes more thorough, e.g. test all non-reading modifiers such as clear, delete, etc.
68
- * Implement template caching with option to disable/enable (disabled by default in development)
69
78
  * Add more view helpers, maybe?
70
79
  * Add helper to easily read and build HTTP query strings. Takes care of "?" and "&" logic, escaping, etc. This is
71
80
  intended to make link building easier.
data/README.md CHANGED
@@ -110,3 +110,12 @@ This API shouldn't look too foreign to anyone familiar with frameworks like Sina
110
110
  Development Progress
111
111
  --------------------
112
112
  Please refer to [Milestones.md](Milestones.md) for a breakdown of development progress.
113
+
114
+
115
+ Links
116
+ -----
117
+ * [Website](http://scorchedrb.com)
118
+ * [Online API Reference](http://rubydoc.info/gems/scorched)
119
+ * [GitHub Project](http://github.com/wardrop/Scorched)
120
+ * [Issue Tracker](http://github.com/wardrop/Scorched/issues)
121
+ * [Discussion/Mailing List](https://groups.google.com/d/forum/scorched)
@@ -119,22 +119,43 @@ end
119
119
 
120
120
  The controller helper takes an optional URL pattern as it's first argument, an optional parent class as it's second, and finally a mapping hash as its third optional argument, where you can define a priority, conditions, or override the URL pattern. Of course, the `controller` helper takes a block as well, which defines the body of the new controller class.
121
121
 
122
- The optional URL pattern defaults to `'/'` which can be handy for grouping a set of routes.
122
+ The optional URL pattern defaults to `'/'` which means it's essentially a match-all mapping. In addition, the generated controller has `:auto_pass` set to `true` by default (refer to configuration documentation for more information). This is a handy combination for grouping a set of routes in their own scope, with their own methods, filters, configuration, etc.
123
123
 
124
124
  ``` ruby
125
125
  class MyApp < Scorched::Controller
126
126
  get '/' do
127
- bold "Hello there"
127
+ format "Hello there"
128
+ end
129
+
130
+ controller do
131
+ before { response['Content-Type'] = 'text/plain' }
132
+
133
+ get '/hello' do
134
+ 'Hello'
135
+ end
136
+
137
+ def emphasise(str)
138
+ "**#{str}**"
139
+ end
128
140
  end
129
141
 
130
- controller conditions: {media_type: 'application/json'} do
131
-
142
+ get '/goodbye' do
143
+ 'Goodbye'
144
+ end
145
+
146
+ after { response.body = emphasise(response.body.join) }
147
+
148
+ def emphasise(str)
149
+ "<strong>#{str}</strong>"
132
150
  end
133
151
  end
134
152
  ```
135
153
 
154
+ That example, while serving no practical purpose, hopefully demonstrates how you can combine various constructs with sub-controllers, to come up with DRY creative solutions.
155
+
156
+
136
157
  The Root Controller
137
158
  -------------------
138
- Although you will likely have a main controller to serve as the target for Rack, Scorched does not have the concept of a root controller. It mades no differentiation between a sub-controller and any other controller. All Controllers are made equal.
159
+ Although you will likely have a main controller to serve as the target for Rack, Scorched does not have the concept of a root controller. It makes no differentiation between a sub-controller and any other controller. All Controllers are made equal.
139
160
 
140
- You can arrange and nest your controllers in any way, shape or form. Scorched has been designed to not make any assumptions about how you structure your controllers, which can accommodate very creative solutions.
161
+ You can arrange and nest your controllers in any way, shape or form. Scorched has been designed to not make any assumptions about how you structure your controllers, which again, can accommodate creative solutions.
@@ -8,7 +8,7 @@ There are two sets of configurables. Those which apply to views, and everything
8
8
  Options
9
9
  -------
10
10
 
11
- Each configuration is listed below, with the default value of each included. Note, environment specific defaults may override the default values below.
11
+ Each configuration is listed below, with the default value of each included. Note, development environment may override the default values below.
12
12
 
13
13
  * `config[:strip_trailing_slash] = :redirect`
14
14
  Controls how trailing forward slashes in requests are handled.
@@ -17,8 +17,9 @@ Each configuration is listed below, with the default value of each included. Not
17
17
  * `false` - Does nothing. Respects the presence of a trailing forward flash.
18
18
  * `config[:static_dir] = 'public'`
19
19
  The directory Scorched should serve static files from. Should be set to false if the web server or some other middleware is serving static files.
20
- * `config[:logger] = Logger.new(STDOUT)` - Currently does nothing until logging is added to Scorched.
20
+ * `config[:logger] = false` - Is currently only used for Rack::Logger.
21
21
  * `config[:auto_pass] = false` - If no routes within the current controller match, automatically _pass_ the request back to the outer controller without running any filters. This makes sub-controllers behave more like a kind-of route group.
22
+ * `config[:cache_templates] = true` - If true, caches compiled templates using Tilt::Cache.
22
23
 
23
24
  You can also configure the default options when rendering views by setting them on the `render_defaults` hash. The options specified here are merged with those provided when calling the `render` method, with the explicit options obviously taking precedence over the defaults.
24
25
 
@@ -0,0 +1,8 @@
1
+ Running a Live Console
2
+ ======================
3
+
4
+ Scorched does not include a console like Rails or Merb does, but only because it doesn't need to. Multiple solutions already exist, including simply loading your application within IRB or Pry.
5
+
6
+ First, I suggest you try [Racksh](https://github.com/sickill/racksh). It's a very handy yet simple Rack shell implementation that should meet most of your expectations. You can install _racksh_ as a gem with `gem install racksh`. I suggest you read the README in the projects Github repository to get started.
7
+
8
+ Another option for those running Phusion Passenger Enterprise, is the live IRB and debugging console. An introductory video can be found here: http://vimeo.com/45923773
@@ -1,4 +1,6 @@
1
1
  module Scorched
2
+ TemplateCache = Tilt::Cache.new
3
+
2
4
  class Controller
3
5
  include Scorched::Options('config')
4
6
  include Scorched::Options('render_defaults')
@@ -10,23 +12,26 @@ module Scorched
10
12
 
11
13
  config << {
12
14
  :strip_trailing_slash => :redirect, # :redirect => Strips and redirects URL ending in forward slash, :ignore => internally ignores trailing slash, false => does nothing.
13
- :static_dir => 'public', # The directory Scorched should serve static files from. Set to false if web server or anything else is serving static files.
15
+ :static_dir => false, # The directory Scorched should serve static files from. Set to false if web server or anything else is serving static files.
14
16
  :logger => nil,
15
17
  :show_exceptions => false,
16
18
  :auto_pass => false, # Automatically _pass_ request back to outer controller if no route matches.
19
+ :cache_templates => true
17
20
  }
18
21
 
19
22
  render_defaults << {
20
23
  :dir => 'views', # The directory containing all the view templates, relative to the current working directory.
21
24
  :layout => false, # The default layout template to use, relative to the view directory. Set to false for no default layout.
22
- :engine => :erb
25
+ :engine => :erb,
26
+ :locals => {},
27
+ :tilt => {}, # Options intended for Tilt. This gets around potentialkey name conflicts between Scorched and the renderer invoked by Tilt. For example, if you had to specify an `:engine` for the renderer, this allows you to do that without.
23
28
  }
24
29
 
25
30
  if ENV['RACK_ENV'] == 'development'
26
31
  config[:logger] = Logger.new(STDOUT)
27
32
  config[:show_exceptions] = true
28
- else
29
- config[:static_dir] = false
33
+ config[:static_dir] = 'public'
34
+ config[:cache_templates] = false
30
35
  end
31
36
 
32
37
  conditions << {
@@ -325,8 +330,9 @@ module Scorched
325
330
  env['scorched.flash'].each { |k,v| session[k] = v } if session && env['scorched.flash']
326
331
  end
327
332
 
328
- # Serves a thin layer of convenience to Rack's built-in methods: Request#cookies, Response#set_cookie, and
333
+ # Serves as a thin layer of convenience to Rack's built-in methods: Request#cookies, Response#set_cookie, and
329
334
  # Response#delete_cookie.
335
+ #
330
336
  # If only one argument is given, the specified cookie is retreived and returned.
331
337
  # If both arguments are supplied, the cookie is either set or deleted, depending on whether the second argument is
332
338
  # nil, or otherwise is a hash containing the key/value pair ``:value => nil``.
@@ -346,35 +352,53 @@ module Scorched
346
352
  end
347
353
 
348
354
  # Renders the given string or file path using the Tilt templating library.
349
- # The options hash is merged with the controllers _render_defaults_. Unrecognised options are passed through to Tilt.
350
- # The template engine is derived from file name, or otherwise as specified by the _:engine_ option. If a string is
351
- # given, the _:engine_ option must be set.
355
+ # Each option defaults to the corresponding value defined in _render_defaults_ attribute. Unrecognised options are
356
+ # passed through to Tilt, but a `:tilt` option is also provided for passing options directly to Tilt.
357
+ # The template engine is derived from the file name, or otherwise as specified by the _:engine_ option. If a string
358
+ # is given, the _:engine_ option must be set.
352
359
  #
353
- # Refer to Tilt documentation for a list of valid template engines.
354
- def render(string_or_file, options = {}, &block)
355
- options = render_defaults.merge(explicit_options = options)
356
- engine = (derived_engine = Tilt[string_or_file.to_s]) || Tilt[options[:engine]]
357
- raise Error, "Invalid or undefined template engine: #{options[:engine].inspect}" unless engine
358
- if Symbol === string_or_file
360
+ # Refer to Tilt documentation for a list of valid template engines and Tilt options.
361
+ def render(
362
+ string_or_file,
363
+ dir: render_defaults[:dir],
364
+ layout: @_no_default_layout ? nil : render_defaults[:layout],
365
+ engine: render_defaults[:engine],
366
+ locals: render_defaults[:locals],
367
+ tilt: render_defaults[:tilt],
368
+ **options,
369
+ &block
370
+ )
371
+ template_cache = config[:cache_templates] ? TemplateCache : Tilt::Cache.new
372
+ tilt_options = options.merge(tilt || {})
373
+ tilt_engine = (derived_engine = Tilt[string_or_file.to_s]) || Tilt[engine]
374
+ raise Error, "Invalid or undefined template engine: #{engine.inspect}" unless tilt_engine
375
+ template = if Symbol === string_or_file
359
376
  file = string_or_file.to_s
360
- file = file << ".#{options[:engine]}" unless derived_engine
361
- file = File.join(options[:dir], file) if options[:dir]
377
+ file = file << ".#{engine}" unless derived_engine
378
+ file = File.join(dir, file) if dir
362
379
  # Tilt still has unresolved file encoding issues. Until that's fixed, we read the file manually.
363
- template = engine.new(nil, nil, options) { File.read(file) }
380
+ template_cache.fetch(:file, tilt_engine, file, tilt_options) do
381
+ tilt_engine.new(nil, nil, tilt_options) { File.read(file) }
382
+ end
364
383
  else
365
- template = engine.new(nil, nil, options) { string_or_file }
384
+ template_cache.fetch(:string, tilt_engine, string_or_file, tilt_options) do
385
+ tilt_engine.new(nil, nil, tilt_options) { string_or_file }
386
+ end
366
387
  end
367
-
388
+
368
389
  # The following chunk of code is responsible for preventing the rendering of layouts within views.
369
- options[:layout] = false if @_no_default_layout && !explicit_options[:layout]
370
390
  begin
371
391
  @_no_default_layout = true
372
- output = template.render(self, options[:locals], &block)
392
+ output = template.render(self, locals, &block)
373
393
  ensure
374
394
  @_no_default_layout = false
375
395
  end
376
- output = render(options[:layout], options.merge(layout: false)) { output } if options[:layout]
377
- output
396
+
397
+ if layout
398
+ render(layout, dir: dir, layout: false, engine: engine, locals: locals, tilt: tilt, **options) { output }
399
+ else
400
+ output
401
+ end
378
402
  end
379
403
 
380
404
  # Takes an optional URL, relative to the applications root, and returns a fully qualified URL.
@@ -1,3 +1,3 @@
1
1
  module Scorched
2
- VERSION = '0.8'
2
+ VERSION = '0.9'
3
3
  end
@@ -525,7 +525,7 @@ module Scorched
525
525
  end
526
526
 
527
527
  describe "configuration" do
528
- describe "strip_trailing_slash" do
528
+ describe :strip_trailing_slash do
529
529
  it "can be set to strip trailing slash and redirect" do
530
530
  app.config[:strip_trailing_slash] = :redirect
531
531
  app.get('/test') { }
@@ -552,7 +552,7 @@ module Scorched
552
552
  end
553
553
  end
554
554
 
555
- describe "static_dir" do
555
+ describe :static_dir do
556
556
  it "can serve static file from the specific directory" do
557
557
  app.config[:static_dir] = 'public'
558
558
  response = rt.get('/static.txt')
@@ -567,7 +567,7 @@ module Scorched
567
567
  end
568
568
  end
569
569
 
570
- describe "show_exceptions" do
570
+ describe :show_exceptions do
571
571
  it "shows debug-friendly error page for unhandled exceptions" do
572
572
  app.config[:show_exceptions] = true
573
573
  app.get('/') { raise RuntimeError, "Kablamo!" }
@@ -585,8 +585,8 @@ module Scorched
585
585
  end
586
586
  end
587
587
 
588
- describe "auto_pass" do
589
- it "if no match, passes to the outer controller without running any filters" do
588
+ describe :auto_pass do
589
+ it "passes to the outer controller without running any filters, if no match" do
590
590
  sub = Class.new(Scorched::Controller) do
591
591
  config[:auto_pass] = true
592
592
  before { response.status = 600 }
@@ -605,6 +605,28 @@ module Scorched
605
605
  rt.get('/').status.should == 600
606
606
  end
607
607
  end
608
+
609
+ describe :cache_templates do
610
+ before(:each) do
611
+ File.open('views/temp.str', 'w') { |f| f.write 'hello world' }
612
+ end
613
+
614
+ it "can cache templates" do
615
+ app.config[:cache_templates] = true
616
+ app.get('/') { render :'temp.str' }
617
+ rt.get('/').body.should == 'hello world'
618
+ File.open('views/temp.str', 'a') { |f| f.write '!!!' }
619
+ rt.get('/').body.should == 'hello world'
620
+ end
621
+
622
+ it "can be set not to cache templates" do
623
+ app.config[:cache_templates] = false
624
+ app.get('/') { render :'temp.str' }
625
+ rt.get('/').body.should == 'hello world'
626
+ File.open('views/temp.str', 'a') { |f| f.write '!!!' }
627
+ rt.get('/').body.should == 'hello world!!!'
628
+ end
629
+ end
608
630
  end
609
631
 
610
632
  describe "sessions" do
@@ -766,6 +788,35 @@ module Scorched
766
788
  end
767
789
  rt.get('/').body.should == '({1 for none})'
768
790
  end
791
+
792
+ it "can pass local variables through to view" do
793
+ app.get '/' do
794
+ render '<%= var %>', engine: 'erb', dir: 'views', locals: {var: 'hello sailor'}
795
+ end
796
+ rt.get('/').body.should == 'hello sailor'
797
+ end
798
+
799
+ it "provides a means for passing options directly to tilt" do
800
+ Tilt.register(Class.new(Tilt::ERBTemplate) do
801
+ def prepare
802
+ options[:engine].new if options[:engine]
803
+ super
804
+ end
805
+ end, 'test')
806
+
807
+ app.get '/safe' do
808
+ render '<%= var %>', engine: 'test', dir: 'views', locals: {var: 'hello sailor'}
809
+ render '<%= var %>', engine: 'test', dir: 'views', locals: {var: 'hello sailor'}, tilt: {engine: Class.new}
810
+ end
811
+ rt.get('/safe').body.should == 'hello sailor'
812
+
813
+ app.get '/boomer' do
814
+ render '<%= var %>', engine: 'test', dir: 'views', locals: {var: 'hello sailor'}, tilt: {engine: 'invalid'}
815
+ end
816
+ expect {
817
+ rt.get('/boomer')
818
+ }.to raise_error(NoMethodError)
819
+ end
769
820
  end
770
821
 
771
822
  describe "url helpers" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scorched
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.8'
4
+ version: '0.9'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Wardrop
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-27 00:00:00.000000000 Z
11
+ date: 2013-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -103,6 +103,7 @@ files:
103
103
  - docs/02_fundamentals/09_sharing_request_state.md
104
104
  - docs/03_further_reading/be_creative.md
105
105
  - docs/03_further_reading/code_reloading.md
106
+ - docs/03_further_reading/live_console.md
106
107
  - docs/03_further_reading/running_unit_tests.md
107
108
  - examples/file_upload.ru
108
109
  - examples/media_types.ru