padrino-relative 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/padrino-relative.rb +78 -80
- metadata +1 -1
data/lib/padrino-relative.rb
CHANGED
@@ -2,92 +2,90 @@
|
|
2
2
|
# File is padrino-core/lib/padrino-core/application/routing.rb
|
3
3
|
# Modified based on https://github.com/kylewlacy/padrino-framework
|
4
4
|
module Padrino
|
5
|
-
module
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
5
|
+
module Relative
|
6
|
+
def parse_route(path, options, verb)
|
7
|
+
# We need save our originals path/options so we can perform correctly cache.
|
8
|
+
original = [path, options.dup]
|
9
|
+
|
10
|
+
# options for the route directly
|
11
|
+
route_options = {}
|
12
|
+
|
13
|
+
# We need check if path is a symbol, if that it's a named route
|
14
|
+
map = options.delete(:map)
|
15
|
+
|
16
|
+
relative_path = false
|
17
|
+
if map == :index
|
18
|
+
map = '/'
|
19
|
+
relative_path = true
|
20
|
+
end
|
21
|
+
|
22
|
+
if path.kind_of?(Symbol) # path i.e :index or :show
|
23
|
+
name = path # The route name
|
24
|
+
path = map ? map.dup : (path == :index ? '/' : path.to_s) # The route path
|
25
|
+
end
|
26
|
+
|
27
|
+
# Build our controller
|
28
|
+
controller = Array(@_controller).map { |c| c.to_s }
|
29
|
+
|
30
|
+
case path
|
31
|
+
when String # path i.e "/index" or "/show"
|
32
|
+
# Now we need to parse our 'with' params
|
33
|
+
if with_params = options.delete(:with)
|
34
|
+
path = process_path_for_with_params(path, with_params)
|
35
|
+
end
|
22
36
|
|
23
|
-
|
24
|
-
|
25
|
-
path = map ? map.dup : (path == :index ? '/' : path.to_s) # The route path
|
26
|
-
end
|
37
|
+
# Now we need to parse our provides
|
38
|
+
options.delete(:provides) if options[:provides].nil?
|
27
39
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
# Now we need to parse our 'with' params
|
34
|
-
if with_params = options.delete(:with)
|
35
|
-
path = process_path_for_with_params(path, with_params)
|
36
|
-
end
|
37
|
-
|
38
|
-
# Now we need to parse our provides
|
39
|
-
options.delete(:provides) if options[:provides].nil?
|
40
|
-
|
41
|
-
if @_use_format or format_params = options[:provides]
|
42
|
-
process_path_for_provides(path, format_params)
|
43
|
-
options[:matching] ||= {}
|
44
|
-
options[:matching][:format] = /[^\.]+/
|
45
|
-
end
|
46
|
-
|
47
|
-
absolute_map = map && map[0] == ?/
|
48
|
-
|
49
|
-
unless controller.empty?
|
50
|
-
# Now we need to add our controller path only if not mapped directly
|
51
|
-
if !absolute_map or relative_path
|
52
|
-
controller_path = controller.join("/")
|
53
|
-
path.gsub!(%r{^\(/\)|/\?}, "")
|
54
|
-
path = File.join(controller_path, path)
|
55
|
-
end
|
56
|
-
# Here we build the correct name route
|
57
|
-
end
|
58
|
-
|
59
|
-
# Now we need to parse our 'parent' params and parent scope
|
60
|
-
if !absolute_map and parent_params = options.delete(:parent) || @_parents
|
61
|
-
parent_params = Array(@_parents) + Array(parent_params)
|
62
|
-
path = process_path_for_parent_params(path, parent_params)
|
63
|
-
end
|
64
|
-
|
65
|
-
# Add any controller level map to the front of the path
|
66
|
-
path = "#{@_map}/#{path}".squeeze('/') unless absolute_map or @_map.blank?
|
67
|
-
|
68
|
-
# Small reformats
|
69
|
-
path.gsub!(%r{/\?$}, '(/)') # Remove index path
|
70
|
-
path.gsub!(%r{//$}, '/') # Remove index path
|
71
|
-
path[0,0] = "/" if path !~ %r{^\(?/} # Paths must start with a /
|
72
|
-
path.sub!(%r{/(\))?$}, '\\1') if path != "/" # Remove latest trailing delimiter
|
73
|
-
path.gsub!(/\/(\(\.|$)/, '\\1') # Remove trailing slashes
|
74
|
-
path.squeeze!('/')
|
75
|
-
when Regexp
|
76
|
-
route_options[:path_for_generation] = options.delete(:generate_with) if options.key?(:generate_with)
|
77
|
-
end
|
40
|
+
if @_use_format or format_params = options[:provides]
|
41
|
+
process_path_for_provides(path, format_params)
|
42
|
+
options[:matching] ||= {}
|
43
|
+
options[:matching][:format] = /[^\.]+/
|
44
|
+
end
|
78
45
|
|
79
|
-
|
80
|
-
name = options.delete(:name) if name.nil? && options.key?(:name)
|
81
|
-
if name
|
82
|
-
controller_name = controller.join("_")
|
83
|
-
name = "#{controller_name}_#{name}".to_sym unless controller_name.blank?
|
84
|
-
end
|
46
|
+
absolute_map = map && map[0] == ?/
|
85
47
|
|
86
|
-
|
87
|
-
|
48
|
+
unless controller.empty?
|
49
|
+
# Now we need to add our controller path only if not mapped directly
|
50
|
+
if !absolute_map or relative_path
|
51
|
+
controller_path = controller.join("/")
|
52
|
+
path.gsub!(%r{^\(/\)|/\?}, "")
|
53
|
+
path = File.join(controller_path, path)
|
54
|
+
end
|
55
|
+
# Here we build the correct name route
|
56
|
+
end
|
88
57
|
|
89
|
-
|
58
|
+
# Now we need to parse our 'parent' params and parent scope
|
59
|
+
if !absolute_map and parent_params = options.delete(:parent) || @_parents
|
60
|
+
parent_params = Array(@_parents) + Array(parent_params)
|
61
|
+
path = process_path_for_parent_params(path, parent_params)
|
90
62
|
end
|
63
|
+
|
64
|
+
# Add any controller level map to the front of the path
|
65
|
+
path = "#{@_map}/#{path}".squeeze('/') unless absolute_map or @_map.blank?
|
66
|
+
|
67
|
+
# Small reformats
|
68
|
+
path.gsub!(%r{/\?$}, '(/)') # Remove index path
|
69
|
+
path.gsub!(%r{//$}, '/') # Remove index path
|
70
|
+
path[0,0] = "/" if path !~ %r{^\(?/} # Paths must start with a /
|
71
|
+
path.sub!(%r{/(\))?$}, '\\1') if path != "/" # Remove latest trailing delimiter
|
72
|
+
path.gsub!(/\/(\(\.|$)/, '\\1') # Remove trailing slashes
|
73
|
+
path.squeeze!('/')
|
74
|
+
when Regexp
|
75
|
+
route_options[:path_for_generation] = options.delete(:generate_with) if options.key?(:generate_with)
|
76
|
+
end
|
77
|
+
|
78
|
+
name = options.delete(:route_name) if name.nil? && options.key?(:route_name)
|
79
|
+
name = options.delete(:name) if name.nil? && options.key?(:name)
|
80
|
+
if name
|
81
|
+
controller_name = controller.join("_")
|
82
|
+
name = "#{controller_name}_#{name}".to_sym unless controller_name.blank?
|
83
|
+
end
|
84
|
+
|
85
|
+
# Merge in option defaults
|
86
|
+
options.reverse_merge!(:default_values => @_defaults)
|
87
|
+
|
88
|
+
[path, name, options, route_options]
|
91
89
|
end
|
92
90
|
end
|
93
91
|
end
|