sinatra-mapping 1.0.3 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +4 -0
- data/VERSION +1 -1
- data/lib/sinatra/mapping.rb +13 -7
- data/lib/sinatra/mapping_helpers.rb +10 -4
- data/test/test_mapping.rb +1 -1
- metadata +1 -1
data/CHANGES
CHANGED
data/VERSION
CHANGED
data/lib/sinatra/mapping.rb
CHANGED
@@ -23,10 +23,9 @@ module Sinatra
|
|
23
23
|
# map :root, "tasks" # => /tasks/
|
24
24
|
# map :changes, "last-changes # => /tasks/last-changes
|
25
25
|
def map(name, path = nil)
|
26
|
-
@env ||= {}
|
27
26
|
@locations ||= {}
|
28
27
|
if name.to_sym == :root
|
29
|
-
@locations[:root] = cleanup_paths("/#{
|
28
|
+
@locations[:root] = cleanup_paths("/#{path}/")
|
30
29
|
metadef "#{name}_path" do |*paths|
|
31
30
|
@locations[:root]
|
32
31
|
end
|
@@ -64,10 +63,10 @@ module Sinatra
|
|
64
63
|
end
|
65
64
|
|
66
65
|
# Returns URL path with query instructions.
|
67
|
-
def
|
66
|
+
def build_path_to(script_name = nil, *args)
|
68
67
|
args.compact!
|
69
68
|
query = args.pop if args.last.kind_of?(Hash)
|
70
|
-
path = map_path_to(*args)
|
69
|
+
path = map_path_to(script_name, *args)
|
71
70
|
path << "?" << Rack::Utils::build_query(query) if query
|
72
71
|
path
|
73
72
|
end
|
@@ -77,16 +76,22 @@ module Sinatra
|
|
77
76
|
# Check arguments. If argument is a symbol and exist map path before
|
78
77
|
# setted, then return path mapped by symbol name.
|
79
78
|
def map_path_to(*args)
|
80
|
-
|
79
|
+
script_name = args.shift if args.first.to_s =~ /\/\w.*/
|
80
|
+
path_mapped(script_name, *locations_get_from(*args))
|
81
81
|
end
|
82
82
|
|
83
83
|
# Returns all paths mapped by root path in prefix.
|
84
|
-
def path_mapped(*args)
|
85
|
-
!args.empty?
|
84
|
+
def path_mapped(script_name, *args)
|
85
|
+
if !args.empty?
|
86
|
+
cleanup_paths("/#{script_name}/#{@locations[:root]}/#{args.join('/')}")
|
87
|
+
else
|
88
|
+
cleanup_paths("/#{script_name}/#{@locations[:root]}")
|
89
|
+
end
|
86
90
|
end
|
87
91
|
|
88
92
|
# Get paths from location maps.
|
89
93
|
def locations_get_from(*args)
|
94
|
+
args.delete(:root)
|
90
95
|
args.collect do |path|
|
91
96
|
@locations.has_key?(path) ? @locations[path] : path
|
92
97
|
end
|
@@ -94,6 +99,7 @@ module Sinatra
|
|
94
99
|
|
95
100
|
# Clean all duplicated slashes.
|
96
101
|
def cleanup_paths(*paths)
|
102
|
+
#.gsub(%r{#{@locations[:root]}/#{@locations[:root]}}, @locations[:root])
|
97
103
|
paths.join('/').gsub(/[\/]{2,}/,'/')
|
98
104
|
end
|
99
105
|
|
@@ -32,8 +32,12 @@ module Sinatra
|
|
32
32
|
# # => <a href="/tasks/status/finished">All finished</a>
|
33
33
|
def link_to(name = nil, *args)
|
34
34
|
options = args.last.kind_of?(Hash) ? args.pop : {}
|
35
|
-
url = args.shift if args.first.to_s =~ /^\w
|
36
|
-
"<a href=\"#{url || path_to(*args)}\" #{extract_tags_attributes
|
35
|
+
url = args.shift if args.first.to_s =~ /^\w.*?:/
|
36
|
+
"<a href=\"#{url || path_to(*args)}\" #{extract_tags_attributes(options)}>#{name || url}</a>"
|
37
|
+
end
|
38
|
+
|
39
|
+
def link_to_query(name, action, query = {}, options = {})
|
40
|
+
"<a href=\"#{path_to(action,query)}\" #{extract_tags_attributes(options)}>#{name}</a>"
|
37
41
|
end
|
38
42
|
|
39
43
|
# Returns all paths with query parameters. Example:
|
@@ -45,7 +49,7 @@ module Sinatra
|
|
45
49
|
# path_to :tags, "ruby", :posts
|
46
50
|
# # returns "/labels/ruby/articles"
|
47
51
|
def path_to(*args)
|
48
|
-
options.
|
52
|
+
options.build_path_to(env['SCRIPT_NAME'], *args)
|
49
53
|
end
|
50
54
|
|
51
55
|
private
|
@@ -67,7 +71,9 @@ module Sinatra
|
|
67
71
|
end
|
68
72
|
end
|
69
73
|
|
70
|
-
end # module
|
74
|
+
end # module MappingHelpers
|
75
|
+
|
76
|
+
helpers MappingHelpers
|
71
77
|
|
72
78
|
end # module Sinatra
|
73
79
|
|
data/test/test_mapping.rb
CHANGED