sinatra-pages 0.9.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
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 plug in this extension, you should create the following file structure inside your application.
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
- The only restriction is extension imposes is that you have to create the *home.haml* and the *not_found.haml* files inside the directory you defined as *:views* for the pluggable application (Sinatra uses the *views/* directory as the default path for the *:views* option). Then you're free to add any layout (Sinatra defined the *layout.haml* file as the default specification for the *:layout* option) and page under any file structure hierarchy inside this directory.
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
- Then, you just need to plug it in inside your *config.ru* file.
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::Pages
67
+ run Sinatra::App
38
68
  end
39
-
40
- Finally, you should test your application by executing the following command on your command line.
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
- class Pages < Sinatra::Base
6
- %w[/? /:page /*/:page].each do |route|
7
- get route do
8
- params[:page] = 'home' if params[:page].nil?
9
-
10
- page_to_render = params[:splat].nil? ? '' : "#{params[:splat].first}/"
11
- page_to_render << params[:page]
12
-
13
- begin
14
- haml page_to_render.to_sym, :layout => !request.xhr?
15
- rescue Errno::ENOENT
16
- halt 404
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
- end
20
-
21
- not_found do
22
- params[:page] = 'not_found'
23
-
24
- haml :not_found, :layout => !request.xhr?
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::Pages
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
@@ -1,3 +1,7 @@
1
- require File.join(Dir.pwd, %w{lib sinatra pages})
1
+ require 'sinatra'
2
2
  require 'rack/test'
3
- require 'fileutils'
3
+ require 'fileutils'
4
+ require File.join(Dir.pwd, %w{lib sinatra pages})
5
+
6
+ set :environment, :test
7
+ set :views, "#{Dir.pwd}/views"
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.9.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-01-23 00:00:00 +01:00
12
+ date: 2010-02-21 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency