Skip to content
This repository

Formatter#generate: decrese amount of Arrays #31

Merged
merged 2 commits into from about 2 years ago

2 participants

Bogdan Gusiev Aaron Patterson
Bogdan Gusiev
bogdan commented

Running benchmark with current working tree
Checkout HEAD^
Running benchmark with HEAD^
Checkout to previous HEAD again

                    user     system      total        real
---------------------------------------------------generate
After patch:    0.000000   0.000000   0.000000 (  0.004518)
Before patch:   0.010000   0.000000   0.010000 (  0.005044)
Aaron Patterson tenderlove merged commit 8f7d3b0 into from
Aaron Patterson tenderlove closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.

Showing 2 changed files with 10 additions and 11 deletions. Show diff stats Hide diff stats

  1. +8 9 lib/journey/formatter.rb
  2. +2 2 lib/journey/route.rb
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 formatter = Visitors::Formatter.new(path_options)

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.