提交 4d1b3a13 编写于 作者: A Aaron Patterson

reuse path formatter from the non-optimized path.

The optimized and non-optimized path share more code now without
significant performance degretation
上级 931ee418
......@@ -76,10 +76,6 @@ def format(path_options)
@path_formatter.evaluate path_options
end
def optimized_path
Visitors::OptimizedPath.new.accept(path.spec)
end
def optional_parts
path.optional_names.map { |n| n.to_sym }
end
......
......@@ -38,7 +38,8 @@ def evaluate(hash)
@parameters.each do |index|
param = parts[index]
value = hash.fetch(param.name) { return ''.freeze }
value = hash[param.name]
return ''.freeze unless value
parts[index] = param.escape value
end
......@@ -149,34 +150,6 @@ def visit_GROUP(node)
end
end
class OptimizedPath < Visitor # :nodoc:
def accept(node)
Array(visit(node))
end
private
def visit_CAT(node)
[visit(node.left), visit(node.right)].flatten
end
def visit_SYMBOL(node)
node.left[1..-1].to_sym
end
def visit_STAR(node)
visit(node.left)
end
def visit_GROUP(node)
[]
end
%w{ LITERAL SLASH DOT }.each do |t|
class_eval %{ def visit_#{t}(n); n.left; end }, __FILE__, __LINE__
end
end
class Dot < Visitor # :nodoc:
def initialize
@nodes = []
......
......@@ -168,7 +168,6 @@ def initialize(route, options)
@klass = Journey::Router::Utils
@required_parts = @route.required_parts
@arg_size = @required_parts.size
@optimized_path = @route.optimized_path
end
def call(t, args)
......@@ -191,11 +190,7 @@ def optimized_helper(args)
raise_generation_error(params, missing_keys)
end
@optimized_path.map{ |segment| replace_segment(params, segment) }.join
end
def replace_segment(params, segment)
Symbol === segment ? @klass.escape_segment(params[segment]) : segment
@route.format params
end
def optimize_routes_generation?(t)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册