Permalink
Browse files

reuse path formatter from the non-optimized path.

The optimized and non-optimized path share more code now without
significant performance degretation
  • Loading branch information...
1 parent 931ee41 commit 4d1b3a13129eb7d5fdb1564706acbcd25a5ffa9b @tenderlove tenderlove committed May 21, 2014
@@ -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)

0 comments on commit 4d1b3a1

Please sign in to comment.