Files

HttpRouter::Util

Public Class Methods

add_path_generation(target, route, path, path_validation_regex = nil) click to toggle source
# File lib/http_router/util.rb, line 3
def self.add_path_generation(target, route, path, path_validation_regex = nil)
  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 path_validation_regex
        code << part
        dynamic = true
      else
        regex << (route.matches_with[part[1, part.size].to_sym] || '.*?').to_s unless path_validation_regex
        code << "\#{args.shift || (options && options.delete(:#{part[1, part.size]})) || return}"
        dynamic = true
      end
    else
      regex << Regexp.quote(part) unless path_validation_regex
      code << part
    end
  end
  path_validation_regex ||= Regexp.new("^#{regex}$") if dynamic
  if path_validation_regex
    target.instance_eval         def raw_url(args, options)          url = \"#{code}\"          #{path_validation_regex.inspect}.match(url) ? url : nil        end, __FILE__, __LINE__ + 1
  else
    target.instance_eval         def raw_url(args, options)          \"#{code}\"        end, __FILE__, __LINE__ + 1
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.