sinatra-contrib 1.3.1 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -127,7 +127,7 @@ All extensions:
127
127
 
128
128
  ``` ruby
129
129
  require 'sinatra/base'
130
- require 'sinatra/contrib'
130
+ require 'sinatra/contrib/all'
131
131
 
132
132
  class MyApp < Sinatra::Base
133
133
  register Sinatra::Contrib
@@ -46,7 +46,7 @@ module Sinatra
46
46
  # require "sinatra/content_for"
47
47
  #
48
48
  # class MyApp < Sinatra::Base
49
- # register Sinatra::ContentFor
49
+ # helpers Sinatra::ContentFor
50
50
  #
51
51
  # # The rest of your modular application code goes here...
52
52
  # end
@@ -78,6 +78,20 @@ module Sinatra
78
78
  def content_for(key, &block)
79
79
  content_blocks[key.to_sym] << capture_later(&block)
80
80
  end
81
+
82
+ # Check if a block of content with the given key was defined. For
83
+ # example:
84
+ #
85
+ # <% content_for :head do %>
86
+ # <script type="text/javascript" src="/foo.js"></script>
87
+ # <% end %>
88
+ #
89
+ # <% if content_for? :head %>
90
+ # <span>content "head" was defined.</span>
91
+ # <% end %>
92
+ def content_for?(key)
93
+ content_blocks[key.to_sym].any?
94
+ end
81
95
 
82
96
  # Render the captured blocks for a given key. For example:
83
97
  #
@@ -45,7 +45,7 @@ module Sinatra
45
45
  # the application you will use them:
46
46
  #
47
47
  # require "sinatra/base"
48
- # require "sinatra/link_header"
48
+ # require "sinatra/cookies"
49
49
  #
50
50
  # class MyApp < Sinatra::Base
51
51
  # helpers Sinatra::Cookies
@@ -20,7 +20,7 @@ module Sinatra
20
20
  # You can define a namespace by a path prefix:
21
21
  #
22
22
  # namespace '/blog' do
23
- # get() { haml :blog }
23
+ # get { haml :blog }
24
24
  # get '/:entry_permalink' do
25
25
  # @entry = Entry.find_by_permalink!(params[:entry_permalink])
26
26
  # haml :entry
@@ -118,7 +118,7 @@ module Sinatra
118
118
  Module.new do
119
119
  extend NamespacedMethods
120
120
  include InstanceMethods
121
- @base, @extensions = base, []
121
+ @base, @extensions, @errors = base, [], {}
122
122
  @pattern, @conditions = compile(pattern, conditions)
123
123
  @templates = Hash.new { |h,k| @base.templates[k] }
124
124
  namespace = self
@@ -135,14 +135,6 @@ module Sinatra
135
135
  def template_cache
136
136
  super.fetch(:nested, @namespace) { Tilt::Cache.new }
137
137
  end
138
-
139
- def error_block!(*keys)
140
- if block = keys.inject(nil) { |b,k| b ||= @namespace.errors[k] }
141
- instance_eval(&block)
142
- else
143
- super
144
- end
145
- end
146
138
  end
147
139
 
148
140
  module SharedMethods
@@ -180,16 +172,23 @@ module Sinatra
180
172
  @extensions.each { |e| e.send(name, *args) if e.respond_to?(name) }
181
173
  end
182
174
 
183
- def errors
184
- @errors ||= {}
185
- end
186
-
187
175
  def not_found(&block)
188
176
  error(404, &block)
189
177
  end
190
178
 
191
- def error(codes = Exception, &block)
192
- [*codes].each { |c| errors[c] = block }
179
+ def errors
180
+ base.errors.merge(@errors)
181
+ end
182
+
183
+ def namespace_errors
184
+ @errors
185
+ end
186
+
187
+ def error(*codes, &block)
188
+ args = Sinatra::Base.send(:compile!, "ERROR", /^#{@pattern}/, block)
189
+ codes = codes.map { |c| Array(c) }.flatten
190
+ codes << Exception if codes.empty?
191
+ codes.each { |c| @errors[c] = args }
193
192
  end
194
193
 
195
194
  def respond_to(*args)
@@ -210,6 +210,7 @@ module Sinatra
210
210
  end
211
211
  end
212
212
  end
213
+ klass.set(:inline_templates, klass.app_file) if klass == Sinatra::Application
213
214
  end
214
215
 
215
216
  # Reloads the modified files, adding, updating and removing the
@@ -231,6 +232,15 @@ module Sinatra
231
232
 
232
233
  # Contains the methods defined in Sinatra::Base that are overriden.
233
234
  module BaseMethods
235
+ # Protects Sinatra::Base.run! from being called more than once.
236
+ def run!(*args)
237
+ if settings.reloader?
238
+ super unless running?
239
+ else
240
+ super
241
+ end
242
+ end
243
+
234
244
  # Does everything Sinatra::Base#route does, but it also tells the
235
245
  # +Watcher::List+ for the Sinatra application to watch the defined
236
246
  # route.
@@ -32,7 +32,7 @@ module Sinatra
32
32
  # list = []
33
33
  #
34
34
  # get '/' do
35
- # stream(false) do |out|
35
+ # stream(:keep_open) do |out|
36
36
  # list << out
37
37
  # out.callback { list.delete out }
38
38
  # out.errback do
@@ -91,7 +91,7 @@ module Sinatra
91
91
  # require "sinatra/streaming"
92
92
  #
93
93
  # class MyApp < Sinatra::Base
94
- # helpers Streaming
94
+ # helpers Sinatra::Streaming
95
95
  # end
96
96
  module Streaming
97
97
  def stream(*)
@@ -1,7 +1,7 @@
1
1
  # Run `rake sinatra-contrib.gemspec` to update the gemspec.
2
2
  Gem::Specification.new do |s|
3
3
  s.name = "sinatra-contrib"
4
- s.version = "1.3.1"
4
+ s.version = "1.3.2"
5
5
  s.description = "Collection of useful Sinatra extensions"
6
6
  s.homepage = "http://github.com/sinatra/sinatra-contrib"
7
7
  s.summary = s.description
@@ -10,10 +10,17 @@ Gem::Specification.new do |s|
10
10
  s.authors = [
11
11
  "Konstantin Haase",
12
12
  "Gabriel Andretta",
13
+ "Trevor Bramble",
13
14
  "Nicolas Sanguinetti",
14
- "Eliot Shepard",
15
+ "Ilya Shindyapin",
16
+ "Masahiro Fujiwara",
17
+ "Adrian Pacała",
15
18
  "Andrew Crump",
19
+ "Eliot Shepard",
20
+ "Eric Marden",
21
+ "Gray Manley",
16
22
  "Matt Lyon",
23
+ "lest",
17
24
  "undr"
18
25
  ]
19
26
 
@@ -21,10 +28,17 @@ Gem::Specification.new do |s|
21
28
  s.email = [
22
29
  "konstantin.mailinglists@googlemail.com",
23
30
  "ohhgabriel@gmail.com",
31
+ "inbox@trevorbramble.com",
24
32
  "contacto@nicolassanguinetti.info",
25
- "eshepard@slower.net",
33
+ "ilya@shindyapin.com",
34
+ "m-fujiwara@axsh.net",
35
+ "altpacala@gmail.com",
26
36
  "andrew.crump@ieee.org",
37
+ "eshepard@slower.net",
38
+ "eric.marden@gmail.com",
39
+ "g.manley@tukaiz.com",
27
40
  "matt@flowerpowered.com",
41
+ "just.lest@gmail.com",
28
42
  "undr@yandex.ru"
29
43
  ]
30
44
 
@@ -65,6 +79,10 @@ Gem::Specification.new do |s|
65
79
  "spec/content_for/different_key.erubis",
66
80
  "spec/content_for/different_key.haml",
67
81
  "spec/content_for/different_key.slim",
82
+ "spec/content_for/footer.erb",
83
+ "spec/content_for/footer.erubis",
84
+ "spec/content_for/footer.haml",
85
+ "spec/content_for/footer.slim",
68
86
  "spec/content_for/layout.erb",
69
87
  "spec/content_for/layout.erubis",
70
88
  "spec/content_for/layout.haml",
@@ -0,0 +1,3 @@
1
+ <% if content_for? :foo %>
2
+ <%= yield_content :foo %>
3
+ <% end %>
@@ -0,0 +1,3 @@
1
+ <% if content_for? :foo %>
2
+ <%= yield_content :foo %>
3
+ <% end %>
@@ -0,0 +1,2 @@
1
+ - if content_for? :foo
2
+ = yield_content :foo
@@ -0,0 +1,2 @@
1
+ - if content_for? :foo
2
+ = yield_content :foo
@@ -138,6 +138,18 @@ describe Sinatra::ContentFor do
138
138
  render(inner, :passes_values).should == "<i>1</i>2"
139
139
  end
140
140
  end
141
+
142
+ describe "with content_for? in Ruby" do
143
+ it 'renders block if key is set' do
144
+ content_for(:foo) { "foot" }
145
+ render(inner, :footer).should == "foot"
146
+ end
147
+
148
+ it 'does not render a block if different key' do
149
+ content_for(:different_key) { "foot" }
150
+ render(inner, :footer).should be_empty
151
+ end
152
+ end
141
153
 
142
154
  engines.each do |outer|
143
155
  describe "with yield_content in #{outer.capitalize}" do
@@ -487,6 +487,48 @@ describe Sinatra::Namespace do
487
487
  get('/de/foo').status.should == 404
488
488
  last_response.body.should == 'nicht gefunden' unless verb == :head
489
489
  end
490
+
491
+ it "should handle custom errors in base. Issue #37." do
492
+ mock_app {
493
+ error(404) { 'not found...' }
494
+ namespace('/en') do
495
+ end
496
+ namespace('/de') do
497
+ error(404) { 'nicht gefunden' }
498
+ end
499
+ }
500
+ send(verb, '/foo').status.should == 404
501
+ last_response.body.should == 'not found...' unless verb == :head
502
+ get('/en/foo').status.should == 404
503
+ last_response.body.should == 'not found...' unless verb == :head
504
+ get('/de/foo').status.should == 404
505
+ last_response.body.should == 'nicht gefunden' unless verb == :head
506
+ end
507
+
508
+ it "should allow custom error handlers with Exception class. Issue #37." do
509
+ mock_app {
510
+ class AError < StandardError; end
511
+ class BError < AError; end
512
+
513
+ error(AError) { body('auth failed'); 401}
514
+ namespace('/en') do
515
+ get '/foo' do
516
+ raise BError
517
+ end
518
+ end
519
+ namespace('/de') do
520
+ error(AError) { body('methode nicht erlaubt'); 406}
521
+
522
+ get '/foo' do
523
+ raise BError
524
+ end
525
+ end
526
+ }
527
+ get('/en/foo').status.should == 401
528
+ last_response.body.should == 'auth failed' unless verb == :head
529
+ get('/de/foo').status.should == 406
530
+ last_response.body.should == 'methode nicht erlaubt' unless verb == :head
531
+ end
490
532
  end
491
533
 
492
534
  describe 'templates' do
@@ -11,14 +11,14 @@ describe Sinatra::Reloader do
11
11
  # Returns the path of the Sinatra application file created by
12
12
  # +setup_example_app+.
13
13
  def app_file_path
14
- File.join(tmp_dir, "example_app_#{@@example_app_counter}.rb")
14
+ File.join(tmp_dir, "example_app_#{$example_app_counter}.rb")
15
15
  end
16
16
 
17
17
  # Returns the name of the Sinatra application created by
18
18
  # +setup_example_app+: 'ExampleApp1' for the first application,
19
19
  # 'ExampleApp2' fo the second one, and so on...
20
20
  def app_name
21
- "ExampleApp#{@@example_app_counter}"
21
+ "ExampleApp#{$example_app_counter}"
22
22
  end
23
23
 
24
24
  # Returns the (constant of the) Sinatra application created by
@@ -70,8 +70,8 @@ describe Sinatra::Reloader do
70
70
  # the new application as the one being tested and enables the
71
71
  # reloader.
72
72
  def setup_example_app(options={})
73
- @@example_app_counter ||= 0
74
- @@example_app_counter += 1
73
+ $example_app_counter ||= 0
74
+ $example_app_counter += 1
75
75
 
76
76
  FileUtils.mkdir_p(tmp_dir)
77
77
  write_app_file(options)
metadata CHANGED
@@ -1,25 +1,32 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-contrib
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.3.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Konstantin Haase
9
9
  - Gabriel Andretta
10
+ - Trevor Bramble
10
11
  - Nicolas Sanguinetti
11
- - Eliot Shepard
12
+ - Ilya Shindyapin
13
+ - Masahiro Fujiwara
14
+ - Adrian Pacała
12
15
  - Andrew Crump
16
+ - Eliot Shepard
17
+ - Eric Marden
18
+ - Gray Manley
13
19
  - Matt Lyon
20
+ - lest
14
21
  - undr
15
22
  autorequire:
16
23
  bindir: bin
17
24
  cert_chain: []
18
- date: 2011-10-01 00:00:00.000000000Z
25
+ date: 2012-10-22 00:00:00.000000000 Z
19
26
  dependencies:
20
27
  - !ruby/object:Gem::Dependency
21
28
  name: sinatra
22
- requirement: &2153005720 !ruby/object:Gem::Requirement
29
+ requirement: !ruby/object:Gem::Requirement
23
30
  none: false
24
31
  requirements:
25
32
  - - ~>
@@ -27,10 +34,15 @@ dependencies:
27
34
  version: 1.3.0
28
35
  type: :runtime
29
36
  prerelease: false
30
- version_requirements: *2153005720
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ version: 1.3.0
31
43
  - !ruby/object:Gem::Dependency
32
44
  name: backports
33
- requirement: &2153005140 !ruby/object:Gem::Requirement
45
+ requirement: !ruby/object:Gem::Requirement
34
46
  none: false
35
47
  requirements:
36
48
  - - ! '>='
@@ -38,10 +50,15 @@ dependencies:
38
50
  version: '2.0'
39
51
  type: :runtime
40
52
  prerelease: false
41
- version_requirements: *2153005140
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '2.0'
42
59
  - !ruby/object:Gem::Dependency
43
60
  name: tilt
44
- requirement: &2153004220 !ruby/object:Gem::Requirement
61
+ requirement: !ruby/object:Gem::Requirement
45
62
  none: false
46
63
  requirements:
47
64
  - - ~>
@@ -49,10 +66,15 @@ dependencies:
49
66
  version: '1.3'
50
67
  type: :runtime
51
68
  prerelease: false
52
- version_requirements: *2153004220
69
+ version_requirements: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ~>
73
+ - !ruby/object:Gem::Version
74
+ version: '1.3'
53
75
  - !ruby/object:Gem::Dependency
54
76
  name: rack-test
55
- requirement: &2153003320 !ruby/object:Gem::Requirement
77
+ requirement: !ruby/object:Gem::Requirement
56
78
  none: false
57
79
  requirements:
58
80
  - - ! '>='
@@ -60,10 +82,15 @@ dependencies:
60
82
  version: '0'
61
83
  type: :runtime
62
84
  prerelease: false
63
- version_requirements: *2153003320
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
64
91
  - !ruby/object:Gem::Dependency
65
92
  name: rack-protection
66
- requirement: &2153002460 !ruby/object:Gem::Requirement
93
+ requirement: !ruby/object:Gem::Requirement
67
94
  none: false
68
95
  requirements:
69
96
  - - ! '>='
@@ -71,10 +98,15 @@ dependencies:
71
98
  version: '0'
72
99
  type: :runtime
73
100
  prerelease: false
74
- version_requirements: *2153002460
101
+ version_requirements: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
75
107
  - !ruby/object:Gem::Dependency
76
108
  name: eventmachine
77
- requirement: &2153002020 !ruby/object:Gem::Requirement
109
+ requirement: !ruby/object:Gem::Requirement
78
110
  none: false
79
111
  requirements:
80
112
  - - ! '>='
@@ -82,10 +114,15 @@ dependencies:
82
114
  version: '0'
83
115
  type: :runtime
84
116
  prerelease: false
85
- version_requirements: *2153002020
117
+ version_requirements: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
86
123
  - !ruby/object:Gem::Dependency
87
124
  name: rspec
88
- requirement: &2153001220 !ruby/object:Gem::Requirement
125
+ requirement: !ruby/object:Gem::Requirement
89
126
  none: false
90
127
  requirements:
91
128
  - - ~>
@@ -93,10 +130,15 @@ dependencies:
93
130
  version: '2.3'
94
131
  type: :development
95
132
  prerelease: false
96
- version_requirements: *2153001220
133
+ version_requirements: !ruby/object:Gem::Requirement
134
+ none: false
135
+ requirements:
136
+ - - ~>
137
+ - !ruby/object:Gem::Version
138
+ version: '2.3'
97
139
  - !ruby/object:Gem::Dependency
98
140
  name: haml
99
- requirement: &2153000680 !ruby/object:Gem::Requirement
141
+ requirement: !ruby/object:Gem::Requirement
100
142
  none: false
101
143
  requirements:
102
144
  - - ! '>='
@@ -104,10 +146,15 @@ dependencies:
104
146
  version: '0'
105
147
  type: :development
106
148
  prerelease: false
107
- version_requirements: *2153000680
149
+ version_requirements: !ruby/object:Gem::Requirement
150
+ none: false
151
+ requirements:
152
+ - - ! '>='
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
108
155
  - !ruby/object:Gem::Dependency
109
156
  name: erubis
110
- requirement: &2152999920 !ruby/object:Gem::Requirement
157
+ requirement: !ruby/object:Gem::Requirement
111
158
  none: false
112
159
  requirements:
113
160
  - - ! '>='
@@ -115,10 +162,15 @@ dependencies:
115
162
  version: '0'
116
163
  type: :development
117
164
  prerelease: false
118
- version_requirements: *2152999920
165
+ version_requirements: !ruby/object:Gem::Requirement
166
+ none: false
167
+ requirements:
168
+ - - ! '>='
169
+ - !ruby/object:Gem::Version
170
+ version: '0'
119
171
  - !ruby/object:Gem::Dependency
120
172
  name: slim
121
- requirement: &2152999280 !ruby/object:Gem::Requirement
173
+ requirement: !ruby/object:Gem::Requirement
122
174
  none: false
123
175
  requirements:
124
176
  - - ! '>='
@@ -126,10 +178,15 @@ dependencies:
126
178
  version: '0'
127
179
  type: :development
128
180
  prerelease: false
129
- version_requirements: *2152999280
181
+ version_requirements: !ruby/object:Gem::Requirement
182
+ none: false
183
+ requirements:
184
+ - - ! '>='
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
130
187
  - !ruby/object:Gem::Dependency
131
188
  name: rake
132
- requirement: &2152998780 !ruby/object:Gem::Requirement
189
+ requirement: !ruby/object:Gem::Requirement
133
190
  none: false
134
191
  requirements:
135
192
  - - ! '>='
@@ -137,15 +194,27 @@ dependencies:
137
194
  version: '0'
138
195
  type: :development
139
196
  prerelease: false
140
- version_requirements: *2152998780
197
+ version_requirements: !ruby/object:Gem::Requirement
198
+ none: false
199
+ requirements:
200
+ - - ! '>='
201
+ - !ruby/object:Gem::Version
202
+ version: '0'
141
203
  description: Collection of useful Sinatra extensions
142
204
  email:
143
205
  - konstantin.mailinglists@googlemail.com
144
206
  - ohhgabriel@gmail.com
207
+ - inbox@trevorbramble.com
145
208
  - contacto@nicolassanguinetti.info
146
- - eshepard@slower.net
209
+ - ilya@shindyapin.com
210
+ - m-fujiwara@axsh.net
211
+ - altpacala@gmail.com
147
212
  - andrew.crump@ieee.org
213
+ - eshepard@slower.net
214
+ - eric.marden@gmail.com
215
+ - g.manley@tukaiz.com
148
216
  - matt@flowerpowered.com
217
+ - just.lest@gmail.com
149
218
  - undr@yandex.ru
150
219
  executables: []
151
220
  extensions: []
@@ -186,6 +255,10 @@ files:
186
255
  - spec/content_for/different_key.erubis
187
256
  - spec/content_for/different_key.haml
188
257
  - spec/content_for/different_key.slim
258
+ - spec/content_for/footer.erb
259
+ - spec/content_for/footer.erubis
260
+ - spec/content_for/footer.haml
261
+ - spec/content_for/footer.slim
189
262
  - spec/content_for/layout.erb
190
263
  - spec/content_for/layout.erubis
191
264
  - spec/content_for/layout.haml
@@ -250,7 +323,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
250
323
  version: '0'
251
324
  requirements: []
252
325
  rubyforge_project:
253
- rubygems_version: 1.8.10
326
+ rubygems_version: 1.8.24
254
327
  signing_key:
255
328
  specification_version: 3
256
329
  summary: Collection of useful Sinatra extensions