sinatra-pages 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.markdown +60 -13
  2. data/spec/pages_spec.rb +206 -146
  3. metadata +4 -4
data/README.markdown CHANGED
@@ -1,5 +1,5 @@
1
1
  # Sinatra Pages
2
- This is a [Sinatra Extension][1] that renders any page or sub-pages located under the directory defined as *:views* and the layout file defined as *:layout* (if there is any) inside your [Sinatra][2] application.
2
+ This is a [Sinatra][1] extension that renders any static page (including sub-pages) located under your *views/* directory using [HAML][2] as the rendering engine. By combining the main driving principles behind these two libraries, this extension allows you to build static websites for [Sinatra][2] or directly on [Rack][3] in no time.
3
3
 
4
4
  ### Installation
5
5
  In order to install this gem, you just need to install the gem from your command line like this:
@@ -8,8 +8,8 @@ In order to install this gem, you just need to install the gem from your command
8
8
 
9
9
  You should take into account that this library have the following dependencies:
10
10
 
11
- * [sinatra][2]
12
- * [haml][3]
11
+ * [sinatra][1]
12
+ * [haml][2]
13
13
 
14
14
  ### Usage
15
15
  Before using this extension, you should create the following file structure inside your application.
@@ -17,17 +17,28 @@ Before using this extension, you should create the following file structure insi
17
17
  app/
18
18
  |- app.rb
19
19
  |- config.ru
20
+ |- public/
21
+ |- images/
22
+ |- ...
23
+ |- scripts/
24
+ |- ...
25
+ |- styles/
26
+ |- ...
20
27
  |- views/
21
28
  |- home.haml
22
- |- a_file.haml
23
- |- another_file.haml
24
- |- another_file/yet_another_file.haml
25
- |- another_file/yet_another_file/still_another_file.haml
26
- |- ...more files and subdirectories...
27
29
  |- layout.haml
28
30
  |- not_found.haml
31
+ |- a_file.haml
32
+ |- another_file.haml
33
+ |- ...
34
+ |- another_file/
35
+ |- yet_another_file.haml
36
+ |- ...
37
+ |- yet_another_file/
38
+ |- still_another_file.haml
39
+ |- ...
29
40
 
30
- Please notice that this extension requires you to create the *home.haml* and the *not_found.haml* files inside the directory you defined as *:views* on your application (Sinatra uses the *views/* directory as the default path for the *:views* directory). Then you're free to add any layout (Sinatra defined the *layout.haml* file as the default specification for the *:layout* template) and page under any file structure hierarchy inside this directory.
41
+ Please notice that this extension requires you to create the *home.haml* and the *not_found.haml* files inside your *views/* directory on your application. Then you're free to add a *layout.haml* (if required) and all the static pages you need under any file structure hierarchy inside this directory. Please don't forget to give the extension *.haml* to all your views.
31
42
 
32
43
  Now, as any other existing extension, there are two possible use cases depending the kind of Sinatra application you're developing. If you follow the __Classic__ approach, then you just need to require this extension in the *app.rb* file.
33
44
 
@@ -68,7 +79,43 @@ You can try your modular application by executing the following command in your
68
79
  $app/rackup config.ru
69
80
 
70
81
  In order to verify if you application is working, open your web browser with the address that will appear after the execution described above.
71
-
82
+
83
+ ### Customization
84
+ This extension assumes the __:views__ and __:public__ configuration are located on the *./views* and *./public* directories respectively. In any case, you are able to change these values as required.
85
+
86
+ Depending on the kind of Sinatra application you're developing, you should proceed as follows. If you follow the __Classic__ approach, then you just need to set these configuration parameters in the *app.rb* file.
87
+
88
+ require 'sinatra'
89
+ require 'sinatra/pages'
90
+
91
+ set :views, File.join(Dir.pwd, 'templates')
92
+ set :public, Proc.new {File.join(root, 'static')}
93
+
94
+ In case you would prefer to follow the __Modular__ approach on your application design, then you can set these variables within your application as a class that inherit from the *Sinatra::Base* class in the *app.rb* file.
95
+
96
+ require 'sinatra/base'
97
+ require 'sinatra/pages'
98
+
99
+ class App < Sinatra::Base
100
+ register Sinatra::Pages
101
+
102
+ set :views, File.join(Dir.pwd, 'templates')
103
+ set :public, Proc.new {File.join(root, 'static')}
104
+ end
105
+
106
+ Alternatively, you can also set these variables directly from the *config.ru* file in which your application class will be associated to a certain route.
107
+
108
+ require 'app'
109
+
110
+ map '/' do
111
+ Sinatra::App.set :views, File.join(Dir.pwd, 'templates')
112
+ Sinatra::App.set :public, Proc.new {File.join(root, 'static')}
113
+
114
+ run Sinatra::App
115
+ end
116
+
117
+ In order to verify if the customized setup is working as expected, open your web browser with the address that will appear after the execution described above.
118
+
72
119
  ### Contributions
73
120
  Everybody is welcome to contribute to this project by commenting the source code, suggesting modifications or new ideas, reporting bugs, writing some documentation and, of course, you're also welcome to contribute with patches as well!
74
121
 
@@ -95,9 +142,9 @@ This extension have been tested on the versions 1.8.6, 1.8.7 and 1.9.1 of the [R
95
142
  ### License
96
143
  This extension is licensed under the [MIT License][9].
97
144
 
98
- [1]: http://www.sinatrarb.com/extensions.html
99
- [2]: http://www.sinatrarb.com/
100
- [3]: http://haml-lang.com/
145
+ [1]: http://www.sinatrarb.com/
146
+ [2]: http://haml-lang.com/
147
+ [3]: http://rack.rubyforge.org/
101
148
  [4]: http://rspec.info/
102
149
  [5]: http://eigenclass.org/hiki/rcov
103
150
  [6]: http://gitrdoc.com/brynary/rack-test/tree/master
data/spec/pages_spec.rb CHANGED
@@ -8,10 +8,6 @@ describe Sinatra::Pages do
8
8
  TestApp
9
9
  end
10
10
 
11
- before :all do
12
- PAGES.each{|page| create_file_for(page)}
13
- end
14
-
15
11
  context 'built-in settings' do
16
12
  context 'by default' do
17
13
  subject {app}
@@ -50,196 +46,260 @@ describe Sinatra::Pages do
50
46
  end
51
47
  end
52
48
 
53
- context "on HTTP GET" do
54
- context "in synchronous mode" do
55
- context "with no Layout file" do
56
- it "should render only the Home page if the given route is either empty or root." do
57
- File.exist?("views/#{file_of('Layout')}.haml").should be_false
58
- File.exist?("views/#{file_of('Home')}.haml").should be_true
59
-
60
- ['/', ''].each do |route|
61
- get route
62
-
63
- last_request.should_not be_xhr
64
- last_response.should be_ok
65
- last_response.body.chomp.should == file_of('Home')
49
+ context 'on HTTP GET' do
50
+ before :all do
51
+ PAGES.each{|page| create_file_for(page, app.views)}
52
+ end
53
+
54
+ context 'in synchronous mode' do
55
+ context 'with no Layout' do
56
+ subject {File.join app.views, "#{file_of('Layout')}.haml"}
57
+ it {File.exist?(subject).should == false}
58
+
59
+ context 'and with Home static page' do
60
+ subject {File.join app.views, "#{file_of('Home')}.haml"}
61
+ it {File.exist?(subject).should == true}
62
+ it 'should render it.' do
63
+ ['/', ''].each do |route|
64
+ get route
65
+
66
+ last_request.should_not be_xhr
67
+ last_response.should be_ok
68
+ last_response.body.chomp.should == file_of('Home')
69
+ end
66
70
  end
67
71
  end
68
72
 
69
- it "should render only an existing page if the given route match the '/:page' or '/*/:page' patterns." do
70
- Dir.glob 'views/**/*.haml' do |file|
71
- File.exist?("views/#{file_of('Layout')}.haml").should be_false
72
- File.exist?(file).should be_true
73
-
74
- directory = File.dirname(file)[5].nil? ? '' : File.dirname(file)[5, File.dirname(file).size]
75
-
76
- get "#{directory}/#{File.basename(file, '.haml')}"
77
-
78
- last_request.should_not be_xhr
79
- last_response.should be_ok
80
- last_response.body.chomp.should == File.basename(file, '.haml')
73
+ Dir.glob "#{Dir.pwd}/views/**/*.haml" do |file|
74
+ filename = File.basename(file, '.haml').capitalize
75
+
76
+ context "and with #{filename} static page" do
77
+ subject {file}
78
+ it {File.exist?(subject).should == true}
79
+ it "should render it." do
80
+ directory = File.dirname(subject)[5].nil? ? '' : File.dirname(subject)[5, File.dirname(subject).size]
81
+ path = "#{directory}/#{File.basename(subject, '.haml')}"
82
+
83
+ [path, "#{path}/"].each do |route|
84
+ get route
85
+
86
+ last_request.should_not be_xhr
87
+ last_response.should be_ok
88
+ last_response.body.chomp.should == File.basename(subject, '.haml')
89
+ end
90
+ end
81
91
  end
