rack-mount 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -35,7 +35,7 @@ module Rack::Mount
35
35
 
36
36
  def process_key(requirements, method, requirement)
37
37
  if requirement.is_a?(Regexp)
38
- expression = parse_regexp(requirement)
38
+ expression = Utils.parse_regexp(requirement)
39
39
  expression = expression.reject { |e| e.is_a?(Reginald::Anchor) }
40
40
 
41
41
  if expression.is_a?(Reginald::Expression) && expression.literal?
@@ -50,19 +50,13 @@ module Rack::Mount
50
50
  @report ||= begin
51
51
  possible_keys.each { |keys| keys.each_pair { |key, _| @key_frequency << key } }
52
52
  return [] if @key_frequency.count <= 1
53
- @key_frequency.select_upper
53
+ @key_frequency.keys_above_mean
54
54
  end
55
55
  end
56
56
 
57
57
  def expire!
58
58
  @possible_keys = @report = nil
59
59
  end
60
-
61
- private
62
- def parse_regexp(regexp)
63
- @parse_regexp_cache ||= {}
64
- @parse_regexp_cache[regexp] ||= Utils.parse_regexp(regexp).freeze
65
- end
66
60
  end
67
61
  end
68
62
  end
@@ -6,20 +6,56 @@ module Rack::Mount
6
6
  def initialize
7
7
  @count = 0
8
8
  super(0)
9
+ expire_caches!
9
10
  end
10
11
 
11
12
  def <<(value)
12
13
  @count += 1
13
14
  self[value] += 1 if value
15
+ expire_caches!
16
+ self
14
17
  end
15
18
 
16
- def select_upper
17
- values = sort_by { |_, value| value }
18
- values.reverse!
19
- values = values.select { |_, value| value >= count / size }
20
- values.map! { |key, _| key }
21
- values
19
+ def sorted_by_frequency
20
+ sort_by { |_, value| value }.reverse!
22
21
  end
22
+
23
+ def max
24
+ @max ||= values.max || 0
25
+ end
26
+
27
+ def min
28
+ @min ||= values.min || 0
29
+ end
30
+
31
+ def mid_range
32
+ @mid_range ||= calculate_mid_range
33
+ end
34
+
35
+ def mean
36
+ @mean ||= calculate_mean
37
+ end
38
+
39
+ def keys_above_mean
40
+ @keys_above_mean ||= compute_keys_above_mean
41
+ end
42
+
43
+ private
44
+ def calculate_mid_range
45
+ (max + min) / 2
46
+ end
47
+
48
+ def calculate_mean
49
+ count / size
50
+ end
51
+
52
+ def compute_keys_above_mean
53
+ sorted_by_frequency.select { |_, value| value >= mean }.map! { |key, _| key }
54
+ end
55
+
56
+ def expire_caches!
57
+ @max = @min = @mid_range = @mean = @keys_above_mean = nil
58
+ end
23
59
  end
24
60
  end
25
61
  end
@@ -39,7 +39,7 @@ module Rack::Mount
39
39
  end
40
40
 
41
41
  def separators(key)
42
- @boundaries[key].select_upper
42
+ @boundaries[key].keys_above_mean
43
43
  end
44
44
 
45
45
  def process_key(requirements, method, requirement)
@@ -57,7 +57,7 @@ module Rack::Mount
57
57
  def analyze_capture_boundaries(regexp, boundaries) #:nodoc:
58
58
  return boundaries unless regexp.is_a?(Regexp)
59
59
 
60
- parts = parse_regexp(regexp)
60
+ parts = Utils.parse_regexp(regexp)
61
61
  parts.each_with_index do |part, index|
62
62
  if part.is_a?(Reginald::Group)
63
63
  if index > 0
@@ -88,7 +88,7 @@ module Rack::Mount
88
88
  def generate_split_keys(regexp, separators) #:nodoc:
89
89
  segments = []
90
90
  buf = nil
91
- parts = parse_regexp(regexp)
91
+ parts = Utils.parse_regexp(regexp)
92
92
  parts.each_with_index do |part, index|
93
93
  case part
94
94
  when Reginald::Anchor
@@ -99,6 +99,12 @@ module Rack::Mount
99
99
  module_function :normalize_extended_expression
100
100
 
101
101
  def parse_regexp(regexp)
102
+ cache = Thread.current[:rack_mount_utils_parse_regexp_cache] ||= {}
103
+
104
+ if expression = cache[regexp]
105
+ return expression
106
+ end
107
+
102
108
  unless regexp.is_a?(RegexpWithNamedGroups)
103
109
  regexp = RegexpWithNamedGroups.new(regexp)
104
110
  end
@@ -119,6 +125,7 @@ module Rack::Mount
119
125
  tag_captures.call(expression)
120
126
  end
121
127
 
128
+ cache[regexp] = expression.freeze
122
129
  expression
123
130
  rescue Racc::ParseError, Reginald::Parser::ScanError
124
131
  []
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-mount
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Peek
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-24 00:00:00 -06:00
12
+ date: 2010-01-06 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency