Class: Regexp

Inherits:
Object show all
Defined in:
lib/action_controller/routing/routing_ext.rb

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.optionalize(pattern) ⇒ Object



32
33
34
35
36
37
# File 'lib/action_controller/routing/routing_ext.rb', line 32

def optionalize(pattern)
  case unoptionalize(pattern)
    when /\A(.|\(.*\))\Z/ then "#{pattern}?"
    else "(?:#{pattern})?"
  end
end

.unoptionalize(pattern) ⇒ Object



39
40
41
42
43
44
# File 'lib/action_controller/routing/routing_ext.rb', line 39

def unoptionalize(pattern)
  [/\A\(\?:(.*)\)\?\Z/, /\A(.|\(.*\))\?\Z/].each do |regexp|
    return $1 if regexp =~ pattern
  end
  return pattern
end

Instance Method Details

#number_of_capturesObject



27
28
29
# File 'lib/action_controller/routing/routing_ext.rb', line 27

def number_of_captures
  Regexp.new("|#{source}").match('').captures.length
end