sinatra-mapping 1.0.7 → 1.1.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 +9 -0
- data/INFO +2 -2
- data/README.rdoc +4 -13
- data/Rakefile +18 -12
- data/VERSION +2 -2
- data/lib/sinatra/mapping.rb +191 -80
- data/test/classic_mapping_test.rb +113 -0
- data/test/fixtures/classic_application.rb +63 -0
- data/test/fixtures/modular_application.rb +0 -2
- data/test/modular_mapping_test.rb +18 -17
- metadata +6 -6
- data/lib/sinatra/mapping_helpers.rb +0 -114
- data/test/helper.rb +0 -6
data/CHANGES
CHANGED
@@ -1,5 +1,14 @@
|
|
1
1
|
= Changes
|
2
2
|
|
3
|
+
[1.1.1 - not released]
|
4
|
+
* The mapping methods works in classic application.
|
5
|
+
|
6
|
+
[1.1.0 - 2009-08-25]
|
7
|
+
* Added suffix "/?" in all paths when no arguments.
|
8
|
+
* The helpers are registered automatically.
|
9
|
+
* Updates in documentation.
|
10
|
+
* Tests have been fixed.
|
11
|
+
|
3
12
|
[1.0.6 - 2009-08-21]
|
4
13
|
* Fixes in the method for build path mapped.
|
5
14
|
* Updates in documentation.
|
data/INFO
CHANGED
data/README.rdoc
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
Map easily URLs in your Web applications.
|
4
4
|
|
5
|
+
* {Homepage}[:link:/]
|
5
6
|
* {Repository}[http://github.com/hallison/sinatra-mapping]
|
6
7
|
* {Project}[http://rubyforge.org/projects/sinatra-mapping]
|
7
|
-
* {Documentation}[:link:Sinatra/Mapping.html]
|
8
8
|
* {Issues}[http://github.com/hallison/sinatra-mapping/issues]
|
9
9
|
|
10
10
|
The extension Sinatra::Mapping is a minimal module that is useful for
|
@@ -12,6 +12,7 @@ create map names for {Ruby}[http://www.ruby-lang.org]
|
|
12
12
|
{Sinatra}[http://www.sinatrarb.com] web applications.
|
13
13
|
|
14
14
|
== Getting start
|
15
|
+
|
15
16
|
Install stable version gem from {RubyForge.org}[http://www.rubyforge.org/]:
|
16
17
|
|
17
18
|
gem install sinatra-mapping
|
@@ -45,17 +46,12 @@ source of application. To better understand, copy and paste the following
|
|
45
46
|
example in {Ruby}[http://www.ruby-lang.org] source file +webfoo.rb+:
|
46
47
|
|
47
48
|
#!/usr/bin/env ruby
|
48
|
-
|
49
49
|
require 'rubygems'
|
50
50
|
require 'sinatra'
|
51
|
-
require 'sinatra/mapping'
|
52
|
-
require 'sinatra/mapping_helpers'
|
51
|
+
require 'sinatra/mapping' # only this line for use mapping!
|
53
52
|
|
54
53
|
class Sinatra::Application
|
55
54
|
|
56
|
-
register Sinatra::Mapping
|
57
|
-
helpers Sinatra::MappingHelpers
|
58
|
-
|
59
55
|
map :root, "blog" # /blog/
|
60
56
|
map :entries, "posts" # /blog/posts
|
61
57
|
map :tags, "labels" # /blog/labels
|
@@ -157,12 +153,7 @@ Run:
|
|
157
153
|
|
158
154
|
Open Web browser http://localhost:3000/blog/ and look ... it works!
|
159
155
|
|
160
|
-
== More informations
|
161
|
-
|
162
|
-
* See the {changes}[:link:CHANGES.html] for understand the goals of
|
163
|
-
that release version.
|
164
|
-
* {License}[:link:LICENSE.html]
|
165
|
-
|
166
156
|
== Copyright
|
167
157
|
|
168
158
|
:include:LICENSE
|
159
|
+
|
data/Rakefile
CHANGED
@@ -1,7 +1,12 @@
|
|
1
|
-
|
1
|
+
begin
|
2
|
+
require 'hanna/rdoctask'
|
3
|
+
rescue LoadError
|
4
|
+
require 'rdoc'
|
5
|
+
require 'rake/rdoctask'
|
6
|
+
end
|
7
|
+
|
2
8
|
require 'rake/testtask'
|
3
9
|
require 'rake/gempackagetask'
|
4
|
-
require 'rake/rdoctask'
|
5
10
|
load 'sinatra-mapping.gemspec'
|
6
11
|
|
7
12
|
def current_version(file = "VERSION")
|
@@ -48,16 +53,17 @@ Rake::GemPackageTask.new(@spec) do |pkg|
|
|
48
53
|
pkg.need_tar_bz2 = true
|
49
54
|
end
|
50
55
|
|
56
|
+
desc "Generate RDoc API documentation."
|
51
57
|
Rake::RDocTask.new("doc:api") do |rdoc|
|
52
|
-
rdoc.title =
|
53
|
-
rdoc.main =
|
54
|
-
rdoc.options =
|
55
|
-
rdoc.rdoc_dir =
|
56
|
-
rdoc.rdoc_files.include
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
58
|
+
rdoc.title = %q{Sinatra::Mapping - API Documentation}
|
59
|
+
rdoc.main = %q{README.rdoc}
|
60
|
+
rdoc.options = %w{--line-numbers --show-hash}
|
61
|
+
rdoc.rdoc_dir = %q{doc/api}
|
62
|
+
rdoc.rdoc_files.include %w{
|
63
|
+
CHANGES
|
64
|
+
LICENSE
|
65
|
+
README.rdoc
|
66
|
+
lib/**/*.rb
|
67
|
+
}
|
62
68
|
end
|
63
69
|
|
data/VERSION
CHANGED
data/lib/sinatra/mapping.rb
CHANGED
@@ -4,109 +4,220 @@ require 'sinatra/base'
|
|
4
4
|
# {official site}[http://www.sinatrarb.com/].
|
5
5
|
module Sinatra
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
7
|
+
# Copyright (c) 2009 Hallison Batista
|
8
|
+
#
|
9
|
+
# This extension is useful for any Web application written using
|
10
|
+
# Sinatra DSL. The main goal is help developers to write URL path
|
11
|
+
# methods.
|
12
|
+
module Mapping
|
13
|
+
|
14
|
+
# All location paths mapped.
|
15
|
+
attr_reader :locations
|
16
|
+
|
17
|
+
# Write URL path method for use in HTTP methods.
|
18
|
+
#
|
19
|
+
# The map method most be used by following syntax:
|
20
|
+
#
|
21
|
+
# map <name>, <path>
|
22
|
+
#
|
23
|
+
# If name is equal :root, then returns path ended by slash "/".
|
24
|
+
#
|
25
|
+
# map :root, "tasks" # => /tasks/
|
26
|
+
# map :changes, "last-changes # => /tasks/last-changes
|
27
|
+
def map(name, path = nil)
|
28
|
+
@locations ||= {}
|
29
|
+
if name.to_sym == :root
|
30
|
+
@locations[:root] = cleanup_paths("/#{path}/")
|
31
|
+
metadef "#{name}_path" do |*paths|
|
32
|
+
cleanup_paths("/#{@locations[:root]}/?")
|
33
|
+
end
|
34
|
+
else
|
35
|
+
@locations[name.to_sym] = cleanup_paths(path || name.to_s)
|
36
|
+
metadef "#{name}_path" do |*paths|
|
37
|
+
map_path_to(@locations[name.to_sym], *paths << "/?")
|
37
38
|
end
|
38
39
|
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Auto mapping from a hash. This method is very useful.
|
43
|
+
# Example:
|
44
|
+
#
|
45
|
+
# # In Web application.
|
46
|
+
# class WebApp << Sinatra::Base
|
47
|
+
# mapping :root => "tasks", # /tasks
|
48
|
+
# :status => "changes" # /tasks/changes
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# Or, it's possible use from configuration file.
|
52
|
+
#
|
53
|
+
# # YAML file "settings.yml".
|
54
|
+
# mapping:
|
55
|
+
# root: tasks
|
56
|
+
# status: changes
|
57
|
+
#
|
58
|
+
# # In Sinatra application.
|
59
|
+
# mapping YAML.load_file("settings.yml")[:mapping]
|
60
|
+
# # root_path # /tasks
|
61
|
+
# # status_path # /tasks/changes
|
62
|
+
def mapping(hash)
|
63
|
+
hash.each do |name, path|
|
64
|
+
map name, path
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# Returns URL path with query instructions.
|
69
|
+
# This method has been extracted from
|
70
|
+
# http://wiki.github.com/sinatra/sinatra/howto-generate-links.
|
71
|
+
def build_path_to(script_name = nil, *args)
|
72
|
+
args.compact!
|
73
|
+
query = args.pop if args.last.kind_of?(Hash)
|
74
|
+
path = map_path_to(script_name, *args)
|
75
|
+
path << "?" << Rack::Utils::build_query(query) if query
|
76
|
+
path
|
77
|
+
end
|
78
|
+
|
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
|
+
# Check arguments. If argument is a symbol and exist map path before
|
87
|
+
# setted, then return path mapped by symbol name.
|
88
|
+
def map_path_to(*args)
|
89
|
+
script_name = args.shift if args.first.to_s =~ %r{^/\w.*}
|
90
|
+
path_mapped(script_name, *locations_get_from(*args))
|
91
|
+
end
|
92
|
+
|
93
|
+
# Returns all paths mapped by root path in prefix.
|
94
|
+
def path_mapped(script_name, *args)
|
95
|
+
return cleanup_paths("/#{script_name}/#{@locations[:root]}") if args.empty?
|
96
|
+
cleanup_paths("/#{script_name}/#{@locations[:root]}/#{args.join('/')}")
|
97
|
+
end
|
98
|
+
|
99
|
+
# Get paths from location maps.
|
100
|
+
def locations_get_from(*args)
|
101
|
+
args.flatten.reject do |path|
|
102
|
+
path == :root
|
103
|
+
end.collect do |path|
|
104
|
+
@locations[path] || path
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
# Clean all duplicated slashes.
|
109
|
+
def cleanup_paths(*paths)
|
110
|
+
#.gsub(%r{#{@locations[:root]}/#{@locations[:root]}}, @locations[:root])
|
111
|
+
paths.join('/').gsub(/[\/]{2,}/,'/')
|
112
|
+
end
|
113
|
+
|
114
|
+
# This module contains several helper methods for paths written using
|
115
|
+
# {map}[:link:./Sinatra/Mapping.html#map] method.
|
116
|
+
module Helpers
|
117
|
+
|
118
|
+
# Creates a title using a path mapped. Otherwise, returns just arguments
|
119
|
+
# joined by spaces and capitalised.
|
120
|
+
#
|
121
|
+
# # In Sinatra application
|
122
|
+
#
|
123
|
+
# map :posts, "articles"
|
124
|
+
# map :tags, "labels"
|
125
|
+
# map :archive, "archive/articles"
|
126
|
+
#
|
127
|
+
# # In views
|
128
|
+
#
|
129
|
+
# <%=title_path :posts%>
|
130
|
+
# # => "Articles"
|
131
|
+
#
|
132
|
+
# <%=title_path :tags%>
|
133
|
+
# # => "Labels"
|
134
|
+
#
|
135
|
+
# <%=title_path :archive%>
|
136
|
+
# # => "Archive articles"
|
137
|
+
def title_path(path, *args)
|
138
|
+
title = (options.locations[path] || path).to_s.gsub('/',' ').strip
|
139
|
+
title.gsub!(/\W/,' ') # Cleanup
|
140
|
+
(args.empty? ? title : "#{title} #{args.join(' ')}").strip.capitalize
|
141
|
+
end
|
39
142
|
|
40
|
-
#
|
41
|
-
# Example:
|
143
|
+
# Creates anchor links for name and extract path and HTML options from
|
144
|
+
# arguments. Example:
|
42
145
|
#
|
43
|
-
# # In
|
44
|
-
#
|
45
|
-
#
|
46
|
-
#
|
146
|
+
# # In Sinatra application, add a map.
|
147
|
+
#
|
148
|
+
# map :tasks, "tasks"
|
149
|
+
# map :status, "tasks/status"
|
150
|
+
#
|
151
|
+
# get tasks_path do
|
152
|
+
# erb :tasks, :locals => { :name => params.values.join(', ') }
|
47
153
|
# end
|
48
154
|
#
|
49
|
-
#
|
155
|
+
# get status_path do
|
156
|
+
# erb :status, :locals => { :status => "finished" }
|
157
|
+
# end
|
50
158
|
#
|
51
|
-
# #
|
52
|
-
# mapping:
|
53
|
-
# root: tasks
|
54
|
-
# status: changes
|
159
|
+
# # In status view, add a link to status map.
|
55
160
|
#
|
56
|
-
#
|
57
|
-
#
|
58
|
-
#
|
59
|
-
#
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
161
|
+
# <%= link_to "All finished", :status, status %>
|
162
|
+
# # => <a href="/tasks/status/finished">All finished</a>
|
163
|
+
#
|
164
|
+
# <%= link_to "All finished", :status, :name => status %>
|
165
|
+
# # => <a href="/tasks/status?name=finished">All finished</a>
|
166
|
+
def link_to(name = nil, *args)
|
167
|
+
options = args.last.kind_of?(Hash) ? args.pop : {}
|
168
|
+
url = args.shift if args.first.to_s =~ /^\w.*?:/
|
169
|
+
args << extract_query_attributes(options)
|
170
|
+
"<a href=\"#{url || path_to(*args)}\"#{extract_link_attributes(options)}>#{name || url}</a>"
|
64
171
|
end
|
65
172
|
|
66
|
-
# Returns
|
67
|
-
#
|
68
|
-
#
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
173
|
+
# Returns all paths with query parameters. Example:
|
174
|
+
#
|
175
|
+
# # In Sinatra application:
|
176
|
+
#
|
177
|
+
# map :post, "articles"
|
178
|
+
# map :tags, "labels"
|
179
|
+
#
|
180
|
+
# # Use the following instructions:
|
181
|
+
#
|
182
|
+
# path_to :tags, "ruby", :posts
|
183
|
+
# # => "/labels/ruby/articles"
|
184
|
+
def path_to(*args)
|
185
|
+
options.build_path_to(env['SCRIPT_NAME'], *args)
|
75
186
|
end
|
76
187
|
|
77
188
|
private
|
78
189
|
|
79
|
-
#
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
190
|
+
# Extract all tag attributes from a hash keys and values.
|
191
|
+
def extract_link_attributes(hash)
|
192
|
+
select_link_attributes(hash).map do |attribute, value|
|
193
|
+
" #{attribute}=\"#{value}\""
|
194
|
+
end
|
84
195
|
end
|
85
196
|
|
86
|
-
# Returns
|
87
|
-
def
|
88
|
-
|
89
|
-
cleanup_paths("/#{script_name}/#{@locations[:root]}/#{args.join('/')}")
|
197
|
+
# Returns only attributes for link tag.
|
198
|
+
def select_link_attributes(hash)
|
199
|
+
hash.select{ |key, value| link_attributes.include?key }
|
90
200
|
end
|
91
201
|
|
92
|
-
#
|
93
|
-
def
|
94
|
-
|
95
|
-
|
96
|
-
end.collect do |path|
|
97
|
-
@locations[path] || path
|
98
|
-
end
|
202
|
+
# Select all keys and values that not included in link attributes.
|
203
|
+
def extract_query_attributes(hash)
|
204
|
+
query = hash.select{ |key, value| !link_attributes.include?key }.flatten
|
205
|
+
Hash[*query] unless query.empty?
|
99
206
|
end
|
100
207
|
|
101
|
-
#
|
102
|
-
def
|
103
|
-
|
104
|
-
|
208
|
+
# Attribute list for link tag.
|
209
|
+
def link_attributes
|
210
|
+
[:accesskey, :charset, :coords, :hreflang, :id, :lang, :name, :onblur,
|
211
|
+
:onclick, :ondblclick, :onfocus, :onkeydown, :onkeypress, :onkeyup,
|
212
|
+
:onmousedown, :onmousemove, :onmouseout, :onmouseover, :onmouseup,
|
213
|
+
:rel, :rev, :shape, :style, :tabindex, :target, :title, :type]
|
105
214
|
end
|
106
215
|
|
107
|
-
end # module
|
216
|
+
end # module Helpers
|
217
|
+
|
218
|
+
end # module Mapping
|
108
219
|
|
109
|
-
|
220
|
+
register Mapping
|
110
221
|
|
111
222
|
end # module Sinatra
|
112
223
|
|
@@ -0,0 +1,113 @@
|
|
1
|
+
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/..")
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'test/unit'
|
5
|
+
require 'rack/test'
|
6
|
+
require 'ruby-debug'
|
7
|
+
|
8
|
+
require 'test/fixtures/classic_application'
|
9
|
+
|
10
|
+
class ModularTestMapping < Test::Unit::TestCase
|
11
|
+
|
12
|
+
include Rack::Test::Methods
|
13
|
+
|
14
|
+
def setup
|
15
|
+
@date = Date.today
|
16
|
+
@link_paths = {
|
17
|
+
:root_path => "/test/blog",
|
18
|
+
:posts_path => "/test/blog/articles",
|
19
|
+
:archive_path => "/test/blog/archive/articles",
|
20
|
+
:about_path => "/test/blog/about",
|
21
|
+
:search_path => "/test/blog/find-articles",
|
22
|
+
:drafts_path => "/test/blog/unpublished"
|
23
|
+
}
|
24
|
+
@root_paths = @link_paths.inject({}) do |hash, (name, path)|
|
25
|
+
hash[name] = "#{path}/"
|
26
|
+
hash
|
27
|
+
end
|
28
|
+
@paths = @root_paths.inject({}) do |hash, (name, path)|
|
29
|
+
hash[name] = "#{path}?"
|
30
|
+
hash
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def app
|
35
|
+
@app = ::ModularApplication
|
36
|
+
@app.set :environment, :test
|
37
|
+
@app
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_check_map_locations
|
41
|
+
@paths.each do |name, location|
|
42
|
+
path = location.gsub(/\/test/,'')
|
43
|
+
assert_equal path, app.send(name)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_should_return_ok_in_root_path
|
48
|
+
get app.root_path do |response|
|
49
|
+
assert response.ok?
|
50
|
+
assert_equal "http://example.org#{@root_paths[:root_path]}", last_request.url
|
51
|
+
assert_equal "Blog path:#{@link_paths[:root_path]}/", response.body
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_should_return_ok_in_posts_path
|
56
|
+
get app.posts_path do |response|
|
57
|
+
assert response.ok?
|
58
|
+
assert_equal "http://example.org#{@root_paths[:posts_path]}", last_request.url
|
59
|
+
assert_equal "Articles published:#{@link_paths[:posts_path]}", response.body
|
60
|
+
end
|
61
|
+
|
62
|
+
path_id = "#{@date.to_s.gsub('-','/')}/post-permalink"
|
63
|
+
get app.posts_path path_id do |response|
|
64
|
+
assert response.ok?
|
65
|
+
assert_equal "http://example.org#{@root_paths[:posts_path]}#{path_id}/", last_request.url
|
66
|
+
assert_equal "Articles:#{@link_paths[:posts_path]}/#{path_id}", response.body
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_should_return_ok_in_archive_path
|
71
|
+
get app.archive_path do |response|
|
72
|
+
assert response.ok?
|
73
|
+
assert_equal "http://example.org#{@root_paths[:archive_path]}", last_request.url
|
74
|
+
assert_equal "Archive articles:#{@link_paths[:archive_path]}", response.body
|
75
|
+
end
|
76
|
+
|
77
|
+
path_id = "#{@date.to_s.gsub('-','/')}/post-permalink"
|
78
|
+
get app.archive_path path_id do |response|
|
79
|
+
assert response.ok?
|
80
|
+
assert_equal "http://example.org#{@root_paths[:archive_path]}#{path_id}/", last_request.url
|
81
|
+
assert_equal "Archive articles:#{@link_paths[:archive_path]}/#{path_id}", response.body
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_should_return_ok_in_about_path
|
86
|
+
get app.about_path do |response|
|
87
|
+
assert response.ok?
|
88
|
+
assert_equal "http://example.org#{@root_paths[:about_path]}", last_request.url
|
89
|
+
assert_equal "About:#{@link_paths[:about_path]}", response.body
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_should_return_ok_in_search_path
|
94
|
+
path_params = "keywords=ruby"
|
95
|
+
get app.search_path, :keywords => "ruby" do |response|
|
96
|
+
assert response.ok?
|
97
|
+
assert_equal "http://example.org#{@root_paths[:search_path]}?keywords=ruby", last_request.url
|
98
|
+
assert_equal "Find articles:#{@link_paths[:search_path]}?keywords=ruby", response.body.split("\n")[0]
|
99
|
+
assert_equal "<a href=\"#{@link_paths[:search_path]}\" title=\"Search\">Search</a>", response.body.split("\n")[1]
|
100
|
+
assert_equal "<a href=\"#{@link_paths[:search_path]}?keywords=ruby\" title=\"Search\">Search</a>", response.body.split("\n")[2]
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_should_check_path_method_with_array_params
|
105
|
+
get app.drafts_path do |response|
|
106
|
+
assert response.ok?
|
107
|
+
assert_equal "Unpublished:#{@link_paths[:drafts_path]}/articles", response.body.split("\n")[0]
|
108
|
+
body_link = "<a href=\"#{@link_paths[:drafts_path]}/articles\" title=\"Unpublished\">Unpublished</a>"
|
109
|
+
assert_equal body_link, response.body.split("\n")[1]
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../../lib/")
|
3
|
+
|
4
|
+
require 'sinatra'
|
5
|
+
require 'sinatra/mapping'
|
6
|
+
require 'ruby-debug'
|
7
|
+
|
8
|
+
#class Sinatra::Application
|
9
|
+
# def env
|
10
|
+
# @env.update('SCRIPT_NAME' => '/test')
|
11
|
+
# end
|
12
|
+
#end
|
13
|
+
|
14
|
+
map :root, "blog" # root_path => /blog/
|
15
|
+
map :about # about_path => /blog/about
|
16
|
+
|
17
|
+
mapping :posts => "articles", # posts_path => /blog/articles
|
18
|
+
:archive => "archive/articles", # archive_path => /blog/archive/articles
|
19
|
+
:search => "find-articles", # search_path => /blog/find-articles
|
20
|
+
:drafts => "unpublished" # drafts_path => /blog/unpublished
|
21
|
+
|
22
|
+
before do
|
23
|
+
@date = Date.today
|
24
|
+
end
|
25
|
+
|
26
|
+
get root_path do
|
27
|
+
"#{title_path :root, :path}:#{path_to :root}"
|
28
|
+
end
|
29
|
+
|
30
|
+
get posts_path do
|
31
|
+
"#{title_path :posts, :published}:#{path_to :posts}"
|
32
|
+
end
|
33
|
+
|
34
|
+
get posts_path "/:year/:month/:day/:permalink" do |year, month, day, permalink|
|
35
|
+
"#{title_path :posts}:" + path_to(:posts, "#{@date.to_s.gsub('-','/')}/#{permalink}")
|
36
|
+
end
|
37
|
+
|
38
|
+
get archive_path do
|
39
|
+
"#{title_path :archive}:#{path_to :archive}"
|
40
|
+
end
|
41
|
+
|
42
|
+
get archive_path "/:year/:month/:day/:permalink" do |year, month, day, permalink|
|
43
|
+
"#{title_path :archive}:" + path_to(:archive, "#{@date.to_s.gsub('-','/')}/#{permalink}")
|
44
|
+
end
|
45
|
+
|
46
|
+
get about_path do
|
47
|
+
"#{title_path :about}:#{path_to :about}"
|
48
|
+
end
|
49
|
+
|
50
|
+
get search_path do
|
51
|
+
<<-end_content.gsub(/^ /,'')
|
52
|
+
#{title_path :search}:#{path_to :search, :keywords => 'ruby'}
|
53
|
+
#{link_to "Search", :search, :title => 'Search'}
|
54
|
+
#{link_to "Search", :search, :title => 'Search', :keywords => 'ruby'}
|
55
|
+
end_content
|
56
|
+
end
|
57
|
+
|
58
|
+
get drafts_path do
|
59
|
+
<<-end_content.gsub(/^ /,'')
|
60
|
+
#{title_path :drafts}:#{path_to [:drafts, :posts]}
|
61
|
+
#{link_to "Unpublished", :drafts, :posts, :title => 'Unpublished'}
|
62
|
+
end_content
|
63
|
+
end
|
@@ -3,7 +3,6 @@ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../../lib/")
|
|
3
3
|
require 'rubygems'
|
4
4
|
require 'sinatra/base'
|
5
5
|
require 'sinatra/mapping'
|
6
|
-
require 'sinatra/mapping_helpers'
|
7
6
|
|
8
7
|
class Sinatra::Base
|
9
8
|
def env
|
@@ -14,7 +13,6 @@ end
|
|
14
13
|
class ModularApplication < Sinatra::Base
|
15
14
|
|
16
15
|
register Sinatra::Mapping
|
17
|
-
helpers Sinatra::MappingHelpers
|
18
16
|
|
19
17
|
map :root, "blog" # root_path => /blog/
|
20
18
|
map :about # about_path => /blog/about
|
@@ -47,23 +47,23 @@ class ModularTestMapping < Test::Unit::TestCase
|
|
47
47
|
def test_should_return_ok_in_root_path
|
48
48
|
get app.root_path do |response|
|
49
49
|
assert response.ok?
|
50
|
-
assert_equal "http://example.org#{@
|
51
|
-
assert_equal "Blog path:#{@link_paths[:root_path]}", response.body
|
50
|
+
assert_equal "http://example.org#{@root_paths[:root_path]}", last_request.url
|
51
|
+
assert_equal "Blog path:#{@link_paths[:root_path]}/", response.body
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
55
|
def test_should_return_ok_in_posts_path
|
56
56
|
get app.posts_path do |response|
|
57
57
|
assert response.ok?
|
58
|
-
assert_equal "http://example.org#{@
|
58
|
+
assert_equal "http://example.org#{@root_paths[:posts_path]}", last_request.url
|
59
59
|
assert_equal "Articles published:#{@link_paths[:posts_path]}", response.body
|
60
60
|
end
|
61
61
|
|
62
|
-
|
63
|
-
get
|
62
|
+
path_id = "#{@date.to_s.gsub('-','/')}/post-permalink"
|
63
|
+
get app.posts_path path_id do |response|
|
64
64
|
assert response.ok?
|
65
|
-
assert_equal "http://example.org
|
66
|
-
assert_equal "Articles
|
65
|
+
assert_equal "http://example.org#{@root_paths[:posts_path]}#{path_id}/", last_request.url
|
66
|
+
assert_equal "Articles:#{@link_paths[:posts_path]}/#{path_id}", response.body
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
@@ -74,11 +74,11 @@ class ModularTestMapping < Test::Unit::TestCase
|
|
74
74
|
assert_equal "Archive articles:#{@link_paths[:archive_path]}", response.body
|
75
75
|
end
|
76
76
|
|
77
|
-
|
78
|
-
get
|
77
|
+
path_id = "#{@date.to_s.gsub('-','/')}/post-permalink"
|
78
|
+
get app.archive_path path_id do |response|
|
79
79
|
assert response.ok?
|
80
|
-
assert_equal "http://example.org
|
81
|
-
assert_equal "Archive articles
|
80
|
+
assert_equal "http://example.org#{@root_paths[:archive_path]}#{path_id}/", last_request.url
|
81
|
+
assert_equal "Archive articles:#{@link_paths[:archive_path]}/#{path_id}", response.body
|
82
82
|
end
|
83
83
|
end
|
84
84
|
|
@@ -91,19 +91,20 @@ class ModularTestMapping < Test::Unit::TestCase
|
|
91
91
|
end
|
92
92
|
|
93
93
|
def test_should_return_ok_in_search_path
|
94
|
-
|
94
|
+
path_params = "keywords=ruby"
|
95
|
+
get app.search_path, :keywords => "ruby" do |response|
|
95
96
|
assert response.ok?
|
96
|
-
assert_equal "http://example.org#{@
|
97
|
-
assert_equal "Find articles:#{@
|
98
|
-
assert_equal "<a href=\"#{@
|
99
|
-
assert_equal "<a href=\"#{@
|
97
|
+
assert_equal "http://example.org#{@root_paths[:search_path]}?keywords=ruby", last_request.url
|
98
|
+
assert_equal "Find articles:#{@link_paths[:search_path]}?keywords=ruby", response.body.split("\n")[0]
|
99
|
+
assert_equal "<a href=\"#{@link_paths[:search_path]}\" title=\"Search\">Search</a>", response.body.split("\n")[1]
|
100
|
+
assert_equal "<a href=\"#{@link_paths[:search_path]}?keywords=ruby\" title=\"Search\">Search</a>", response.body.split("\n")[2]
|
100
101
|
end
|
101
102
|
end
|
102
103
|
|
103
104
|
def test_should_check_path_method_with_array_params
|
104
105
|
get app.drafts_path do |response|
|
105
106
|
assert response.ok?
|
106
|
-
assert_equal "Unpublished:#{@link_paths[:drafts_path]}/articles"
|
107
|
+
assert_equal "Unpublished:#{@link_paths[:drafts_path]}/articles", response.body.split("\n")[0]
|
107
108
|
body_link = "<a href=\"#{@link_paths[:drafts_path]}/articles\" title=\"Unpublished\">Unpublished</a>"
|
108
109
|
assert_equal body_link, response.body.split("\n")[1]
|
109
110
|
end
|
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.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hallison Batista
|
@@ -20,7 +20,7 @@ dependencies:
|
|
20
20
|
requirements:
|
21
21
|
- - ">="
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version:
|
23
|
+
version: 0.9.1
|
24
24
|
version:
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: sinatra
|
@@ -30,7 +30,7 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0.9.
|
33
|
+
version: 0.9.1.1
|
34
34
|
version:
|
35
35
|
description: Sinatra mapping extension is a minimal module that is useful for create map names for Sinatra web application.
|
36
36
|
email: email@hallisonbatista.com
|
@@ -49,10 +49,10 @@ files:
|
|
49
49
|
- Rakefile
|
50
50
|
- VERSION
|
51
51
|
- lib/sinatra/mapping.rb
|
52
|
-
- lib/sinatra/mapping_helpers.rb
|
53
52
|
- sinatra-mapping.gemspec
|
53
|
+
- test/classic_mapping_test.rb
|
54
|
+
- test/fixtures/classic_application.rb
|
54
55
|
- test/fixtures/modular_application.rb
|
55
|
-
- test/helper.rb
|
56
56
|
- test/modular_mapping_test.rb
|
57
57
|
has_rdoc: true
|
58
58
|
homepage: http://sinatra-mapping.rubyforge.org/
|
@@ -83,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
83
|
requirements: []
|
84
84
|
|
85
85
|
rubyforge_project: sinatra-mapping
|
86
|
-
rubygems_version: 1.3.
|
86
|
+
rubygems_version: 1.3.5
|
87
87
|
signing_key:
|
88
88
|
specification_version: 3
|
89
89
|
summary: Sinatra mapping extension for web application.
|
@@ -1,114 +0,0 @@
|
|
1
|
-
module Sinatra
|
2
|
-
|
3
|
-
# This module contains several helper methods for paths written using
|
4
|
-
# {map}[:link:./Sinatra/Mapping.html#map] method.
|
5
|
-
#
|
6
|
-
# For use this helper, add the following syntax in application source:
|
7
|
-
#
|
8
|
-
# register Sinatra::MappingHelpers
|
9
|
-
module MappingHelpers
|
10
|
-
|
11
|
-
# Creates a title using a path mapped. Otherwise, returns just arguments
|
12
|
-
# joined by spaces and capitalised.
|
13
|
-
#
|
14
|
-
# # In Sinatra application
|
15
|
-
#
|
16
|
-
# map :posts, "articles"
|
17
|
-
# map :tags, "labels"
|
18
|
-
# map :archive, "archive/articles"
|
19
|
-
#
|
20
|
-
# # In views
|
21
|
-
#
|
22
|
-
# <%=title_path :posts%>
|
23
|
-
# # => "Articles"
|
24
|
-
#
|
25
|
-
# <%=title_path :tags%>
|
26
|
-
# # => "Labels"
|
27
|
-
#
|
28
|
-
# <%=title_path :archive%>
|
29
|
-
# # => "Archive articles"
|
30
|
-
def title_path(path, *args)
|
31
|
-
title = (options.locations[path] || path).to_s.gsub('/',' ').strip
|
32
|
-
title.gsub!(/\W/,' ') # Cleanup
|
33
|
-
(args.empty? ? title : "#{title} #{args.join(' ')}").strip.capitalize
|
34
|
-
end
|
35
|
-
|
36
|
-
# Creates anchor links for name and extract path and HTML options from
|
37
|
-
# arguments. Example:
|
38
|
-
#
|
39
|
-
# # In Sinatra application, add a map.
|
40
|
-
#
|
41
|
-
# map :tasks, "tasks"
|
42
|
-
# map :status, "tasks/status"
|
43
|
-
#
|
44
|
-
# get tasks_path do
|
45
|
-
# erb :tasks, :locals => { :name => params.values.join(', ') }
|
46
|
-
# end
|
47
|
-
#
|
48
|
-
# get status_path do
|
49
|
-
# erb :status, :locals => { :status => "finished" }
|
50
|
-
# end
|
51
|
-
#
|
52
|
-
# # In status view, add a link to status map.
|
53
|
-
#
|
54
|
-
# <%= link_to "All finished", :status, status %>
|
55
|
-
# # => <a href="/tasks/status/finished">All finished</a>
|
56
|
-
#
|
57
|
-
# <%= link_to "All finished", :status, :name => status %>
|
58
|
-
# # => <a href="/tasks/status?name=finished">All finished</a>
|
59
|
-
def link_to(name = nil, *args)
|
60
|
-
options = args.last.kind_of?(Hash) ? args.pop : {}
|
61
|
-
url = args.shift if args.first.to_s =~ /^\w.*?:/
|
62
|
-
args << extract_query_attributes(options)
|
63
|
-
"<a href=\"#{url || path_to(*args)}\"#{extract_link_attributes(options)}>#{name || url}</a>"
|
64
|
-
end
|
65
|
-
|
66
|
-
# Returns all paths with query parameters. Example:
|
67
|
-
#
|
68
|
-
# # In Sinatra application:
|
69
|
-
#
|
70
|
-
# map :post, "articles"
|
71
|
-
# map :tags, "labels"
|
72
|
-
#
|
73
|
-
# # Use the following instructions:
|
74
|
-
#
|
75
|
-
# path_to :tags, "ruby", :posts
|
76
|
-
# # => "/labels/ruby/articles"
|
77
|
-
def path_to(*args)
|
78
|
-
options.build_path_to(env['SCRIPT_NAME'], *args)
|
79
|
-
end
|
80
|
-
|
81
|
-
private
|
82
|
-
|
83
|
-
# Extract all tag attributes from a hash keys and values.
|
84
|
-
def extract_link_attributes(hash)
|
85
|
-
select_link_attributes(hash).map do |attribute, value|
|
86
|
-
" #{attribute}=\"#{value}\""
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
# Returns only attributes for link tag.
|
91
|
-
def select_link_attributes(hash)
|
92
|
-
hash.select{ |key, value| link_attributes.include?key }
|
93
|
-
end
|
94
|
-
|
95
|
-
# Select all keys and values that not included in link attributes.
|
96
|
-
def extract_query_attributes(hash)
|
97
|
-
query = hash.select{ |key, value| !link_attributes.include?key }.flatten
|
98
|
-
Hash[*query] unless query.empty?
|
99
|
-
end
|
100
|
-
|
101
|
-
# Attribute list for link tag.
|
102
|
-
def link_attributes
|
103
|
-
[:accesskey, :charset, :coords, :hreflang, :id, :lang, :name, :onblur,
|
104
|
-
:onclick, :ondblclick, :onfocus, :onkeydown, :onkeypress, :onkeyup,
|
105
|
-
:onmousedown, :onmousemove, :onmouseout, :onmouseover, :onmouseup,
|
106
|
-
:rel, :rev, :shape, :style, :tabindex, :target, :title, :type]
|
107
|
-
end
|
108
|
-
|
109
|
-
end # module MappingHelpers
|
110
|
-
|
111
|
-
helpers MappingHelpers
|
112
|
-
|
113
|
-
end # module Sinatra
|
114
|
-
|