| 1 |
module ActionController |
|---|
| 2 |
module Routing |
|---|
| 3 |
class Segment |
|---|
| 4 |
RESERVED_PCHAR = ':@&=+$,;' |
|---|
| 5 |
UNSAFE_PCHAR = Regexp.new("[^#{URI::REGEXP::PATTERN::UNRESERVED}#{RESERVED_PCHAR}]", false, 'N').freeze |
|---|
| 6 |
|
|---|
| 7 |
attr_accessor :is_optional |
|---|
| 8 |
alias_method :optional?, :is_optional |
|---|
| 9 |
|
|---|
| 10 |
def initialize |
|---|
| 11 |
self.is_optional = false |
|---|
| 12 |
end |
|---|
| 13 |
|
|---|
| 14 |
def extraction_code |
|---|
| 15 |
nil |
|---|
| 16 |
end |
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
def continue_string_structure(prior_segments) |
|---|
| 20 |
if prior_segments.empty? |
|---|
| 21 |
interpolation_statement(prior_segments) |
|---|
| 22 |
else |
|---|
| 23 |
new_priors = prior_segments[0..-2] |
|---|
| 24 |
prior_segments.last.string_structure(new_priors) |
|---|
| 25 |
end |
|---|
| 26 |
end |
|---|
| 27 |
|
|---|
| 28 |
def interpolation_chunk |
|---|
| 29 |
URI.escape(value, UNSAFE_PCHAR) |
|---|
| 30 |
end |
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
def interpolation_statement(prior_segments) |
|---|
| 34 |
chunks = prior_segments.collect { |s| s.interpolation_chunk } |
|---|
| 35 |
chunks << interpolation_chunk |
|---|
| 36 |
"\"#{chunks * ''}\"#{all_optionals_available_condition(prior_segments)}" |
|---|
| 37 |
end |
|---|
| 38 |
|
|---|
| 39 |
def string_structure(prior_segments) |
|---|
| 40 |
optional? ? continue_string_structure(prior_segments) : interpolation_statement(prior_segments) |
|---|
| 41 |
end |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
def all_optionals_available_condition(prior_segments) |
|---|
| 46 |
optional_locals = prior_segments.collect { |s| s.local_name if s.optional? && s.respond_to?(:local_name) }.compact |
|---|
| 47 |
optional_locals.empty? ? nil : " if #{optional_locals * ' && '}" |
|---|
| 48 |
end |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
def match_extraction(next_capture) |
|---|
| 53 |
nil |
|---|
| 54 |
end |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
def optionality_implied? |
|---|
| 61 |
false |
|---|
| 62 |
end |
|---|
| 63 |
end |
|---|
| 64 |
|
|---|
| 65 |
class StaticSegment < Segment |
|---|
| 66 |
attr_accessor :value, :raw |
|---|
| 67 |
alias_method :raw?, :raw |
|---|
| 68 |
|
|---|
| 69 |
def initialize(value = nil) |
|---|
| 70 |
super() |
|---|
| 71 |
self.value = value |
|---|
| 72 |
end |
|---|
| 73 |
|
|---|
| 74 |
def interpolation_chunk |
|---|
| 75 |
raw? ? value : super |
|---|
| 76 |
end |
|---|
| 77 |
|
|---|
| 78 |
def regexp_chunk |
|---|
| 79 |
chunk = Regexp.escape(value) |
|---|
| 80 |
optional? ? Regexp.optionalize(chunk) : chunk |
|---|
| 81 |
end |
|---|
| 82 |
|
|---|
| 83 |
def build_pattern(pattern) |
|---|
| 84 |
escaped = Regexp.escape(value) |
|---|
| 85 |
if optional? && ! pattern.empty? |
|---|
| 86 |
"(?:#{Regexp.optionalize escaped}\\Z|#{escaped}#{Regexp.unoptionalize pattern})" |
|---|
| 87 |
elsif optional? |
|---|
| 88 |
Regexp.optionalize escaped |
|---|
| 89 |
else |
|---|
| 90 |
escaped + pattern |
|---|
| 91 |
end |
|---|
| 92 |
end |
|---|
| 93 |
|
|---|
| 94 |
def to_s |
|---|
| 95 |
value |
|---|
| 96 |
end |
|---|
| 97 |
end |
|---|
| 98 |
|
|---|
| 99 |
class DividerSegment < StaticSegment |
|---|
| 100 |
def initialize(value = nil) |
|---|
| 101 |
super(value) |
|---|
| 102 |
self.raw = true |
|---|
| 103 |
self.is_optional = true |
|---|
| 104 |
end |
|---|
| 105 |
|
|---|
| 106 |
def optionality_implied? |
|---|
| 107 |
true |
|---|
| 108 |
end |
|---|
| 109 |
end |
|---|
| 110 |
|
|---|
| 111 |
class DynamicSegment < Segment |
|---|
| 112 |
attr_accessor :key, :default, :regexp |
|---|
| 113 |
|
|---|
| 114 |
def initialize(key = nil, options = {}) |
|---|
| 115 |
super() |
|---|
| 116 |
self.key = key |
|---|
| 117 |
self.default = options[:default] if options.key? :default |
|---|
| 118 |
self.is_optional = true if options[:optional] || options.key?(:default) |
|---|
| 119 |
end |
|---|
| 120 |
|
|---|
| 121 |
def to_s |
|---|
| 122 |
":#{key}" |
|---|
| 123 |
end |
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
def local_name |
|---|
| 127 |
"#{key}_value" |
|---|
| 128 |
end |
|---|
| 129 |
|
|---|
| 130 |
def extract_value |
|---|
| 131 |
"#{local_name} = hash[:#{key}] && hash[:#{key}].to_param #{"|| |
|---|
| 132 |
end |
|---|
| 133 |
def value_check |
|---|
| 134 |
if default |
|---|
| 135 |
"#{value_regexp.inspect} =~ #{local_name}" if regexp |
|---|
| 136 |
elsif optional? |
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
"#{local_name}.nil? || #{value_regexp.inspect} =~ #{local_name}" if regexp |
|---|
| 140 |
else |
|---|
| 141 |
"#{local_name} #{"&& |
|---|
| 142 |
end |
|---|
| 143 |
end |
|---|
| 144 |
def expiry_statement |
|---|
| 145 |
"expired, hash = true, options if !expired && expire_on[:#{key}]" |
|---|
| 146 |
end |
|---|
| 147 |
|
|---|
| 148 |
def extraction_code |
|---|
| 149 |
s = extract_value |
|---|
| 150 |
vc = value_check |
|---|
| 151 |
s << "\nreturn [nil,nil] unless #{vc}" if vc |
|---|
| 152 |
s << "\n#{expiry_statement}" |
|---|
| 153 |
end |
|---|
| 154 |
|
|---|
| 155 |
def interpolation_chunk(value_code = "#{local_name}") |
|---|
| 156 |
"\#{URI.escape(#{value_code}.to_s, ActionController::Routing::Segment::UNSAFE_PCHAR)}" |
|---|
| 157 |
end |
|---|
| 158 |
|
|---|
| 159 |
def string_structure(prior_segments) |
|---|
| 160 |
if optional? |
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
"if #{local_name} == #{default.inspect}\n" + |
|---|
| 165 |
continue_string_structure(prior_segments) + |
|---|
| 166 |
"\nelse\n" + |
|---|
| 167 |
"#{interpolation_statement(prior_segments)}\nend" |
|---|
| 168 |
else |
|---|
| 169 |
interpolation_statement(prior_segments) |
|---|
| 170 |
end |
|---|
| 171 |
end |
|---|
| 172 |
|
|---|
| 173 |
def value_regexp |
|---|
| 174 |
Regexp.new "\\A#{regexp.to_s}\\Z" if regexp |
|---|
| 175 |
end |
|---|
| 176 |
|
|---|
| 177 |
def regexp_chunk |
|---|
| 178 |
if regexp |
|---|
| 179 |
if regexp_has_modifiers? |
|---|
| 180 |
"(#{regexp.to_s})" |
|---|
| 181 |
else |
|---|
| 182 |
"(#{regexp.source})" |
|---|
| 183 |
end |
|---|
| 184 |
else |
|---|
| 185 |
"([^#{Routing::SEPARATORS.join}]+)" |
|---|
| 186 |
end |
|---|
| 187 |
end |
|---|
| 188 |
|
|---|
| 189 |
def build_pattern(pattern) |
|---|
| 190 |
chunk = regexp_chunk |
|---|
| 191 |
chunk = "(#{chunk})" if Regexp.new(chunk).number_of_captures == 0 |
|---|
| 192 |
pattern = "#{chunk}#{pattern}" |
|---|
| 193 |
optional? ? Regexp.optionalize(pattern) : pattern |
|---|
| 194 |
end |
|---|
| 195 |
|
|---|
| 196 |
def match_extraction(next_capture) |
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
default_value = default ? default.inspect : nil |
|---|
| 200 |
%[ |
|---|
| 201 |
value = if (m = match[ |
|---|
| 202 |
URI.unescape(m) |
|---|
| 203 |
else |
|---|
| 204 |
|
|---|
| 205 |
end |
|---|
| 206 |
params[: |
|---|
| 207 |
] |
|---|
| 208 |
end |
|---|
| 209 |
|
|---|
| 210 |
def optionality_implied? |
|---|
| 211 |
[:action, :id].include? key |
|---|
| 212 |
end |
|---|
| 213 |
|
|---|
| 214 |
def regexp_has_modifiers? |
|---|
| 215 |
regexp.options & (Regexp::IGNORECASE | Regexp::EXTENDED) != 0 |
|---|
| 216 |
end |
|---|
| 217 |
|
|---|
| 218 |
end |
|---|
| 219 |
|
|---|
| 220 |
class ControllerSegment < DynamicSegment |
|---|
| 221 |
def regexp_chunk |
|---|
| 222 |
possible_names = Routing.possible_controllers.collect { |name| Regexp.escape name } |
|---|
| 223 |
"(?i-:(#{(regexp || Regexp.union(*possible_names)).source}))" |
|---|
| 224 |
end |
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
def interpolation_chunk(value_code = "#{local_name}") |
|---|
| 228 |
"\#{#{value_code}.to_s}" |
|---|
| 229 |
end |
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
def extract_value |
|---|
| 234 |
"#{local_name} = (hash[:#{key}] #{"|| |
|---|
| 235 |
end |
|---|
| 236 |
|
|---|
| 237 |
def match_extraction(next_capture) |
|---|
| 238 |
if default |
|---|
| 239 |
"params[:#{key}] = match[#{next_capture}] ? match[#{next_capture}].downcase : '#{default}'" |
|---|
| 240 |
else |
|---|
| 241 |
"params[:#{key}] = match[#{next_capture}].downcase if match[#{next_capture}]" |
|---|
| 242 |
end |
|---|
| 243 |
end |
|---|
| 244 |
end |
|---|
| 245 |
|
|---|
| 246 |
class PathSegment < DynamicSegment |
|---|
| 247 |
RESERVED_PCHAR = "#{Segment::RESERVED_PCHAR}/" |
|---|
| 248 |
UNSAFE_PCHAR = Regexp.new("[^#{URI::REGEXP::PATTERN::UNRESERVED}#{RESERVED_PCHAR}]", false, 'N').freeze |
|---|
| 249 |
|
|---|
| 250 |
def interpolation_chunk(value_code = "#{local_name}") |
|---|
| 251 |
"\#{URI.escape(#{value_code}.to_s, ActionController::Routing::PathSegment::UNSAFE_PCHAR)}" |
|---|
| 252 |
end |
|---|
| 253 |
|
|---|
| 254 |
def default |
|---|
| 255 |
'' |
|---|
| 256 |
end |
|---|
| 257 |
|
|---|
| 258 |
def default=(path) |
|---|
| 259 |
raise RoutingError, "paths cannot have non-empty default values" unless path.blank? |
|---|
| 260 |
end |
|---|
| 261 |
|
|---|
| 262 |
def match_extraction(next_capture) |
|---|
| 263 |
"params[:#{key}] = PathSegment::Result.new_escaped((match[#{next_capture}]#{" || " + default.inspect if default}).split('/'))#{" if match[" + next_capture + "]" if !default}" |
|---|
| 264 |
end |
|---|
| 265 |
|
|---|
| 266 |
def regexp_chunk |
|---|
| 267 |
regexp || "(.*)" |
|---|
| 268 |
end |
|---|
| 269 |
|
|---|
| 270 |
def optionality_implied? |
|---|
| 271 |
true |
|---|
| 272 |
end |
|---|
| 273 |
|
|---|
| 274 |
class Result < ::Array |
|---|
| 275 |
def to_s() join '/' end |
|---|
| 276 |
def self.new_escaped(strings) |
|---|
| 277 |
new strings.collect {|str| URI.unescape str} |
|---|
| 278 |
end |
|---|
| 279 |
end |
|---|
| 280 |
end |
|---|
| 281 |
end |
|---|
| 282 |
end |
|---|