82
92
  end
83
93
 
84
- it "should render only the Not Found page if a given route can't find its static page on 'views/'." do
85
- File.exist?("views/#{file_of('Layout')}.haml").should be_false
86
- File.exist?("views/#{file_of('Do Not Exist')}.haml").should be_false
87
-
88
- get "/#{file_of('Do Not Exist')}"
94
+ context 'and with a non-existing static page' do
95
+ subject {File.join app.views, "#{file_of('Do Not Exist')}.haml"}
96
+ it {File.exist?(subject).should == false}
97
+ it 'should render the Not Found page.' do
98
+ path = "/#{file_of('Do Not Exist')}"
99
+
100
+ [path, "#{path}/"].each do |route|
101
+ get route
89
102
 
90
- last_request.should_not be_xhr
91
- last_response.should be_not_found
92
- last_response.body.chomp.should == file_of('Not Found')
103
+ last_request.should_not be_xhr
104
+ last_response.should be_not_found
105
+ last_response.body.chomp.should == file_of('Not Found')
106
+ end
107
+ end
93
108
  end
94
109
  end
95
110
 
96
- context "with Layout file" do
111
+ context "with Layout" do
97
112
  before :all do
98
- create_file_for('Layout', 'views', ['Layout', '= yield'])
113
+ create_file_for('Layout', app.views, ['Layout', '= yield'])
99
114
  end
