sinatra-pages 0.9.0 → 1.0.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.
- data/README.markdown +37 -7
- data/lib/sinatra/pages.rb +22 -18
- data/spec/pages_spec.rb +2 -3
- data/spec/spec_helper.rb +6 -2
- metadata +2 -2
data/README.markdown
CHANGED
@@ -12,9 +12,10 @@ You should take into account that this library have the following dependencies:
|
|
12
12
|
* [haml][3]
|
13
13
|
|
14
14
|
### Usage
|
15
|
-
Before
|
15
|
+
Before using this extension, you should create the following file structure inside your application.
|
16
16
|
|
17
17
|
app/
|
18
|
+
|- app.rb
|
18
19
|
|- config.ru
|
19
20
|
|- views/
|
20
21
|
|- home.haml
|
@@ -26,18 +27,47 @@ Before plug in this extension, you should create the following file structure in
|
|
26
27
|
|- layout.haml
|
27
28
|
|- not_found.haml
|
28
29
|
|
29
|
-
|
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.
|
30
31
|
|
31
|
-
|
32
|
+
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.
|
32
33
|
|
33
34
|
require 'sinatra'
|
34
35
|
require 'sinatra/pages'
|
35
|
-
|
36
|
+
|
37
|
+
Then you should require your *app.rb* file inside the *config.ru* file in order to make your application runnable.
|
38
|
+
|
39
|
+
require 'app'
|
40
|
+
|
41
|
+
disable :run
|
42
|
+
|
43
|
+
run Sinatra::Application
|
44
|
+
|
45
|
+
You can test your application by executing the following command on your command line.
|
46
|
+
|
47
|
+
$app/ruby app.rb
|
48
|
+
|
49
|
+
In case you would prefer to follow the __Modular__ approach on your application design, then you just need to declare your application as a class that inherit from the *Sinatra::Base* class and then register the extension inside it in the *app.rb* file.
|
50
|
+
|
51
|
+
require 'sinatra/base'
|
52
|
+
require 'sinatra/pages'
|
53
|
+
|
54
|
+
module Sinatra
|
55
|
+
module App < Sinatra::Base
|
56
|
+
register Sinatra::Pages
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
Then you should require your *app.rb* inside the *config.ru* file and associate your application class to a certain route.
|
61
|
+
|
62
|
+
require 'app'
|
63
|
+
|
64
|
+
disable :run
|
65
|
+
|
36
66
|
map '/' do
|
37
|
-
run Sinatra::
|
67
|
+
run Sinatra::App
|
38
68
|
end
|
39
|
-
|
40
|
-
|
69
|
+
|
70
|
+
You can try your modular application by executing the following command in your command line.
|
41
71
|
|
42
72
|
$app/rackup config.ru
|
43
73
|
|
data/lib/sinatra/pages.rb
CHANGED
@@ -2,26 +2,30 @@ require 'sinatra/base'
|
|
2
2
|
require 'haml'
|
3
3
|
|
4
4
|
module Sinatra
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
5
|
+
module Pages
|
6
|
+
def self.registered(app)
|
7
|
+
%w[/? /:page /*/:page].each do |route|
|
8
|
+
app.get route do
|
9
|
+
params[:page] = 'home' if params[:page].nil?
|
10
|
+
|
11
|
+
page_to_render = params[:splat].nil? ? '' : "#{params[:splat].first}/"
|
12
|
+
page_to_render << params[:page]
|
13
|
+
|
14
|
+
begin
|
15
|
+
haml page_to_render.to_sym, :layout => !request.xhr?
|
16
|
+
rescue Errno::ENOENT
|
17
|
+
halt 404
|
18
|
+
end
|
17
19
|
end
|
18
20
|
end
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
21
|
+
|
22
|
+
app.not_found do
|
23
|
+
params[:page] = 'not_found'
|
24
|
+
|
25
|
+
haml :not_found, :layout => !request.xhr?
|
26
|
+
end
|
25
27
|
end
|
26
28
|
end
|
29
|
+
|
30
|
+
register Pages
|
27
31
|
end
|
data/spec/pages_spec.rb
CHANGED
@@ -4,7 +4,7 @@ describe Sinatra::Pages do
|
|
4
4
|
include Rack::Test::Methods
|
5
5
|
|
6
6
|
def app
|
7
|
-
Sinatra::
|
7
|
+
Sinatra::Application
|
8
8
|
end
|
9
9
|
|
10
10
|
PAGES = ['Home','Generic',{'Generic Test'=>'Test'},{'Another Generic Test'=>{'Generic Test'=>'Test'}},'Not Found']
|
@@ -15,14 +15,13 @@ describe Sinatra::Pages do
|
|
15
15
|
page_to_create = page.class == String ? page : page.keys.first
|
16
16
|
content << '= "#{params[:page]}"' if content.empty?
|
17
17
|
|
18
|
-
Dir.mkdir directory unless File.exist? "#{directory}/"
|
18
|
+
Dir.mkdir "#{directory}/" unless File.exist? "#{directory}/"
|
19
19
|
File.open("#{directory}/#{file_of.(page_to_create)}.haml", 'w'){|file| content.each{|line| file.puts line}}
|
20
20
|
|
21
21
|
create_file_for.(page.values.first, "#{directory}/#{file_of.(page.keys.first)}") if page.class == Hash
|
22
22
|
end
|
23
23
|
|
24
24
|
before :all do
|
25
|
-
FileUtils.mkdir 'views'
|
26
25
|
PAGES.each{|page| create_file_for.(page)}
|
27
26
|
end
|
28
27
|
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-pages
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Julio Javier Cicchelli
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-
|
12
|
+
date: 2010-02-21 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|