aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/regexp_with_named_groups.rb
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2010-06-27 13:11:49 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2010-06-27 16:28:04 -0700
commit654929190170c174c8b844d0adcd968c3049d515 (patch)
treedaf1571d1f1d82e1b4cee204bf8baa2fea4f2696 /actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/regexp_with_named_groups.rb
parentd15256af6cb160d0cdd76695db22872f09d6e835 (diff)
downloadrails-654929190170c174c8b844d0adcd968c3049d515.tar.gz
rails-654929190170c174c8b844d0adcd968c3049d515.tar.bz2
rails-654929190170c174c8b844d0adcd968c3049d515.zip
Vendor unreleased rack-mount 0.6.6.pre dependency
Diffstat (limited to 'actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/regexp_with_named_groups.rb')
-rw-r--r--actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/regexp_with_named_groups.rb69
1 files changed, 69 insertions, 0 deletions
diff --git a/actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/regexp_with_named_groups.rb b/actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/regexp_with_named_groups.rb
new file mode 100644
index 0000000000..c11292b2a2
--- /dev/null
+++ b/actionpack/lib/action_dispatch/vendor/rack-mount-0.6.6.pre/rack/mount/regexp_with_named_groups.rb
@@ -0,0 +1,69 @@
+module Rack::Mount
+ if Regin.regexp_supports_named_captures?
+ RegexpWithNamedGroups = Regexp
+ else
+ require 'strscan'
+
+ # A wrapper that adds shim named capture support to older
+ # versions of Ruby.
+ #
+ # Because the named capture syntax causes a parse error, an
+ # alternate syntax is used to indicate named captures.
+ #
+ # Ruby 1.9+ named capture syntax:
+ #
+ # /(?<foo>[a-z]+)/
+ #
+ # Ruby 1.8 shim syntax:
+ #
+ # /(?:<foo>[a-z]+)/
+ class RegexpWithNamedGroups < Regexp
+ def self.new(regexp) #:nodoc:
+ if regexp.is_a?(RegexpWithNamedGroups)
+ regexp
+ else
+ super
+ end
+ end
+
+ # Wraps Regexp with named capture support.
+ def initialize(regexp)
+ regexp = Regexp.compile(regexp) unless regexp.is_a?(Regexp)
+ source, options = regexp.source, regexp.options
+ @names, scanner = [], StringScanner.new(source)
+
+ while scanner.skip_until(/\(/)
+ if scanner.scan(/\?:<([^>]+)>/)
+ @names << scanner[1]
+ elsif scanner.scan(/\?(i?m?x?\-?i?m?x?)?:/)
+ # ignore noncapture
+ else
+ @names << nil
+ end
+ end
+ source.gsub!(/\?:<([^>]+)>/, '')
+
+ @names = [] unless @names.any?
+ @names.freeze
+
+ super(source, options)
+ end
+
+ def names
+ @names.dup
+ end
+
+ def named_captures
+ named_captures = {}
+ names.each_with_index { |n, i|
+ named_captures[n] = [i+1] if n
+ }
+ named_captures
+ end
+
+ def eql?(other)
+ super && @names.eql?(other.names)
+ end
+ end
+ end
+end