Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #31 from bogdan/refactor

Formatter#generate: decrese amount of Arrays
  • Loading branch information...
commit 8f7d3b04748fb28d69d6ee9ab0c7b372bc6c10af 2 parents c1ac1e6 + 6687901
Aaron Patterson authored May 25, 2012
17  lib/journey/formatter.rb
@@ -10,22 +10,19 @@ def initialize routes
10 10
       @cache  = nil
11 11
     end
12 12
 
13  
-    def generate key, name, options, recall = {}, parameterize = nil
  13
+    def generate type, name, options, recall = {}, parameterize = nil
14 14
       constraints = recall.merge options
15 15
 
16 16
       match_route(name, constraints) do |route|
17  
-        data = constraints.dup
18 17
 
19 18
         keys_to_keep = route.parts.reverse.drop_while { |part|
20 19
           !options.key?(part) || (options[part] || recall[part]).nil?
21  
-        } | route.required_parts
  20
+        }
22 21
 
23  
-        (data.keys - keys_to_keep).each do |bad_key|
24  
-          data.delete bad_key
  22
+        parameterized_parts = constraints.dup.keep_if do |key, _|
  23
+          keys_to_keep.include?(key) || route.required_parts.include?(key)
25 24
         end
26 25
 
27  
-        parameterized_parts = data.dup
28  
-
29 26
         if parameterize
30 27
           parameterized_parts.each do |k,v|
31 28
             parameterized_parts[k] = parameterize.call(k, v)
@@ -36,9 +33,11 @@ def generate key, name, options, recall = {}, parameterize = nil
36 33
 
37 34
         next unless verify_required_parts!(route, parameterized_parts)
38 35
 
39  
-        z = Hash[options.to_a - data.to_a - route.defaults.to_a]
  36
+        params = options.dup.delete_if do |key, _|
  37
+          parameterized_parts.key?(key) || route.defaults.key?(key)
  38
+        end
40 39
 
41  
-        return [route.format(parameterized_parts), z]
  40
+        return [route.format(parameterized_parts), params]
42 41
       end
43 42
 
44 43
       raise Router::RoutingError
4  lib/journey/route.rb
@@ -67,8 +67,8 @@ def parts
67 67
     alias :segment_keys :parts
68 68
 
69 69
     def format path_options
70  
-      (defaults.keys - required_parts).each do |key|
71  
-        path_options.delete key if defaults[key].to_s == path_options[key].to_s
  70
+      path_options.delete_if do |key, value|
  71
+        value.to_s == defaults[key].to_s && !required_parts.include?(key)
72 72
       end
73 73
 
74 74
       Visitors::Formatter.new(path_options).accept(path.spec)

0 notes on commit 8f7d3b0

Please sign in to comment.
Something went wrong with that request. Please try again.