sinatra-mapping 1.1.0 → 1.2.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/CHANGES +3 -1
- data/INFO +0 -1
- data/LICENSE +1 -0
- data/README.rdoc +93 -97
- data/VERSION +2 -2
- data/lib/sinatra/mapping.rb +35 -31
- data/test/classic_mapping_test.rb +2 -3
- data/test/fixtures/classic_application.rb +7 -8
- data/test/modular_mapping_test.rb +1 -2
- metadata +2 -12
data/CHANGES
CHANGED
data/INFO
CHANGED
data/LICENSE
CHANGED
data/README.rdoc
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Map easily URLs in your Web applications.
|
4
4
|
|
5
|
-
* {Homepage}[
|
5
|
+
* {Homepage}[http://sinatra-mapping.rubyforge.org/]
|
6
6
|
* {Repository}[http://github.com/hallison/sinatra-mapping]
|
7
7
|
* {Project}[http://rubyforge.org/projects/sinatra-mapping]
|
8
8
|
* {Issues}[http://github.com/hallison/sinatra-mapping/issues]
|
@@ -43,113 +43,109 @@ Sinatra implements REST routes:
|
|
43
43
|
|
44
44
|
For improve this routes use extension by registered method in the main
|
45
45
|
source of application. To better understand, copy and paste the following
|
46
|
-
example in {Ruby}[http://www.ruby-lang.org] source file +
|
46
|
+
example in {Ruby}[http://www.ruby-lang.org] source file +weblog.rb+:
|
47
47
|
|
48
48
|
#!/usr/bin/env ruby
|
49
49
|
require 'rubygems'
|
50
50
|
require 'sinatra'
|
51
51
|
require 'sinatra/mapping' # only this line for use mapping!
|
52
52
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
<
|
67
|
-
<
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
<
|
79
|
-
<
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
<
|
94
|
-
<
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
end_content
|
133
|
-
end
|
134
|
-
|
135
|
-
get tagged_entries_path do |tag_id|
|
136
|
-
<<-end_content
|
137
|
-
<h1>Welcome to Foo Web Application</h1>
|
138
|
-
<h2>#{tag_id.capitalize}</h2>
|
139
|
-
<p>
|
140
|
-
It works and show all entries tagged with "#{tag_id}".
|
141
|
-
</p>
|
142
|
-
<p>
|
143
|
-
#{link_to "Back", :root}
|
144
|
-
</p>
|
145
|
-
end_content
|
146
|
-
end
|
53
|
+
map :root, "blog" # /blog/
|
54
|
+
map :entries, "posts" # /blog/posts
|
55
|
+
map :tags, "labels" # /blog/labels
|
56
|
+
|
57
|
+
mapping :entry => "posts/:entry_id", # /blog/posts/id-for-post
|
58
|
+
:entry_comments => "posts/:entry_id/comments", # /blog/posts/id-for-post/comments
|
59
|
+
:tagged_entries => "labels/:tag_id/entries" # /blog/labels/id-for-tag/entries
|
60
|
+
|
61
|
+
# /blog/
|
62
|
+
get root_path do
|
63
|
+
<<-end_content
|
64
|
+
<h1>Welcome to Foo Web Application</h1>
|
65
|
+
<ul>
|
66
|
+
<li>#{link_to title_path(:entries), :entries, :title => title_path(:entries)}</li>
|
67
|
+
<li>#{link_to title_path(:tags), :tags, :title => title_path(:tags)}</li>
|
68
|
+
</ul>
|
69
|
+
end_content
|
70
|
+
end
|
71
|
+
|
72
|
+
# /blog/entries
|
73
|
+
get entries_path do
|
74
|
+
<<-end_content
|
75
|
+
<h1>Welcome to Foo Web Application</h1>
|
76
|
+
<h2>#{title_path(:entries)}</h2>
|
77
|
+
<ul>
|
78
|
+
<li>#{link_to "Testing new entry ...", :entries, "testing-new-entry"}</li>
|
79
|
+
<li>#{link_to "Testing old entry ...", :entries, "testing-old-entry"}</li>
|
80
|
+
</ul>
|
81
|
+
<p>
|
82
|
+
#{link_to "Back", :root}
|
83
|
+
</p>
|
84
|
+
end_content
|
85
|
+
end
|
86
|
+
|
87
|
+
# /blog/labels/tag-id-for-show-content
|
88
|
+
get tags_path do
|
89
|
+
<<-end_content
|
90
|
+
<h1>Welcome to Foo Web Application</h1>
|
91
|
+
<h2>#{title_path(:tags)}</h2>
|
92
|
+
<ul>
|
93
|
+
<li>#{link_to "Ruby", :tags, "ruby", "entries"}</li>
|
94
|
+
<li>#{link_to "Sinatra", :tags, "sinatra", "entries"}</li>
|
95
|
+
<li>#{link_to "Mapping", :tags, "mapping", "entries"}</li>
|
96
|
+
</ul>
|
97
|
+
<p>
|
98
|
+
#{link_to "Back", :root}
|
99
|
+
</p>
|
100
|
+
end_content
|
101
|
+
end
|
102
|
+
|
103
|
+
# /blog/entries/entry-id-for-show-content
|
104
|
+
get entry_path do |entry_id|
|
105
|
+
title = entry_id.gsub('-',' ').capitalize
|
106
|
+
<<-end_content
|
107
|
+
<h1>Welcome to Foo Web Application</h1>
|
108
|
+
<h2>#{title}</h2>
|
109
|
+
<p>
|
110
|
+
It works!
|
111
|
+
</p>
|
112
|
+
<p>
|
113
|
+
#{link_to "Back", :root} | #{link_to "Comments", :entries, entry_id, 'comments'}
|
114
|
+
</p>
|
115
|
+
end_content
|
116
|
+
end
|
117
|
+
|
118
|
+
# /blog/entries/entry-id-for-show-content/comments
|
119
|
+
get entry_comments_path do |entry_id|
|
120
|
+
title = entry_id.gsub('-',' ').capitalize
|
121
|
+
<<-end_content
|
122
|
+
<h1>Welcome to Foo Web Application</h1>
|
123
|
+
<h2>#{title}</h2>
|
124
|
+
<p>
|
125
|
+
It works and show comments for "#{title}".
|
126
|
+
</p>
|
127
|
+
<p>
|
128
|
+
#{link_to "Back", :root}
|
129
|
+
</p>
|
130
|
+
end_content
|
131
|
+
end
|
147
132
|
|
133
|
+
get tagged_entries_path do |tag_id|
|
134
|
+
<<-end_content
|
135
|
+
<h1>Welcome to Foo Web Application</h1>
|
136
|
+
<h2>#{tag_id.capitalize}</h2>
|
137
|
+
<p>
|
138
|
+
It works and show all entries tagged with "#{tag_id}".
|
139
|
+
</p>
|
140
|
+
<p>
|
141
|
+
#{link_to "Back", :root}
|
142
|
+
</p>
|
143
|
+
end_content
|
148
144
|
end
|
149
145
|
|
150
146
|
Run:
|
151
147
|
|
152
|
-
$ ruby
|
148
|
+
$ ruby weblog.rb -p 3000
|
153
149
|
|
154
150
|
Open Web browser http://localhost:3000/blog/ and look ... it works!
|
155
151
|
|
data/VERSION
CHANGED
data/lib/sinatra/mapping.rb
CHANGED
@@ -22,8 +22,8 @@ module Mapping
|
|
22
22
|
#
|
23
23
|
# If name is equal :root, then returns path ended by slash "/".
|
24
24
|
#
|
25
|
-
# map :root, "tasks"
|
26
|
-
# map :changes, "last-changes
|
25
|
+
# map :root, "tasks" #=> /tasks/
|
26
|
+
# map :changes, "last-changes #=> /tasks/last-changes
|
27
27
|
def map(name, path = nil)
|
28
28
|
@locations ||= {}
|
29
29
|
if name.to_sym == :root
|
@@ -37,12 +37,14 @@ module Mapping
|
|
37
37
|
map_path_to(@locations[name.to_sym], *paths << "/?")
|
38
38
|
end
|
39
39
|
end
|
40
|
+
Delegator.delegate "#{name}_path"
|
40
41
|
end
|
41
42
|
|
42
43
|
# Auto mapping from a hash. This method is very useful.
|
43
44
|
# Example:
|
44
45
|
#
|
45
|
-
#
|
46
|
+
# In Web application:
|
47
|
+
#
|
46
48
|
# class WebApp << Sinatra::Base
|
47
49
|
# mapping :root => "tasks", # /tasks
|
48
50
|
# :status => "changes" # /tasks/changes
|
@@ -50,21 +52,28 @@ module Mapping
|
|
50
52
|
#
|
51
53
|
# Or, it's possible use from configuration file.
|
52
54
|
#
|
53
|
-
# # YAML file "
|
54
|
-
#
|
55
|
-
#
|
56
|
-
# status: changes
|
55
|
+
# # YAML file "mapping.yml".
|
56
|
+
# root: tasks
|
57
|
+
# status: changes
|
57
58
|
#
|
58
59
|
# # In Sinatra application.
|
59
|
-
#
|
60
|
-
#
|
61
|
-
#
|
60
|
+
#
|
61
|
+
# mapping YAML.load_file("mapping.yml")
|
62
|
+
# #=> root_path # /tasks
|
63
|
+
# #=> status_path # /tasks/changes
|
62
64
|
def mapping(hash)
|
63
65
|
hash.each do |name, path|
|
64
66
|
map name, path
|
65
67
|
end
|
66
68
|
end
|
67
69
|
|
70
|
+
# Register automatically all helpers in base application.
|
71
|
+
def self.registered(app)
|
72
|
+
app.helpers Mapping::Helpers
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
68
77
|
# Returns URL path with query instructions.
|
69
78
|
# This method has been extracted from
|
70
79
|
# http://wiki.github.com/sinatra/sinatra/howto-generate-links.
|
@@ -76,13 +85,6 @@ module Mapping
|
|
76
85
|
path
|
77
86
|
end
|
78
87
|
|
79
|
-
# Register automatically all helpers in base application.
|
80
|
-
def self.registered(app)
|
81
|
-
app.helpers Mapping::Helpers
|
82
|
-
end
|
83
|
-
|
84
|
-
private
|
85
|
-
|
86
88
|
# Check arguments. If argument is a symbol and exist map path before
|
87
89
|
# setted, then return path mapped by symbol name.
|
88
90
|
def map_path_to(*args)
|
@@ -111,29 +113,31 @@ private
|
|
111
113
|
paths.join('/').gsub(/[\/]{2,}/,'/')
|
112
114
|
end
|
113
115
|
|
116
|
+
# Copyright (c) 2009 Hallison Batista
|
117
|
+
#
|
114
118
|
# This module contains several helper methods for paths written using
|
115
|
-
#
|
119
|
+
# +map+ method.
|
116
120
|
module Helpers
|
117
121
|
|
118
122
|
# Creates a title using a path mapped. Otherwise, returns just arguments
|
119
123
|
# joined by spaces and capitalised.
|
120
124
|
#
|
121
|
-
#
|
125
|
+
# In Sinatra application:
|
122
126
|
#
|
123
127
|
# map :posts, "articles"
|
124
128
|
# map :tags, "labels"
|
125
129
|
# map :archive, "archive/articles"
|
126
130
|
#
|
127
|
-
#
|
131
|
+
# In views:
|
128
132
|
#
|
129
133
|
# <%=title_path :posts%>
|
130
|
-
#
|
134
|
+
# #=> "Articles"
|
131
135
|
#
|
132
136
|
# <%=title_path :tags%>
|
133
|
-
#
|
137
|
+
# #=> "Labels"
|
134
138
|
#
|
135
139
|
# <%=title_path :archive%>
|
136
|
-
#
|
140
|
+
# #=> "Archive articles"
|
137
141
|
def title_path(path, *args)
|
138
142
|
title = (options.locations[path] || path).to_s.gsub('/',' ').strip
|
139
143
|
title.gsub!(/\W/,' ') # Cleanup
|
@@ -143,7 +147,7 @@ private
|
|
143
147
|
# Creates anchor links for name and extract path and HTML options from
|
144
148
|
# arguments. Example:
|
145
149
|
#
|
146
|
-
#
|
150
|
+
# In Sinatra application, add a map.
|
147
151
|
#
|
148
152
|
# map :tasks, "tasks"
|
149
153
|
# map :status, "tasks/status"
|
@@ -156,13 +160,13 @@ private
|
|
156
160
|
# erb :status, :locals => { :status => "finished" }
|
157
161
|
# end
|
158
162
|
#
|
159
|
-
#
|
163
|
+
# In status view, add a link to status map.
|
160
164
|
#
|
161
165
|
# <%= link_to "All finished", :status, status %>
|
162
|
-
#
|
166
|
+
# #=> <a href="/tasks/status/finished">All finished</a>
|
163
167
|
#
|
164
168
|
# <%= link_to "All finished", :status, :name => status %>
|
165
|
-
#
|
169
|
+
# #=> <a href="/tasks/status?name=finished">All finished</a>
|
166
170
|
def link_to(name = nil, *args)
|
167
171
|
options = args.last.kind_of?(Hash) ? args.pop : {}
|
168
172
|
url = args.shift if args.first.to_s =~ /^\w.*?:/
|
@@ -172,17 +176,17 @@ private
|
|
172
176
|
|
173
177
|
# Returns all paths with query parameters. Example:
|
174
178
|
#
|
175
|
-
#
|
179
|
+
# In Sinatra application:
|
176
180
|
#
|
177
181
|
# map :post, "articles"
|
178
182
|
# map :tags, "labels"
|
179
183
|
#
|
180
|
-
#
|
184
|
+
# Use the following instructions:
|
181
185
|
#
|
182
186
|
# path_to :tags, "ruby", :posts
|
183
|
-
#
|
187
|
+
# #=> "/labels/ruby/articles"
|
184
188
|
def path_to(*args)
|
185
|
-
|
189
|
+
self.class.send(:build_path_to, env['SCRIPT_NAME'], *args)
|
186
190
|
end
|
187
191
|
|
188
192
|
private
|
@@ -3,11 +3,10 @@ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/..")
|
|
3
3
|
require 'rubygems'
|
4
4
|
require 'test/unit'
|
5
5
|
require 'rack/test'
|
6
|
-
require 'ruby-debug'
|
7
6
|
|
8
7
|
require 'test/fixtures/classic_application'
|
9
8
|
|
10
|
-
class
|
9
|
+
class ClassicMappingTest < Test::Unit::TestCase
|
11
10
|
|
12
11
|
include Rack::Test::Methods
|
13
12
|
|
@@ -32,7 +31,7 @@ class ModularTestMapping < Test::Unit::TestCase
|
|
32
31
|
end
|
33
32
|
|
34
33
|
def app
|
35
|
-
@app = ::
|
34
|
+
@app = Sinatra::Application
|
36
35
|
@app.set :environment, :test
|
37
36
|
@app
|
38
37
|
end
|
@@ -3,13 +3,12 @@ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../../lib/")
|
|
3
3
|
|
4
4
|
require 'sinatra'
|
5
5
|
require 'sinatra/mapping'
|
6
|
-
require 'ruby-debug'
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
7
|
+
class Sinatra::Application
|
8
|
+
def env
|
9
|
+
@env.update('SCRIPT_NAME' => '/test')
|
10
|
+
end
|
11
|
+
end
|
13
12
|
|
14
13
|
map :root, "blog" # root_path => /blog/
|
15
14
|
map :about # about_path => /blog/about
|
@@ -48,7 +47,7 @@ get about_path do
|
|
48
47
|
end
|
49
48
|
|
50
49
|
get search_path do
|
51
|
-
<<-end_content.gsub(/^
|
50
|
+
<<-end_content.gsub(/^ /,'')
|
52
51
|
#{title_path :search}:#{path_to :search, :keywords => 'ruby'}
|
53
52
|
#{link_to "Search", :search, :title => 'Search'}
|
54
53
|
#{link_to "Search", :search, :title => 'Search', :keywords => 'ruby'}
|
@@ -56,7 +55,7 @@ get search_path do
|
|
56
55
|
end
|
57
56
|
|
58
57
|
get drafts_path do
|
59
|
-
<<-end_content.gsub(/^
|
58
|
+
<<-end_content.gsub(/^ /,'')
|
60
59
|
#{title_path :drafts}:#{path_to [:drafts, :posts]}
|
61
60
|
#{link_to "Unpublished", :drafts, :posts, :title => 'Unpublished'}
|
62
61
|
end_content
|
@@ -3,11 +3,10 @@ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/..")
|
|
3
3
|
require 'rubygems'
|
4
4
|
require 'test/unit'
|
5
5
|
require 'rack/test'
|
6
|
-
require 'ruby-debug'
|
7
6
|
|
8
7
|
require 'test/fixtures/modular_application'
|
9
8
|
|
10
|
-
class
|
9
|
+
class ModularMappingTest < Test::Unit::TestCase
|
11
10
|
|
12
11
|
include Rack::Test::Methods
|
13
12
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-mapping
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hallison Batista
|
@@ -9,19 +9,9 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-09-22 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
|
-
- !ruby/object:Gem::Dependency
|
16
|
-
name: rack
|
17
|
-
type: :runtime
|
18
|
-
version_requirement:
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
20
|
-
requirements:
|
21
|
-
- - ">="
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 0.9.1
|
24
|
-
version:
|
25
15
|
- !ruby/object:Gem::Dependency
|
26
16
|
name: sinatra
|
27
17
|
type: :runtime
|