def initialize(route, path, validation_regex = nil)
@route = route
@path = path.dup
@param_names = []
if path.is_a?(String)
path[0, 0] = '/' unless path[0] == /
regex_parts = path.split(/([:\*][a-zA-Z0-9_]+)/)
regex, code = '', ''
dynamic = false
regex_parts.each_with_index do |part, index|
case part[0]
when :, *
if index != 0 && regex_parts[index - 1][-1] == \ regex << Regexp.quote(part) unless validation_regex
code << part
dynamic = true
else
regex << (@route.matches_with(part[1, part.size].to_sym) || '.*?').to_s unless validation_regex
code << "\#{args.shift || (options && options.delete(:#{part[1, part.size]})) || return}"
dynamic = true
end
else
regex << Regexp.quote(part) unless validation_regex
code << part
end
end
validation_regex ||= Regexp.new("^#{regex}$") if dynamic
if validation_regex
instance_eval " def generate(args, options)
generated_path = \"#{code}\"
#{validation_regex.inspect}.match(generated_path) ? URI.escape(generated_path) : nil
end
", __FILE__, __LINE__ + 1
else
instance_eval " def generate(args, options)
URI.escape(\"#{code}\")
end
", __FILE__, __LINE__ + 1
end
end
end