pirj-sinatra-contrib 1.3.0

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.
Files changed (75) hide show
  1. data/LICENSE +20 -0
  2. data/README.md +135 -0
  3. data/Rakefile +61 -0
  4. data/ideas.md +29 -0
  5. data/lib/sinatra/capture.rb +42 -0
  6. data/lib/sinatra/config_file.rb +151 -0
  7. data/lib/sinatra/content_for.rb +111 -0
  8. data/lib/sinatra/contrib.rb +39 -0
  9. data/lib/sinatra/contrib/all.rb +2 -0
  10. data/lib/sinatra/contrib/setup.rb +53 -0
  11. data/lib/sinatra/contrib/version.rb +45 -0
  12. data/lib/sinatra/decompile.rb +113 -0
  13. data/lib/sinatra/engine_tracking.rb +96 -0
  14. data/lib/sinatra/extension.rb +95 -0
  15. data/lib/sinatra/json.rb +134 -0
  16. data/lib/sinatra/link_header.rb +132 -0
  17. data/lib/sinatra/namespace.rb +282 -0
  18. data/lib/sinatra/reloader.rb +384 -0
  19. data/lib/sinatra/respond_with.rb +245 -0
  20. data/lib/sinatra/streaming.rb +267 -0
  21. data/lib/sinatra/test_helpers.rb +87 -0
  22. data/sinatra-contrib.gemspec +121 -0
  23. data/spec/capture_spec.rb +80 -0
  24. data/spec/config_file/key_value.yml +6 -0
  25. data/spec/config_file/missing_env.yml +4 -0
  26. data/spec/config_file/with_envs.yml +7 -0
  27. data/spec/config_file/with_nested_envs.yml +11 -0
  28. data/spec/config_file_spec.rb +44 -0
  29. data/spec/content_for/different_key.erb +1 -0
  30. data/spec/content_for/different_key.erubis +1 -0
  31. data/spec/content_for/different_key.haml +2 -0
  32. data/spec/content_for/different_key.slim +2 -0
  33. data/spec/content_for/layout.erb +1 -0
  34. data/spec/content_for/layout.erubis +1 -0
  35. data/spec/content_for/layout.haml +1 -0
  36. data/spec/content_for/layout.slim +1 -0
  37. data/spec/content_for/multiple_blocks.erb +4 -0
  38. data/spec/content_for/multiple_blocks.erubis +4 -0
  39. data/spec/content_for/multiple_blocks.haml +8 -0
  40. data/spec/content_for/multiple_blocks.slim +8 -0
  41. data/spec/content_for/multiple_yields.erb +3 -0
  42. data/spec/content_for/multiple_yields.erubis +3 -0
  43. data/spec/content_for/multiple_yields.haml +3 -0
  44. data/spec/content_for/multiple_yields.slim +3 -0
  45. data/spec/content_for/passes_values.erb +1 -0
  46. data/spec/content_for/passes_values.erubis +1 -0
  47. data/spec/content_for/passes_values.haml +1 -0
  48. data/spec/content_for/passes_values.slim +1 -0
  49. data/spec/content_for/same_key.erb +1 -0
  50. data/spec/content_for/same_key.erubis +1 -0
  51. data/spec/content_for/same_key.haml +2 -0
  52. data/spec/content_for/same_key.slim +2 -0
  53. data/spec/content_for/takes_values.erb +1 -0
  54. data/spec/content_for/takes_values.erubis +1 -0
  55. data/spec/content_for/takes_values.haml +3 -0
  56. data/spec/content_for/takes_values.slim +3 -0
  57. data/spec/content_for_spec.rb +201 -0
  58. data/spec/decompile_spec.rb +44 -0
  59. data/spec/extension_spec.rb +33 -0
  60. data/spec/json_spec.rb +115 -0
  61. data/spec/link_header_spec.rb +100 -0
  62. data/spec/namespace/foo.erb +1 -0
  63. data/spec/namespace/nested/foo.erb +1 -0
  64. data/spec/namespace_spec.rb +623 -0
  65. data/spec/okjson.rb +581 -0
  66. data/spec/reloader/app.rb.erb +40 -0
  67. data/spec/reloader_spec.rb +441 -0
  68. data/spec/respond_with/bar.erb +1 -0
  69. data/spec/respond_with/bar.json.erb +1 -0
  70. data/spec/respond_with/foo.html.erb +1 -0
  71. data/spec/respond_with/not_html.sass +2 -0
  72. data/spec/respond_with_spec.rb +289 -0
  73. data/spec/spec_helper.rb +6 -0
  74. data/spec/streaming_spec.rb +436 -0
  75. metadata +252 -0
