| 1 |
require 'cgi' |
|---|
| 2 |
require 'uri' |
|---|
| 3 |
require 'action_controller/polymorphic_routes' |
|---|
| 4 |
require 'action_controller/routing_optimisation' |
|---|
| 5 |
|
|---|
| 6 |
class Object |
|---|
| 7 |
def to_param |
|---|
| 8 |
to_s |
|---|
| 9 |
end |
|---|
| 10 |
end |
|---|
| 11 |
|
|---|
| 12 |
class TrueClass |
|---|
| 13 |
def to_param |
|---|
| 14 |
self |
|---|
| 15 |
end |
|---|
| 16 |
end |
|---|
| 17 |
|
|---|
| 18 |
class FalseClass |
|---|
| 19 |
def to_param |
|---|
| 20 |
self |
|---|
| 21 |
end |
|---|
| 22 |
end |
|---|
| 23 |
|
|---|
| 24 |
class NilClass |
|---|
| 25 |
def to_param |
|---|
| 26 |
self |
|---|
| 27 |
end |
|---|
| 28 |
end |
|---|
| 29 |
|
|---|
| 30 |
class Regexp |
|---|
| 31 |
def number_of_captures |
|---|
| 32 |
Regexp.new("|#{source}").match('').captures.length |
|---|
| 33 |
end |
|---|
| 34 |
|
|---|
| 35 |
class << self |
|---|
| 36 |
def optionalize(pattern) |
|---|
| 37 |
case unoptionalize(pattern) |
|---|
| 38 |
when /\A(.|\(.*\))\Z/ then "#{pattern}?" |
|---|
| 39 |
else "(?:#{pattern})?" |
|---|
| 40 |
end |
|---|
| 41 |
end |
|---|
| 42 |
|
|---|
| 43 |
def unoptionalize(pattern) |
|---|
| 44 |
[/\A\(\?:(.*)\)\?\Z/, /\A(.|\(.*\))\?\Z/].each do |regexp| |
|---|
| 45 |
return $1 if regexp =~ pattern |
|---|
| 46 |
end |
|---|
| 47 |
return pattern |
|---|
| 48 |
end |
|---|
| 49 |
end |
|---|
| 50 |
end |
|---|
| 51 |
|
|---|
| 52 |
module ActionController |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
module Routing |
|---|
| 251 |
SEPARATORS = %w( / ; . , ? ) |
|---|
| 252 |
|
|---|
| 253 |
HTTP_METHODS = [:get, :head, :post, :put, :delete] |
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 |
mattr_accessor :controller_paths |
|---|
| 257 |
self.controller_paths = [] |
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
mattr_accessor :optimise_named_routes |
|---|
| 262 |
self.optimise_named_routes = true |
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 |
module Helpers |
|---|
| 266 |
include PolymorphicRoutes |
|---|
| 267 |
end |
|---|
| 268 |
|
|---|
| 269 |
class << self |
|---|
| 270 |
def with_controllers(names) |
|---|
| 271 |
prior_controllers = @possible_controllers |
|---|
| 272 |
use_controllers! names |
|---|
| 273 |
yield |
|---|
| 274 |
ensure |
|---|
| 275 |
use_controllers! prior_controllers |
|---|
| 276 |
end |
|---|
| 277 |
|
|---|
| 278 |
def normalize_paths(paths) |
|---|
| 279 |
|
|---|
| 280 |
paths = paths.collect do |path| |
|---|
| 281 |
path = path. |
|---|
| 282 |
gsub("//", "/"). |
|---|
| 283 |
gsub("\\\\", "\\"). |
|---|
| 284 |
gsub(%r{(.)[\\/]$}, '\1') |
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
re = %r{\w+[/\\]\.\.[/\\]} |
|---|
| 288 |
path.gsub!(%r{\w+[/\\]\.\.[/\\]}, "") while path.match(re) |
|---|
| 289 |
path |
|---|
| 290 |
end |
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 |
paths = paths.uniq.sort_by { |path| - path.length } |
|---|
| 294 |
end |
|---|
| 295 |
|
|---|
| 296 |
def possible_controllers |
|---|
| 297 |
unless @possible_controllers |
|---|
| 298 |
@possible_controllers = [] |
|---|
| 299 |
|
|---|
| 300 |
paths = controller_paths.select { |path| File.directory?(path) && path != "." } |
|---|
| 301 |
|
|---|
| 302 |
seen_paths = Hash.new {|h, k| h[k] = true; false} |
|---|
| 303 |
normalize_paths(paths).each do |load_path| |
|---|
| 304 |
Dir["#{load_path}/**/*_controller.rb"].sort.collect do |path| |
|---|
| 305 |
next if seen_paths[path.gsub(%r{^\.[/\\]}, "")] |
|---|
| 306 |
|
|---|
| 307 |
controller_name = path[(load_path.length + 1)..-1] |
|---|
| 308 |
|
|---|
| 309 |
controller_name.gsub!(/_controller\.rb\Z/, '') |
|---|
| 310 |
@possible_controllers << controller_name |
|---|
| 311 |
end |
|---|
| 312 |
end |
|---|
| 313 |
|
|---|
| 314 |
|
|---|
| 315 |
@possible_controllers.uniq! |
|---|
| 316 |
end |
|---|
| 317 |
@possible_controllers |
|---|
| 318 |
end |
|---|
| 319 |
|
|---|
| 320 |
def use_controllers!(controller_names) |
|---|
| 321 |
@possible_controllers = controller_names |
|---|
| 322 |
end |
|---|
| 323 |
|
|---|
| 324 |
def controller_relative_to(controller, previous) |
|---|
| 325 |
if controller.nil? then previous |
|---|
| 326 |
elsif controller[0] == ?/ then controller[1..-1] |
|---|
| 327 |
elsif %r{^(.*)/} =~ previous then "#{$1}/#{controller}" |
|---|
| 328 |
else controller |
|---|
| 329 |
end |
|---|
| 330 |
end |
|---|
| 331 |
end |
|---|
| 332 |
|
|---|
| 333 |
class Route |
|---|
| 334 |
attr_accessor :segments, :requirements, :conditions, :optimise |
|---|
| 335 |
|
|---|
| 336 |
def initialize |
|---|
| 337 |
@segments = [] |
|---|
| 338 |
@requirements = {} |
|---|
| 339 |
@conditions = {} |
|---|
| 340 |
end |
|---|
| 341 |
|
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 |
def optimise? |
|---|
| 345 |
@optimise |
|---|
| 346 |
end |
|---|
| 347 |
|
|---|
| 348 |
def segment_keys |
|---|
| 349 |
segments.collect do |segment| |
|---|
| 350 |
segment.key if segment.respond_to? :key |
|---|
| 351 |
end.compact |
|---|
| 352 |
end |
|---|
| 353 |
|
|---|
| 354 |
|
|---|
| 355 |
def write_generation |
|---|
| 356 |
|
|---|
| 357 |
body = "expired = false\n#{generation_extraction}\n#{generation_structure}" |
|---|
| 358 |
|
|---|
| 359 |
|
|---|
| 360 |
body = "if #{generation_requirements}\n#{body}\nend" if generation_requirements |
|---|
| 361 |
args = "options, hash, expire_on = {}" |
|---|
| 362 |
|
|---|
| 363 |
|
|---|
| 364 |
raw_method = method_decl = "def generate_raw(#{args})\npath = begin\n#{body}\nend\n[path, hash]\nend" |
|---|
| 365 |
instance_eval method_decl, "generated code (#{__FILE__}:#{__LINE__})" |
|---|
| 366 |
|
|---|
| 367 |
|
|---|
| 368 |
|
|---|
| 369 |
|
|---|
| 370 |
|
|---|
| 371 |
|
|---|
| 372 |
|
|---|
| 373 |
method_decl = "def generate(#{args})\npath, hash = generate_raw(options, hash, expire_on)\nappend_query_string(path, hash, extra_keys(options))\nend" |
|---|
| 374 |
instance_eval method_decl, "generated code (#{__FILE__}:#{__LINE__})" |
|---|
| 375 |
|
|---|
| 376 |
method_decl = "def generate_extras(#{args})\npath, hash = generate_raw(options, hash, expire_on)\n[path, extra_keys(options)]\nend" |
|---|
| 377 |
instance_eval method_decl, "generated code (#{__FILE__}:#{__LINE__})" |
|---|
| 378 |
raw_method |
|---|
| 379 |
end |
|---|
| 380 |
|
|---|
| 381 |
|
|---|
| 382 |
|
|---|
| 383 |
def generation_extraction |
|---|
| 384 |
segments.collect do |segment| |
|---|
| 385 |
segment.extraction_code |
|---|
| 386 |
end.compact * "\n" |
|---|
| 387 |
end |
|---|
| 388 |
|
|---|
| 389 |
|
|---|
| 390 |
|
|---|
| 391 |
def generation_requirements |
|---|
| 392 |
requirement_conditions = requirements.collect do |key, req| |
|---|
| 393 |
if req.is_a? Regexp |
|---|
| 394 |
value_regexp = Regexp.new "\\A#{req.source}\\Z" |
|---|
| 395 |
"hash[:#{key}] && #{value_regexp.inspect} =~ options[:#{key}]" |
|---|
| 396 |
else |
|---|
| 397 |
"hash[:#{key}] == #{req.inspect}" |
|---|
| 398 |
end |
|---|
| 399 |
end |
|---|
| 400 |
requirement_conditions * ' && ' unless requirement_conditions.empty? |
|---|
| 401 |
end |
|---|
| 402 |
|
|---|
| 403 |
def generation_structure |
|---|
| 404 |
segments.last.string_structure segments[0..-2] |
|---|
| 405 |
end |
|---|
| 406 |
|
|---|
| 407 |
|
|---|
| 408 |
def write_recognition |
|---|
| 409 |
|
|---|
| 410 |
body = "params = parameter_shell.dup\n#{recognition_extraction * "\n"}\nparams" |
|---|
| 411 |
body = "if #{recognition_conditions.join(" && ")}\n#{body}\nend" |
|---|
| 412 |
|
|---|
| 413 |
|
|---|
| 414 |
method_decl = "def recognize(path, env={})\n#{body}\nend" |
|---|
| 415 |
instance_eval method_decl, "generated code (#{__FILE__}:#{__LINE__})" |
|---|
| 416 |
method_decl |
|---|
| 417 |
end |
|---|
| 418 |
|
|---|
| 419 |
|
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 |
def recognition_conditions |
|---|
| 423 |
result = ["(match = #{Regexp.new(recognition_pattern).inspect}.match(path))"] |
|---|
| 424 |
result << "conditions[:method] === env[:method]" if conditions[:method] |
|---|
| 425 |
result |
|---|
| 426 |
end |
|---|
| 427 |
|
|---|
| 428 |
|
|---|
| 429 |
def recognition_pattern(wrap = true) |
|---|
| 430 |
pattern = '' |
|---|
| 431 |
segments.reverse_each do |segment| |
|---|
| 432 |
pattern = segment.build_pattern pattern |
|---|
| 433 |
end |
|---|
| 434 |
wrap ? ("\\A" + pattern + "\\Z") : pattern |
|---|
| 435 |
end |
|---|
| 436 |
|
|---|
| 437 |
|
|---|
| 438 |
def recognition_extraction |
|---|
| 439 |
next_capture = 1 |
|---|
| 440 |
extraction = segments.collect do |segment| |
|---|
| 441 |
x = segment.match_extraction(next_capture) |
|---|
| 442 |
next_capture += Regexp.new(segment.regexp_chunk).number_of_captures |
|---|
| 443 |
x |
|---|
| 444 |
end |
|---|
| 445 |
extraction.compact |
|---|
| 446 |
end |
|---|
| 447 |
|
|---|
| 448 |
|
|---|
| 449 |
def generate(options, hash, expire_on = {}) |
|---|
| 450 |
write_generation |
|---|
| 451 |
generate options, hash, expire_on |
|---|
| 452 |
end |
|---|
| 453 |
|
|---|
| 454 |
def generate_extras(options, hash, expire_on = {}) |
|---|
| 455 |
write_generation |
|---|
| 456 |
generate_extras options, hash, expire_on |
|---|
| 457 |
end |
|---|
| 458 |
|
|---|
| 459 |
|
|---|
| 460 |
|
|---|
| 461 |
def append_query_string(path, hash, query_keys=nil) |
|---|
| 462 |
return nil unless path |
|---|
| 463 |
query_keys ||= extra_keys(hash) |
|---|
| 464 |
"#{path}#{build_query_string(hash, query_keys)}" |
|---|
| 465 |
end |
|---|
| 466 |
|
|---|
| 467 |
|
|---|
| 468 |
|
|---|
| 469 |
|
|---|
| 470 |
|
|---|
| 471 |
|
|---|
| 472 |
|
|---|
| 473 |
def extra_keys(hash, recall={}) |
|---|
| 474 |
(hash || {}).keys.map { |k| k.to_sym } - (recall || {}).keys - significant_keys |
|---|
| 475 |
end |
|---|
| 476 |
|
|---|
| 477 |
|
|---|
| 478 |
|
|---|
| 479 |
|
|---|
| 480 |
|
|---|
| 481 |
def build_query_string(hash, only_keys = nil) |
|---|
| 482 |
elements = [] |
|---|
| 483 |
|
|---|
| 484 |
(only_keys || hash.keys).each do |key| |
|---|
| 485 |
if value = hash[key] |
|---|
| 486 |
elements << value.to_query(key) |
|---|
| 487 |
end |
|---|
| 488 |
end |
|---|
| 489 |
|
|---|
| 490 |
elements.empty? ? '' : "?#{elements.sort * '&'}" |
|---|
| 491 |
end |
|---|
| 492 |
|
|---|
| 493 |
|
|---|
| 494 |
def recognize(path, environment={}) |
|---|
| 495 |
write_recognition |
|---|
| 496 |
recognize path, environment |
|---|
| 497 |
end |
|---|
| 498 |
|
|---|
| 499 |
|
|---|
| 500 |
|
|---|
| 501 |
|
|---|
| 502 |
|
|---|
| 503 |
|
|---|
| 504 |
|
|---|
| 505 |
|
|---|
| 506 |
def parameter_shell |
|---|
| 507 |
@parameter_shell ||= returning({}) do |shell| |
|---|
| 508 |
requirements.each do |key, requirement| |
|---|
| 509 |
shell[key] = requirement unless requirement.is_a? Regexp |
|---|
| 510 |
end |
|---|
| 511 |
end |
|---|
| 512 |
end |
|---|
| 513 |
|
|---|
| 514 |
|
|---|
| 515 |
|
|---|
| 516 |
|
|---|
| 517 |
def significant_keys |
|---|
| 518 |
@significant_keys ||= returning [] do |sk| |
|---|
| 519 |
segments.each { |segment| sk << segment.key if segment.respond_to? :key } |
|---|
| 520 |
sk.concat requirements.keys |
|---|
| 521 |
sk.uniq! |
|---|
| 522 |
end |
|---|
| 523 |
end |
|---|
| 524 |
|
|---|
| 525 |
|
|---|
| 526 |
|
|---|
| 527 |
def defaults |
|---|
| 528 |
@defaults ||= returning({}) do |hash| |
|---|
| 529 |
segments.each do |segment| |
|---|
| 530 |
next unless segment.respond_to? :default |
|---|
| 531 |
hash[segment.key] = segment.default unless segment.default.nil? |
|---|
| 532 |
end |
|---|
| 533 |
requirements.each do |key,req| |
|---|
| 534 |
next if Regexp === req || req.nil? |
|---|
| 535 |
hash[key] = req |
|---|
| 536 |
end |
|---|
| 537 |
end |
|---|
| 538 |
end |
|---|
| 539 |
|
|---|
| 540 |
def matches_controller_and_action?(controller, action) |
|---|
| 541 |
unless defined? @matching_prepared |
|---|
| 542 |
@controller_requirement = requirement_for(:controller) |
|---|
| 543 |
@action_requirement = requirement_for(:action) |
|---|
| 544 |
@matching_prepared = true |
|---|
| 545 |
end |
|---|
| 546 |
|
|---|
| 547 |
(@controller_requirement.nil? || @controller_requirement === controller) && |
|---|
| 548 |
(@action_requirement.nil? || @action_requirement === action) |
|---|
| 549 |
end |
|---|
| 550 |
|
|---|
| 551 |
def to_s |
|---|
| 552 |
@to_s ||= begin |
|---|
| 553 |
segs = segments.inject("") { |str,s| str << s.to_s } |
|---|
| 554 |
"%-6s %-40s %s" % [(conditions[:method] || :any).to_s.upcase, segs, requirements.inspect] |
|---|
| 555 |
end |
|---|
| 556 |
end |
|---|
| 557 |
|
|---|
| 558 |
protected |
|---|
| 559 |
def requirement_for(key) |
|---|
| 560 |
return requirements[key] if requirements.key? key |
|---|
| 561 |
segments.each do |segment| |
|---|
| 562 |
return segment.regexp if segment.respond_to?(:key) && segment.key == key |
|---|
| 563 |
end |
|---|
| 564 |
nil |
|---|
| 565 |
end |
|---|
| 566 |
|
|---|
| 567 |
end |
|---|
| 568 |
|
|---|
| 569 |
class Segment |
|---|
| 570 |
RESERVED_PCHAR = ':@&=+$' |
|---|
| 571 |
UNSAFE_PCHAR = Regexp.new("[^#{URI::REGEXP::PATTERN::UNRESERVED}#{RESERVED_PCHAR}]", false, 'N').freeze |
|---|
| 572 |
|
|---|
| 573 |
attr_accessor :is_optional |
|---|
| 574 |
alias_method :optional?, :is_optional |
|---|
| 575 |
|
|---|
| 576 |
def initialize |
|---|
| 577 |
self.is_optional = false |
|---|
| 578 |
end |
|---|
| 579 |
|
|---|
| 580 |
def extraction_code |
|---|
| 581 |
nil |
|---|
| 582 |
end |
|---|
| 583 |
|
|---|
| 584 |
|
|---|
| 585 |
def continue_string_structure(prior_segments) |
|---|
| 586 |
if prior_segments.empty? |
|---|
| 587 |
interpolation_statement(prior_segments) |
|---|
| 588 |
else |
|---|
| 589 |
new_priors = prior_segments[0..-2] |
|---|
| 590 |
prior_segments.last.string_structure(new_priors) |
|---|
| 591 |
end |
|---|
| 592 |
end |
|---|
| 593 |
|
|---|
| 594 |
def interpolation_chunk |
|---|
| 595 |
URI.escape(value, UNSAFE_PCHAR) |
|---|
| 596 |
end |
|---|
| 597 |
|
|---|
| 598 |
|
|---|
| 599 |
def interpolation_statement(prior_segments) |
|---|
| 600 |
chunks = prior_segments.collect { |s| s.interpolation_chunk } |
|---|
| 601 |
chunks << interpolation_chunk |
|---|
| 602 |
"\"#{chunks * ''}\"#{all_optionals_available_condition(prior_segments)}" |
|---|
| 603 |
end |
|---|
| 604 |
|
|---|
| 605 |
def string_structure(prior_segments) |
|---|
| 606 |
optional? ? continue_string_structure(prior_segments) : interpolation_statement(prior_segments) |
|---|
| 607 |
end |
|---|
| 608 |
|
|---|
| 609 |
|
|---|
| 610 |
|
|---|
| 611 |
def all_optionals_available_condition(prior_segments) |
|---|
| 612 |
optional_locals = prior_segments.collect { |s| s.local_name if s.optional? && s.respond_to?(:local_name) }.compact |
|---|
| 613 |
optional_locals.empty? ? nil : " if #{optional_locals * ' && '}" |
|---|
| 614 |
end |
|---|
| 615 |
|
|---|
| 616 |
|
|---|
| 617 |
|
|---|
| 618 |
def match_extraction(next_capture) |
|---|
| 619 |
nil |
|---|
| 620 |
end |
|---|
| 621 |
|
|---|
| 622 |
|
|---|
| 623 |
|
|---|
| 624 |
|
|---|
| 625 |
|
|---|
| 626 |
def optionality_implied? |
|---|
| 627 |
false |
|---|
| 628 |
end |
|---|
| 629 |
end |
|---|
| 630 |
|
|---|
| 631 |
class StaticSegment < Segment |
|---|
| 632 |
attr_accessor :value, :raw |
|---|
| 633 |
alias_method :raw?, :raw |
|---|
| 634 |
|
|---|
| 635 |
def initialize(value = nil) |
|---|
| 636 |
super() |
|---|
| 637 |
self.value = value |
|---|
| 638 |
end |
|---|
| 639 |
|
|---|
| 640 |
def interpolation_chunk |
|---|
| 641 |
raw? ? value : super |
|---|
| 642 |
end |
|---|
| 643 |
|
|---|
| 644 |
def regexp_chunk |
|---|
| 645 |
chunk = Regexp.escape(value) |
|---|
| 646 |
optional? ? Regexp.optionalize(chunk) : chunk |
|---|
| 647 |
end |
|---|
| 648 |
|
|---|
| 649 |
def build_pattern(pattern) |
|---|
| 650 |
escaped = Regexp.escape(value) |
|---|
| 651 |
if optional? && ! pattern.empty? |
|---|
| 652 |
"(?:#{Regexp.optionalize escaped}\\Z|#{escaped}#{Regexp.unoptionalize pattern})" |
|---|
| 653 |
elsif optional? |
|---|
| 654 |
Regexp.optionalize escaped |
|---|
| 655 |
else |
|---|
| 656 |
escaped + pattern |
|---|
| 657 |
end |
|---|
| 658 |
end |
|---|
| 659 |
|
|---|
| 660 |
def to_s |
|---|
| 661 |
value |
|---|
| 662 |
|
|---|