100
-
101
- it "should render both the Layout and Home page if the given route is either empty or root." do
102
- File.exist?("views/#{file_of('Layout')}.haml").should be_true
103
- File.exist?("views/#{file_of('Home')}.haml").should be_true
104
-
105
- ['/', ''].each do |route|
106
- get route
107
-
108
- last_request.should_not be_xhr
109
- last_response.should be_ok
110
- separate(last_response.body).first.should == 'Layout'
111
- separate(last_response.body).last.should == file_of('Home')
115
+
116
+ subject {File.join app.views, "#{file_of('Layout')}.haml"}
117
+ it {File.exist?(subject).should == true}
118
+
119
+ context 'and with Home static page' do
120
+ subject {File.join app.views, "#{file_of('Home')}.haml"}
121
+ it {File.exist?(subject).should == true}
122
+ it 'should render both.' do
123
+ ['/', ''].each do |route|
124
+ get route
125
+
126
+ last_request.should_not be_xhr
127
+ last_response.should be_ok
128
+ separate(last_response.body).first.should == 'Layout'
129
+ separate(last_response.body).last.should == file_of('Home')
130
+ end
112
131
  end
113
132
  end
114
-
115
- it "should render both the Layout and an existing page if the given route match the '/:page' or '/*/:page' patterns." do
116
- Dir.glob('views/**/*.haml').reject{|file| file =~ /layout/}.each do |file|
117
- File.exist?("views/#{file_of('Layout')}.haml").should be_true
118
- File.exist?(file).should be_true
119
-
120
- directory = File.dirname(file)[5].nil? ? '' : File.dirname(file)[5, File.dirname(file).size]
121
-
122
- get "#{directory}/#{File.basename(file, '.haml')}"
123
-
124
- last_request.should_not be_xhr
125
- last_response.should be_ok
126
- separate(last_response.body).first.should == 'Layout'
127
- separate(last_response.body).last.should == File.basename(file, '.haml')
133
+
134
+ Dir.glob "#{Dir.pwd}/views/**/*.haml" do |file|
135
+ filename = File.basename(file, '.haml').capitalize
136
+
137
+ context "and with #{filename} static page" do
138
+ subject {file}
139
+ it {File.exist?(subject).should == true}
140
+ it "should render both." do
141
+ directory = File.dirname(subject)[5].nil? ? '' : File.dirname(subject)[5, File.dirname(subject).size]
142
+ path = "#{directory}/#{File.basename(subject, '.haml')}"
143
+
144
+ [path, "#{path}/"].each do |route|
145
+ get route
146
+
147
+ last_request.should_not be_xhr
148
+ last_response.should be_ok
149
+ separate(last_response.body).first.should == 'Layout'
150
+ separate(last_response.body).last.should == File.basename(file, '.haml')
151
+ end
152
+ end
128
153
  end
