Skip to content

Commit

Permalink
allow generation from regex routes
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbuddy committed Jul 23, 2011
1 parent 51c39b9 commit ee60fb5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
54 changes: 26 additions & 28 deletions lib/http_router/node/path.rb
Expand Up @@ -6,7 +6,7 @@ class Path < Node
def initialize(router, parent, route, path, param_names = [])
@route, @original_path, @param_names, @dynamic = route, path, param_names, !param_names.empty?
raise AmbiguousVariableException, "You have duplicate variable name present: #{param_names.join(', ')}" if param_names.uniq.size != param_names.size
process_path
process_path_for_generation(@original_path) if @original_path.respond_to?(:split)
super router, parent
end

Expand Down Expand Up @@ -58,37 +58,35 @@ def raw_url(args, options)
raise InvalidRouteException
end

def process_path
if @original_path.respond_to?(:split)
regex_parts = @original_path.split(/([:\*][a-zA-Z0-9_]+)/)
path_validation_regex, code = '', ''
regex_parts.each_with_index{ |part, index|
new_part = case part[0]
when ?:, ?*
if index != 0 && regex_parts[index - 1][-1] == ?\\
path_validation_regex << Regexp.quote(part)
code << part
else
path_validation_regex << (route.matches_with[part[1, part.size].to_sym] || '.*?').to_s
code << "\#{args.shift || (options && options.delete(:#{part[1, part.size]})) || return}"
end
else
path_validation_regex << Regexp.quote(part)
def process_path_for_generation(path, path_validation_regex = nil)
@path_validation_regex = path_validation_regex
regex_parts = path.split(/([:\*][a-zA-Z0-9_]+)/)
regex, code = '', ''
regex_parts.each_with_index do |part, index|
case part[0]
when ?:, ?*
if index != 0 && regex_parts[index - 1][-1] == ?\\
regex << Regexp.quote(part)
code << part
else
regex << (route.matches_with[part[1, part.size].to_sym] || '.*?').to_s
code << "\#{args.shift || (options && options.delete(:#{part[1, part.size]})) || return}"
end
new_part
}
@path_validation_regex = Regexp.new("^#{path_validation_regex}$")
if @dynamic
instance_eval <<-EOT, __FILE__, __LINE__ + 1
def raw_url(args, options)
url = \"#{code}\"
#{"url !~ @path_validation_regex ? nil : " if @dynamic} url
end
EOT
else
instance_eval "def raw_url(args, options); \"#{code}\"; end", __FILE__, __LINE__
regex << Regexp.quote(part)
code << part
end
end
@path_validation_regex ||= Regexp.new("^#{regex}$") if dynamic?
if @path_validation_regex
instance_eval <<-EOT, __FILE__, __LINE__ + 1
def raw_url(args, options)
url = \"#{code}\"
#{"url !~ @path_validation_regex ? nil : " if @dynamic} url
end
EOT
else
instance_eval "def raw_url(args, options); \"#{code}\"; end", __FILE__, __LINE__
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/http_router/regex_route.rb
Expand Up @@ -3,6 +3,7 @@ class RegexRoute < Route
def initialize(router, path, opts = {})
@router, @original_path, @opts = router, path, opts
@param_names = @original_path.respond_to?(:names) ? @original_path.names.map(&:to_sym) : []
process_path_for_generation(opts.delete(:path_for_generation), @original_path) if opts.key?(:path_for_generation)
process_opts
end

Expand Down

0 comments on commit ee60fb5

Please sign in to comment.