@@ -0,0 +1,80 @@
1
+ require 'backports'
2
+ require 'slim'
3
+ require_relative 'spec_helper'
4
+
5
+ describe Sinatra::Capture do
6
+ subject do
7
+ Sinatra.new do
8
+ enable :inline_templates
9
+ helpers Sinatra::Capture
10
+ end.new!
11
+ end
12
+ Tilt.prefer Tilt::ERBTemplate
13
+
14
+ extend Forwardable
15
+ def_delegators :subject, :capture, :capture_later
16
+
17
+ def render(engine, template)
18
+ subject.send(:render, engine, template.to_sym).strip.gsub(/\s+/, ' ')
19
+ end
20
+
21
+ shared_examples_for "a template language" do |engine|
22
+ lang = engine == :erubis ? :erb : engine
23
+
24
+ it "captures content" do
25
+ render(engine, "simple_#{lang}").should == "Say Hello World!"
26
+ end
27
+
28
+ it "allows nested captures" do
29
+ render(engine, "nested_#{lang}").should == "Say Hello World!"
30
+ end
31
+ end
32
+
33
+ describe('haml') { it_behaves_like "a template language", :haml }
34
+ describe('slim') { it_behaves_like "a template language", :slim }
35
+ describe('erb') { it_behaves_like "a template language", :erb }
36
+ describe('erubis') { it_behaves_like "a template language", :erubis }
37
+ end
38
+
39
+ __END__
40
+
41
+ @@ simple_erb
42
+ Say
43
+ <% a = capture do %>World<% end %>
44
+ Hello <%= a %>!
45
+
46
+ @@ nested_erb
47
+ Say
48
+ <% a = capture do %>
49
+ <% b = capture do %>World<% end %>
50
+ <%= b %>!
51
+ <% end %>
52
+ Hello <%= a.strip %>
53
+
54
+ @@ simple_slim
55
+ | Say
56
+ - a = capture do
57
+ | World
58
+ | Hello #{a.strip}!
59
+
60
+ @@ nested_slim
61
+ | Say
62
+ - a = capture do
63
+ - b = capture do
64
+ | World
65
+ | #{b.strip}!
66
+ | Hello #{a.strip}
67
+
68
+ @@ simple_haml
69
+ Say
70
+ - a = capture do
71
+ World
72
+ Hello #{a.strip}!
73
+
74
+ @@ nested_haml
75
+ Say
76
+ - a = capture do
77
+ - b = capture do
78
+ World
79
+ #{b.strip}!
80
+ Hello #{a.strip}
@@ -0,0 +1,6 @@
1
+ ---
2
+ foo: bar
3
+ something: 42
4
+ nested:
5
+ a: 1
6
+ b: 2
@@ -0,0 +1,4 @@
1
+ ---
2
+ foo:
3
+ production: 10
4
+ development: 20
@@ -0,0 +1,7 @@
1
+ ---
2
+ development:
3
+ foo: development
4
+ production:
5
+ foo: production
6
+ test:
7
+ foo: test
@@ -0,0 +1,11 @@
1
+ ---
2
+ database:
3
+ production:
4
+ adapter: postgresql
5
+ database: foo_production
6
+ development:
7
+ adapter: sqlite
8
+ database: db/development.db
9
+ test:
10
+ adapter: sqlite
11
+ database: db/test.db
@@ -0,0 +1,44 @@
1
+ require 'backports'
2
+ require_relative 'spec_helper'
3
+
4
+ describe Sinatra::ConfigFile do
5
+ def config_file(*args, &block)
6
+ mock_app do
7
+ register Sinatra::ConfigFile
8
+ set :root, File.expand_path('../config_file', __FILE__)
9
+ instance_eval(&block) if block
10
+ config_file(*args)
11
+ end
12
+ end
13
+
14
+ it 'should set options from a simple config_file' do
15
+ config_file 'key_value.yml'
16
+ settings.foo.should == 'bar'
17
+ settings.something.should == 42
18
+ end
19
+
20
+ it 'should create indifferent hashes' do
21
+ config_file 'key_value.yml'
22
+ settings.nested['a'].should == 1
23
+ settings.nested[:a].should == 1
24
+ end
25
+
26
+ it 'should recognize env specific settings per file' do
27
+ config_file 'with_envs.yml'
28
+ settings.foo.should == 'test'
29
+ end
30
+
31
+ it 'should recognize env specific settings per setting' do
32
+ config_file 'with_nested_envs.yml'
33
+ settings.database[:adapter].should == 'sqlite'
34
+ end
35
+
36
+ it 'should not set present values to nil if the current env is missing' do
37
+ # first let's check the test is actually working properly
38
+ config_file('missing_env.yml') { set :foo => 42, :environment => :production }
39
+ settings.foo.should == 10
40
+ # now test it
41
+ config_file('missing_env.yml') { set :foo => 42, :environment => :test }
42
+ settings.foo.should == 42
43
+ end
44
+ end
@@ -0,0 +1 @@
1
+ <% content_for :bar do %>bar<% end %>
@@ -0,0 +1 @@
1
+ <% content_for :bar do %>bar<% end %>
@@ -0,0 +1,2 @@
1
+ - content_for :bar do
2
+ bar
@@ -0,0 +1,2 @@
1
+ - content_for :bar do
2
+ | bar
@@ -0,0 +1 @@
1
+ <%= yield_content :foo %>
@@ -0,0 +1 @@
1
+ <%= yield_content :foo %>
@@ -0,0 +1 @@
1
+ = yield_content :foo
@@ -0,0 +1 @@
1
+ = yield_content :foo
@@ -0,0 +1,4 @@
1
+ <% content_for :foo do %>foo<% end %>
2
+ <% content_for :foo do %>bar<% end %>
3
+ <% content_for :baz do %>WON'T RENDER ME<% end %>
4
+ <% content_for :foo do %>baz<% end %>
@@ -0,0 +1,4 @@
1
+ <% content_for :foo do %>foo<% end %>
2
+ <% content_for :foo do %>bar<% end %>
3
+ <% content_for :baz do %>WON'T RENDER ME<% end %>
4
+ <% content_for :foo do %>baz<% end %>
@@ -0,0 +1,8 @@
1
+ - content_for :foo do
2
+ foo
3
+ - content_for :foo do
4
+ bar
5
+ - content_for :baz do
6
+ WON'T RENDER ME
7
+ - content_for :foo do
8
+ baz
@@ -0,0 +1,8 @@
1
+ - content_for :foo do
2
+ | foo
3
+ - content_for :foo do
4
+ | bar
5
+ - content_for :baz do
6
+ | WON'T RENDER ME
7
+ - content_for :foo do
8
+ | baz
@@ -0,0 +1,3 @@
1
+ <%= yield_content :foo %>
2
+ <%= yield_content :foo %>
3
+ <%= yield_content :foo %>
@@ -0,0 +1,3 @@
1
+ <%= yield_content :foo %>
2
+ <%= yield_content :foo %>
3
+ <%= yield_content :foo %>
@@ -0,0 +1,3 @@
1
+ = yield_content :foo
2
+ = yield_content :foo
3
+ = yield_content :foo
@@ -0,0 +1,3 @@
1
+ = yield_content :foo
2
+ = yield_content :foo
3
+ = yield_content :foo
@@ -0,0 +1 @@
1
+ <%= yield_content :foo, 1, 2 %>
@@ -0,0 +1 @@
1
+ <%= yield_content :foo, 1, 2 %>
@@ -0,0 +1 @@
1
+ = yield_content :foo, 1, 2
@@ -0,0 +1 @@
1
+ == yield_content :foo, 1, 2
@@ -0,0 +1 @@
1
+ <% content_for :foo do %>foo<% end %>
@@ -0,0 +1 @@
1
+ <% content_for :foo do %>foo<% end %>
@@ -0,0 +1,2 @@
1
+ - content_for :foo do
2
+ foo
@@ -0,0 +1,2 @@
1
+ - content_for :foo do
2
+ | foo
@@ -0,0 +1 @@
1
+ <% content_for :foo do |a, b| %><i><%= a %></i> <%= b %><% end %>
@@ -0,0 +1 @@
1
+ <% content_for :foo do |a, b| %><i><%= a %></i> <%= b %><% end %>
@@ -0,0 +1,3 @@
1
+ - content_for :foo do |a, b|
2
+ %i= a
3
+ =b
@@ -0,0 +1,3 @@
1
+ - content_for :foo do |a, b|
2
+ i= a
3
+ = b
@@ -0,0 +1,201 @@
1
+ require 'backports'
2
+ require_relative 'spec_helper'
3
+
4
+ describe Sinatra::ContentFor do
5
+ subject do
6
+ Sinatra.new do
7
+ helpers Sinatra::ContentFor
8
+ set :views, File.expand_path("../content_for", __FILE__)
9
+ end.new!
10
+ end
11
+
12
+ Tilt.prefer Tilt::ERBTemplate
13
+
14
+ extend Forwardable
15
+ def_delegators :subject, :content_for, :yield_content
16
+ def render(engine, template)
17
+ subject.send(:render, engine, template, :layout => false).gsub(/\s/, '')
18
+ end
19
+
20
+ describe "without templates" do
21
+ it 'renders blocks declared with the same key you use when rendering' do
22
+ content_for(:foo) { "foo" }
23
+ yield_content(:foo).should == "foo"
24
+ end
25
+
26
+ it 'renders blocks more than once' do
27
+ content_for(:foo) { "foo" }
28
+ 3.times { yield_content(:foo).should == "foo" }
29
+ end
30
+
31
+ it 'does not render a block with a different key' do
32
+ content_for(:bar) { "bar" }
33
+ yield_content(:foo).should be_empty
34
+ end
35
+
36
+ it 'renders multiple blocks with the same key' do
37
+ content_for(:foo) { "foo" }
38
+ content_for(:foo) { "bar" }
39
+ content_for(:bar) { "WON'T RENDER ME" }
40
+ content_for(:foo) { "baz" }
41
+ yield_content(:foo).should == "foobarbaz"
42
+ end
43
+
44
+ it 'renders multiple blocks more than once' do
45
+ content_for(:foo) { "foo" }
46
+ content_for(:foo) { "bar" }
47
+ content_for(:bar) { "WON'T RENDER ME" }
48
+ content_for(:foo) { "baz" }
49
+ 3.times { yield_content(:foo).should == "foobarbaz" }
50
+ end
51
+
52
+ it 'passes values to the blocks' do
53
+ content_for(:foo) { |a| a.upcase }
54
+ yield_content(:foo, 'a').should == "A"
55
+ yield_content(:foo, 'b').should == "B"
56
+ end
57
+ end
58
+
59
+ # TODO: liquid radius markaby builder nokogiri
60
+ engines = %w[erb erubis haml slim]
61
+
62
+ engines.each do |inner|
63
+ describe inner.capitalize do
64
+ before :all do
65
+ begin
66
+ require inner
67
+ rescue LoadError => e
68
+ pending "Skipping: " << e.message
69
+ end
70
+ end
71
+
72
+ describe "with yield_content in Ruby" do
73
+ it 'renders blocks declared with the same key you use when rendering' do
74
+ render inner, :same_key
75
+ yield_content(:foo).strip.should == "foo"
76
+ end
77
+
78
+ it 'renders blocks more than once' do
79
+ render inner, :same_key
80
+ 3.times { yield_content(:foo).strip.should == "foo" }
81
+ end
82
+
83
+ it 'does not render a block with a different key' do
84
+ render inner, :different_key
85
+ yield_content(:foo).should be_empty
86
+ end
87
+
88
+ it 'renders multiple blocks with the same key' do
89
+ render inner, :multiple_blocks
90
+ yield_content(:foo).gsub(/\s/, '').should == "foobarbaz"
91
+ end
92
+
93
+ it 'renders multiple blocks more than once' do
94
+ render inner, :multiple_blocks
95
+ 3.times { yield_content(:foo).gsub(/\s/, '').should == "foobarbaz" }
96
+ end
97
+
98
+ it 'passes values to the blocks' do
99
+ render inner, :takes_values
100
+ yield_content(:foo, 1, 2).gsub(/\s/, '').should == "<i>1</i>2"
101
+ end
102
+ end
103
+
104
+ describe "with content_for in Ruby" do
105
+ it 'renders blocks declared with the same key you use when rendering' do
106
+ content_for(:foo) { "foo" }
107
+ render(inner, :layout).should == "foo"
108
+ end
109
+
110
+ it 'renders blocks more than once' do
111
+ content_for(:foo) { "foo" }
112
+ render(inner, :multiple_yields).should == "foofoofoo"
113
+ end
114
+
115
+ it 'does not render a block with a different key' do
116
+ content_for(:bar) { "foo" }
117
+ render(inner, :layout).should be_empty
118
+ end
119
+
120
+ it 'renders multiple blocks with the same key' do
121
+ content_for(:foo) { "foo" }
122
+ content_for(:foo) { "bar" }
123
+ content_for(:bar) { "WON'T RENDER ME" }
124
+ content_for(:foo) { "baz" }
125
+ render(inner, :layout).should == "foobarbaz"
126
+ end
127
+
128
+ it 'renders multiple blocks more than once' do
129
+ content_for(:foo) { "foo" }
130
+ content_for(:foo) { "bar" }
131
+ content_for(:bar) { "WON'T RENDER ME" }
132
+ content_for(:foo) { "baz" }
133
+ render(inner, :multiple_yields).should == "foobarbazfoobarbazfoobarbaz"
134
+ end
135
+
136
+ it 'passes values to the blocks' do
137
+ content_for(:foo) { |a,b| "<i>#{a}</i>#{b}" }
138
+ render(inner, :passes_values).should == "<i>1</i>2"
139
+ end
140
+ end
141
+
142
+ engines.each do |outer|
143
+ describe "with yield_content in #{outer.capitalize}" do
144
+ def body
145
+ last_response.body.gsub(/\s/, '')
146
+ end
147
+
148
+ before :all do
149
+ begin
150
+ require outer
151
+ rescue LoadError => e
152
+ pending "Skipping: " << e.message
153
+ end
154
+ end
155
+
156
+ before do
157
+ mock_app do
158
+ helpers Sinatra::ContentFor
159
+ set inner, :layout_engine => outer
160
+ set :views, File.expand_path("../content_for", __FILE__)
161
+ get('/:view') { render(inner, params[:view].to_sym) }
162
+ get('/:layout/:view') do
163
+ render inner, params[:view].to_sym, :layout => params[:layout].to_sym
164
+ end
165
+ end
166
+ end
167
+
168
+ it 'renders blocks declared with the same key you use when rendering' do
169
+ get('/same_key').should be_ok
170
+ body.should == "foo"
171
+ end
172
+
173
+ it 'renders blocks more than once' do
174
+ get('/multiple_yields/same_key').should be_ok
175
+ body.should == "foofoofoo"
176
+ end
177
+
178
+ it 'does not render a block with a different key' do
179
+ get('/different_key').should be_ok
180
+ body.should be_empty
181
+ end
182
+
183
+ it 'renders multiple blocks with the same key' do
184
+ get('/multiple_blocks').should be_ok
185
+ body.should == "foobarbaz"
186
+ end
187
+
188
+ it 'renders multiple blocks more than once' do
189
+ get('/multiple_yields/multiple_blocks').should be_ok
190
+ body.should == "foobarbazfoobarbazfoobarbaz"
191
+ end
192
+
193
+ it 'passes values to the blocks' do
194
+ get('/passes_values/takes_values').should be_ok
195
+ body.should == "<i>1</i>2"
196
+ end
197
+ end
198
+ end
199
+ end
200
+ end
201
+ end