129
154
  end
130
155
 
131
- it "should render both the Layout and the Not Found page if a given route can't find its static page on 'views/'." do
132
- File.exist?("views/#{file_of('Layout')}.haml").should be_true
133
- File.exist?("views/#{file_of('Do Not Exist')}.haml").should be_false
134
-
135
- get "/#{file_of('Do Not Exist')}"
136
-
137
- last_request.should_not be_xhr
138
- last_response.should be_not_found
139
- separate(last_response.body).first.should == 'Layout'
140
- separate(last_response.body).last.should == file_of('Not Found')
156
+ context 'and with a non-existing static page' do
157
+ subject {File.join app.views, "#{file_of('Do Not Exist')}.haml"}
158
+ it {File.exist?(subject).should == false}
159
+ it 'should render the Layout and the Not Found page.' do
160
+ path = "/#{file_of('Do Not Exist')}"
161
+
162
+ [path, "#{path}/"].each do |route|
163
+ get route
164
+
165
+ last_request.should_not be_xhr
166
+ last_response.should be_not_found
167
+ separate(last_response.body).first.should == 'Layout'
168
+ separate(last_response.body).last.should == file_of('Not Found')
169
+ end
170
+ end
141
171
  end
142
172
 
143
173
  after :all do
144
- FileUtils.rm 'views/layout.haml'
174
+ FileUtils.rm "#{app.views}/layout.haml"
145
175
  end
146
176
  end
147
177
  end
148
178
 
149
179
  context "in asynchronous mode" do
150
- context "with no Layout file" do
151
- it "should render just the Home page if the given route is either empty or root." do
152
- File.exist?("views/#{file_of('Layout')}.haml").should be_false
153
- File.exist?("views/#{file_of('Home')}.haml").should be_true
154
-
155
- ['/', ''].each do |route|
156
- request route, :method => 'GET', :xhr => true
157
-
158
- last_request.should be_xhr
159
- last_response.should be_ok
160
- last_response.body.chomp.should == file_of('Home')
180
+ context 'with no Layout' do
181
+ subject {File.join app.views, "#{file_of('Layout')}.haml"}
182
+ it {File.exist?(subject).should == false}
183
+
184
+ context 'and with Home static page' do
185
+ subject {File.join app.views, "#{file_of('Home')}.haml"}
186
+ it {File.exist?(subject).should == true}
187
+ it 'should render it.' do
188
+ ['/', ''].each do |route|
189
+ request route, :method => 'GET', :xhr => true
190
+
191
+ last_request.should be_xhr
192
+ last_response.should be_ok
193
+ last_response.body.chomp.should == file_of('Home')
194
+ end
161
195
  end
