提交 f28788b2 编写于 作者: A Aaron Patterson

only look up the format option from the hash once

上级 7a25444f
......@@ -75,13 +75,15 @@ def initialize(scope, path, options)
@default_controller = options[:controller] || scope[:controller]
@default_action = options[:action] || scope[:action]
path = normalize_path! path, options[:format]
formatted = options[:format]
path = normalize_path! path, formatted
ast = path_ast path
path_params = path_params ast
@options = normalize_options!(options, path_params, ast)
normalize_requirements!(path_params)
@options = normalize_options!(options, formatted, path_params, ast)
normalize_requirements!(path_params, formatted)
normalize_conditions!(path_params, path, ast)
normalize_defaults!
normalize_defaults!(formatted)
end
def to_route
......@@ -106,10 +108,10 @@ def optional_format?(path, format)
format != false && !path.include?(':format') && !path.end_with?('/')
end
def normalize_options!(options, path_params, path_ast)
def normalize_options!(options, formatted, path_params, path_ast)
# Add a constraint for wildcard route to make it non-greedy and match the
# optional format part of the route by default
if options[:format] != false
if formatted != false
path_ast.grep(Journey::Nodes::Star) do |node|
options[node.name.to_sym] ||= /.+?/
end
......@@ -132,19 +134,19 @@ def normalize_options!(options, path_params, path_ast)
end
end
def normalize_requirements!(path_params)
def normalize_requirements!(path_params, formatted)
constraints.each do |key, requirement|
next unless path_params.include?(key) || key == :controller
verify_regexp_requirement(requirement) if requirement.is_a?(Regexp)
@requirements[key] = requirement
end
if options[:format] == true
if formatted == true
@requirements[:format] ||= /.+/
elsif Regexp === options[:format]
@requirements[:format] = options[:format]
elsif String === options[:format]
@requirements[:format] = Regexp.compile(options[:format])
elsif Regexp === formatted
@requirements[:format] = formatted
elsif String === formatted
@requirements[:format] = Regexp.compile(formatted)
end
end
......@@ -158,7 +160,7 @@ def verify_regexp_requirement(requirement)
end
end
def normalize_defaults!
def normalize_defaults!(formatted)
@defaults.merge!(scope[:defaults]) if scope[:defaults]
@defaults.merge!(options[:defaults]) if options[:defaults]
......@@ -178,10 +180,10 @@ def normalize_defaults!
verify_callable_constraint(options[:constraints])
end
if Regexp === options[:format]
if Regexp === formatted
@defaults[:format] = nil
elsif String === options[:format]
@defaults[:format] = options[:format]
elsif String === formatted
@defaults[:format] = formatted
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册