rack-mount 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rack/mount/generatable_regexp.rb +50 -8
- data/lib/rack/mount/generation/route.rb +14 -28
- data/lib/rack/mount/route.rb +3 -0
- metadata +2 -2
@@ -32,17 +32,34 @@ module Rack::Mount
|
|
32
32
|
obj.segments
|
33
33
|
end
|
34
34
|
|
35
|
+
def defaults=(defaults)
|
36
|
+
@required_captures = nil
|
37
|
+
@required_params = nil
|
38
|
+
@required_defaults = nil
|
39
|
+
@defaults = defaults
|
40
|
+
end
|
41
|
+
|
42
|
+
def defaults
|
43
|
+
@defaults ||= {}
|
44
|
+
end
|
45
|
+
|
35
46
|
def generatable?
|
36
47
|
segments.any?
|
37
48
|
end
|
38
49
|
|
39
|
-
def generate(params = {}, recall = {},
|
50
|
+
def generate(params = {}, recall = {}, options = {})
|
51
|
+
return nil unless generatable?
|
52
|
+
|
40
53
|
merged = recall.merge(params)
|
41
|
-
|
54
|
+
return nil unless required_params.all? { |p| merged.include?(p) }
|
55
|
+
return nil unless required_defaults.all? { |k, v| merged[k] == v }
|
56
|
+
|
57
|
+
generate_from_segments(segments, params, merged, options)
|
42
58
|
end
|
43
59
|
|
44
60
|
def segments
|
45
61
|
@segments ||= begin
|
62
|
+
defaults
|
46
63
|
segments = []
|
47
64
|
catch(:halt) do
|
48
65
|
expression = Utils.parse_regexp(self)
|
@@ -57,7 +74,32 @@ module Rack::Mount
|
|
57
74
|
end
|
58
75
|
|
59
76
|
def required_captures
|
60
|
-
segments.find_all { |s|
|
77
|
+
@required_captures ||= segments.find_all { |s|
|
78
|
+
s.is_a?(DynamicSegment) && !@defaults.include?(s.name)
|
79
|
+
}.freeze
|
80
|
+
end
|
81
|
+
|
82
|
+
def required_params
|
83
|
+
@required_params ||= required_captures.map { |s| s.name }.freeze
|
84
|
+
end
|
85
|
+
|
86
|
+
def required_defaults
|
87
|
+
@required_defaults ||= begin
|
88
|
+
required_defaults = @defaults.dup
|
89
|
+
captures.inject({}) { |h, s| h.merge!(s.to_hash) }.keys.each { |name|
|
90
|
+
required_defaults.delete(name)
|
91
|
+
}
|
92
|
+
required_defaults
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def freeze
|
97
|
+
segments
|
98
|
+
captures
|
99
|
+
required_captures
|
100
|
+
required_params
|
101
|
+
required_defaults
|
102
|
+
super
|
61
103
|
end
|
62
104
|
|
63
105
|
private
|
@@ -93,7 +135,7 @@ module Rack::Mount
|
|
93
135
|
|
94
136
|
EMPTY_STRING = ''.freeze
|
95
137
|
|
96
|
-
def generate_from_segments(segments, params, merged,
|
138
|
+
def generate_from_segments(segments, params, merged, options, optional = false)
|
97
139
|
if optional
|
98
140
|
return EMPTY_STRING if segments.all? { |s| s.is_a?(String) }
|
99
141
|
return EMPTY_STRING unless segments.flatten.any? { |s|
|
@@ -101,11 +143,11 @@ module Rack::Mount
|
|
101
143
|
}
|
102
144
|
return EMPTY_STRING if segments.any? { |segment|
|
103
145
|
if segment.is_a?(DynamicSegment)
|
104
|
-
value = merged[segment.name] || defaults[segment.name]
|
146
|
+
value = merged[segment.name] || @defaults[segment.name]
|
105
147
|
value = parameterize(segment.name, value, options)
|
106
148
|
|
107
149
|
merged_value = parameterize(segment.name, merged[segment.name], options)
|
108
|
-
default_value = parameterize(segment.name, defaults[segment.name], options)
|
150
|
+
default_value = parameterize(segment.name, @defaults[segment.name], options)
|
109
151
|
|
110
152
|
if value.nil? || segment !~ value
|
111
153
|
true
|
@@ -124,7 +166,7 @@ module Rack::Mount
|
|
124
166
|
when String
|
125
167
|
segment
|
126
168
|
when DynamicSegment
|
127
|
-
value = params[segment.name] || merged[segment.name] || defaults[segment.name]
|
169
|
+
value = params[segment.name] || merged[segment.name] || @defaults[segment.name]
|
128
170
|
value = parameterize(segment.name, value, options)
|
129
171
|
if value && segment =~ value.to_s
|
130
172
|
value
|
@@ -132,7 +174,7 @@ module Rack::Mount
|
|
132
174
|
return
|
133
175
|
end
|
134
176
|
when Array
|
135
|
-
value = generate_from_segments(segment, params, merged,
|
177
|
+
value = generate_from_segments(segment, params, merged, options, true)
|
136
178
|
if value == :clear_remaining_segments
|
137
179
|
segment.each { |s| params.delete(s.name) if s.is_a?(DynamicSegment) }
|
138
180
|
EMPTY_STRING
|
@@ -3,29 +3,18 @@ require 'rack/mount/utils'
|
|
3
3
|
module Rack::Mount
|
4
4
|
module Generation
|
5
5
|
module Route #:nodoc:
|
6
|
-
attr_reader :generation_keys
|
7
|
-
|
8
6
|
def initialize(*args)
|
9
7
|
super
|
10
8
|
|
11
|
-
@
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
@conditions.each do |method, condition|
|
16
|
-
@required_params[method] = @conditions[method].required_captures.map { |s| s.name }.reject { |s| @defaults.include?(s) }.freeze
|
17
|
-
@required_defaults[method] = @defaults.dup
|
18
|
-
@conditions[method].captures.inject({}) { |h, s| h.merge!(s.to_hash) }.keys.each { |name|
|
19
|
-
@required_defaults[method].delete(name)
|
20
|
-
@generation_keys.delete(name) if @defaults.include?(name)
|
21
|
-
}
|
22
|
-
@required_defaults[method].freeze
|
23
|
-
end
|
24
|
-
@has_significant_params = (@required_params.any? { |k, v| v.any? } || @required_defaults.any? { |k, v| v.any? }) ? true : false
|
9
|
+
@has_significant_params = @conditions.any? { |method, condition|
|
10
|
+
condition.required_params.any? || condition.required_defaults.any?
|
11
|
+
}
|
12
|
+
end
|
25
13
|
|
26
|
-
|
27
|
-
@
|
28
|
-
|
14
|
+
def generation_keys
|
15
|
+
@conditions.inject({}) { |keys, (method, condition)|
|
16
|
+
keys.merge!(condition.required_defaults)
|
17
|
+
}
|
29
18
|
end
|
30
19
|
|
31
20
|
def significant_params?
|
@@ -34,9 +23,9 @@ module Rack::Mount
|
|
34
23
|
|
35
24
|
def generate(methods, params = {}, recall = {}, options = {})
|
36
25
|
if methods.is_a?(Array)
|
37
|
-
result = methods.map { |m| generate_method(m, params, recall,
|
26
|
+
result = methods.map { |m| generate_method(m, params, recall, options) || (return nil) }
|
38
27
|
else
|
39
|
-
result = generate_method(methods, params, recall,
|
28
|
+
result = generate_method(methods, params, recall, options)
|
40
29
|
end
|
41
30
|
|
42
31
|
if result
|
@@ -49,13 +38,10 @@ module Rack::Mount
|
|
49
38
|
end
|
50
39
|
|
51
40
|
private
|
52
|
-
def generate_method(method, params, recall,
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
return nil unless @required_params[method].all? { |p| merged.include?(p) }
|
57
|
-
return nil unless @required_defaults[method].all? { |k, v| merged[k] == v }
|
58
|
-
condition.generate(params, recall, defaults, options)
|
41
|
+
def generate_method(method, params, recall, options)
|
42
|
+
if condition = @conditions[method]
|
43
|
+
condition.generate(params, recall, options)
|
44
|
+
end
|
59
45
|
end
|
60
46
|
end
|
61
47
|
end
|
data/lib/rack/mount/route.rb
CHANGED
@@ -54,8 +54,11 @@ module Rack::Mount
|
|
54
54
|
|
55
55
|
pattern = Regexp.compile("\\A#{Regexp.escape(pattern)}\\Z") if pattern.is_a?(String)
|
56
56
|
pattern = Utils.normalize_extended_expression(pattern)
|
57
|
+
|
57
58
|
pattern = RegexpWithNamedGroups.new(pattern)
|
58
59
|
pattern.extend(GeneratableRegexp::InstanceMethods)
|
60
|
+
pattern.defaults = @defaults
|
61
|
+
|
59
62
|
@conditions[method] = pattern.freeze
|
60
63
|
end
|
61
64
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-mount
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua Peek
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-12-
|
12
|
+
date: 2009-12-03 00:00:00 -06:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|