162
196
  end
163
197
 
164
- it "should render just an existing page if the given route match the '/:page' or '/*/:page' patterns." do
165
- Dir.glob 'views/**/*.haml' do |file|
166
- File.exist?("views/#{file_of('Layout')}.haml").should be_false
167
- File.exist?(file).should be_true
168
-
169
- directory = File.dirname(file)[5].nil? ? '' : File.dirname(file)[5, File.dirname(file).size]
170
-
171
- request "#{directory}/#{File.basename(file, '.haml')}", :method => 'GET', :xhr => true
172
-
173
- last_request.should be_xhr
174
- last_response.should be_ok
175
- last_response.body.chomp.should == File.basename(file, '.haml')
198
+ Dir.glob "#{Dir.pwd}/views/**/*.haml" do |file|
199
+ filename = File.basename(file, '.haml').capitalize
200
+
201
+ context "and with #{filename} static page" do
202
+ subject {file}
203
+ it {File.exist?(subject).should == true}
204
+ it "should render it." do
205
+ directory = File.dirname(subject)[5].nil? ? '' : File.dirname(subject)[5, File.dirname(subject).size]
206
+ path = "#{directory}/#{File.basename(subject, '.haml')}"
207
+
208
+ [path, "#{path}/"].each do |route|
209
+ request route, :method => 'GET', :xhr => true
210
+
211
+ last_request.should be_xhr
212
+ last_response.should be_ok
213
+ last_response.body.chomp.should == File.basename(subject, '.haml')
214
+ end
215
+ end
176
216
  end
177
217
  end
178
218
 
179
- it "should render just the Not Found page if a given route can't find its static page on 'views/." do
180
- File.exist?("views/#{file_of('Layout')}.haml").should be_false
181
- File.exist?("views/#{file_of('Do Not Exist')}.haml").should be_false
182
-
183
- request "/#{file_of('Do Not Exist')}", :method => 'GET', :xhr => true
219
+ context 'and with a non-existing static page' do
220
+ subject {File.join app.views, "#{file_of('Do Not Exist')}.haml"}
221
+ it {File.exist?(subject).should == false}
222
+ it 'should render the Not Found page.' do
223
+ path = "/#{file_of('Do Not Exist')}"
224
+
225
+ [path, "#{path}/"].each do |route|
226
+ request route, :method => 'GET', :xhr => true
184
227
 
185
- last_request.should be_xhr
186
- last_response.should be_not_found
187
- last_response.body.chomp.should == file_of('Not Found')
228
+ last_request.should be_xhr
229
+ last_response.should be_not_found
230
+ last_response.body.chomp.should == file_of('Not Found')
231
+ end
232
+ end
188
233
  end
189
234
  end
190
235
 
191
- context "with a Layout file" do
236
+ context 'with a Layout' do
192
237
  before :all do
193
- create_file_for('Layout', 'views', ['Layout', '= yield'])
238
+ create_file_for('Layout', app.views, ['Layout', '= yield'])
194
239
  end
195
-
196
- it "should render both the Layout and Home page if the given route is either empty or root." do
197
- File.exist?("views/#{file_of('Layout')}.haml").should be_true
198
- File.exist?("views/#{file_of('Home')}.haml").should be_true
199
-
200
- ['/', ''].each do |route|
201
- request route, :method => 'GET', :xhr => true
202
-
203
- last_request.should be_xhr
204
- last_response.should be_ok
205
- last_response.body.chomp.should == file_of('Home')
240
+
241
+ subject {File.join app.views, "#{file_of('Layout')}.haml"}
242
+ it {File.exist?(subject).should == true}
243
+
244
+ context 'and with Home static page' do
245
+ subject {File.join app.views, "#{file_of('Home')}.haml"}
246
+ it {File.exist?(subject).should == true}
247
+ it 'should render it.' do
248
+ ['/', ''].each do |route|
249
+ request route, :method => 'GET', :xhr => true
250
+
251
+ last_request.should be_xhr
252
+ last_response.should be_ok
253
+ last_response.body.chomp.should == file_of('Home')
254
+ end
206
255
  end
207
256
  end
208
-
209
- it "should render both the Layout and an existing page if the given route match the '/:page' or '/*/:page' patterns." do
210
- Dir.glob('views/**/*.haml').reject{|file| file =~ /layout/}.each do |file|
211
- File.exist?("views/#{file_of('Layout')}.haml").should be_true
212
- File.exist?(file).should be_true
213
-
214
- directory = File.dirname(file)[5].nil? ? '' : File.dirname(file)[5, File.dirname(file).size]
215
-
216
- request "#{directory}/#{File.basename(file, '.haml')}", :method => 'GET', :xhr => true
217
-
218
- last_request.should be_xhr
219
- last_response.should be_ok
220
- last_response.body.chomp.should == File.basename(file, '.haml')
257
+
258
+ Dir.glob "#{Dir.pwd}/views/**/*.haml" do |file|
259
+ filename = File.basename(file, '.haml').capitalize
260
+
261
+ context "and with #{filename} static page" do
262
+ subject {file}
263
+ it {File.exist?(subject).should == true}
264
+ it "should render both." do
265
+ directory = File.dirname(subject)[5].nil? ? '' : File.dirname(subject)[5, File.dirname(subject).size]
266
+ path = "#{directory}/#{File.basename(subject, '.haml')}"
267
+
268
+ [path, "#{path}/"].each do |route|
269
+ request route, :method => 'GET', :xhr => true
270
+
271
+ last_request.should be_xhr
272
+ last_response.should be_ok
273
+ last_response.body.chomp.should == File.basename(file, '.haml')
274
+ end
275
+ end
221
276
  end
222
277
  end
223
278
 
224
- it "should render both the Layout and the Not Found page if a given route can't find its static page on 'views/'." do
225
- File.exist?("views/#{file_of('Layout')}.haml").should be_true
226
- File.exist?("views/#{file_of('Do Not Exist')}.haml").should be_false
227
-
228
- request "/#{file_of('Do Not Exist')}", :method => 'GET', :xhr => true
279
+ context 'and with a non-existing static page' do
280
+ subject {File.join app.views, "#{file_of('Do Not Exist')}.haml"}
281
+ it {File.exist?(subject).should == false}
282
+ it 'should render the Layout and the Not Found page.' do
283
+ path = "/#{file_of('Do Not Exist')}"
284
+
285
+ [path, "#{path}/"].each do |route|
286
+ request route, :method => 'GET', :xhr => true
229
287
 
230
- last_request.should be_xhr
231
- last_response.should be_not_found
232
- last_response.body.chomp.should == file_of('Not Found')
288
+ last_request.should be_xhr
289
+ last_response.should be_not_found
290
+ last_response.body.chomp.should == file_of('Not Found')
291
+ end
292
+ end
233
293
  end
234
294
 
235
295
  after :all do
236
- FileUtils.rm 'views/layout.haml'
296
+ FileUtils.rm "#{app.views}/layout.haml"
237
297
  end
238
298
  end
239
299
  end
240
- end
241
-
242
- after :all do
243
- FileUtils.rm_r 'views', :force => true
300
+
301
+ after :all do
302
+ FileUtils.rm_r app.views, :force => true
303
+ end
244
304
  end
245
305
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-pages
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 1.2.0
10
+ version: 1.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Julio Javier Cicchelli
@@ -98,7 +98,7 @@ dependencies:
98
98
  version: 0.5.4
99
99
  type: :development
100
100
  version_requirements: *id005
101
- description: " A Sinatra extension for static pages rendering.\n"
101
+ description: " A Sinatra extension for static pages rendering using the HAML rendering engine.\n"
102
102
  email: javier@rock-n-code.com
103
103
  executables: []
104
104