rack-mount 0.4.6 → 0.4.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,7 +5,11 @@ module Rack::Mount
5
5
  module Splitting
6
6
  NULL = "\0".freeze
7
7
 
8
- class Key < Struct.new(:method, :index, :separators)
8
+ class Key < Array
9
+ def initialize(method, index, separators)
10
+ replace([method, index, separators])
11
+ end
12
+
9
13
  def self.split(value, separator_pattern)
10
14
  keys = value.split(separator_pattern)
11
15
  keys.shift if keys[0] == ''
@@ -14,15 +18,11 @@ module Rack::Mount
14
18
  end
15
19
 
16
20
  def call(cache, obj)
17
- (cache[method] ||= self.class.split(obj.send(method), separators))[index]
21
+ (cache[self[0]] ||= self.class.split(obj.send(self[0]), self[2]))[self[1]]
18
22
  end
19
23
 
20
24
  def call_source(cache, obj)
21
- "(#{cache}[:#{method}] ||= Analysis::Splitting::Key.split(#{obj}.#{method}, #{separators.inspect}))[#{index}]"
22
- end
23
-
24
- def inspect
25
- "#{method}[#{index}]"
25
+ "(#{cache}[:#{self[0]}] ||= Analysis::Splitting::Key.split(#{obj}.#{self[0]}, #{self[2].inspect}))[#{self[1]}]"
26
26
  end
27
27
  end
28
28
 
@@ -4,12 +4,6 @@ module Rack::Mount
4
4
  # metaclass. This allows mixins to be stacked ontop of the instance
5
5
  # methods.
6
6
  module Mixover
7
- def self.extended(klass)
8
- klass.instance_eval do
9
- @extended_modules = []
10
- end
11
- end
12
-
13
7
  module InstanceMethods #:nodoc:
14
8
  def dup
15
9
  obj = super
@@ -21,40 +15,33 @@ module Rack::Mount
21
15
 
22
16
  # Replaces include with a lazy version.
23
17
  def include(*mod)
24
- extended_modules.push(*mod)
25
- end
26
-
27
- def extended_modules
28
- Thread.current[extended_modules_thread_local_key] || @extended_modules
18
+ (@included_modules ||= []).push(*mod)
29
19
  end
30
20
 
31
21
  def new(*args, &block) #:nodoc:
32
22
  obj = allocate
33
23
  obj.extend(InstanceMethods)
34
- extended_modules.each { |mod| obj.extend(mod) }
24
+ (@included_modules ||= []).each { |mod| obj.extend(mod) }
35
25
  obj.send(:initialize, *args, &block)
36
26
  obj
37
27
  end
38
28
 
39
29
  # Create a new class without an included module.
40
30
  def new_without_module(mod, *args, &block)
41
- (Thread.current[extended_modules_thread_local_key] = extended_modules.dup).delete(mod)
31
+ old_included_modules = (@included_modules ||= []).dup
32
+ @included_modules.delete(mod)
42
33
  new(*args, &block)
43
34
  ensure
44
- Thread.current[extended_modules_thread_local_key] = nil
35
+ @included_modules = old_included_modules
45
36
  end
46
37
 
47
38
  # Create a new class temporarily with a module.
48
39
  def new_with_module(mod, *args, &block)
49
- (Thread.current[extended_modules_thread_local_key] = extended_modules.dup).push(*mod)
40
+ old_included_modules = (@included_modules ||= []).dup
41
+ include(mod)
50
42
  new(*args, &block)
51
43
  ensure
52
- Thread.current[extended_modules_thread_local_key] = nil
44
+ @included_modules = old_included_modules
53
45
  end
54
-
55
- private
56
- def extended_modules_thread_local_key
57
- "mixover_extended_modules_#{object_id}"
58
- end
59
46
  end
60
47
  end
@@ -7,29 +7,23 @@ module Rack::Mount
7
7
  SCRIPT_NAME = 'SCRIPT_NAME'.freeze
8
8
  SLASH = '/'.freeze
9
9
 
10
- KEY = 'rack.mount.prefix'.freeze
11
-
12
- def initialize(app, prefix = nil)
10
+ def initialize(app, prefix)
13
11
  @app, @prefix = app, prefix.freeze
14
12
  freeze
15
13
  end
16
14
 
17
15
  def call(env)
18
- if prefix = env[KEY] || @prefix
19
- old_path_info = env[PATH_INFO].dup
20
- old_script_name = env[SCRIPT_NAME].dup
16
+ old_path_info = env[PATH_INFO].dup
17
+ old_script_name = env[SCRIPT_NAME].dup
21
18
 
22
- begin
23
- env[PATH_INFO] = Utils.normalize_path(env[PATH_INFO].sub(prefix, EMPTY_STRING))
24
- env[PATH_INFO] = EMPTY_STRING if env[PATH_INFO] == SLASH
25
- env[SCRIPT_NAME] = Utils.normalize_path(env[SCRIPT_NAME].to_s + prefix)
26
- @app.call(env)
27
- ensure
28
- env[PATH_INFO] = old_path_info
29
- env[SCRIPT_NAME] = old_script_name
30
- end
31
- else
19
+ begin
20
+ env[PATH_INFO] = Utils.normalize_path(env[PATH_INFO].sub(@prefix, EMPTY_STRING))
21
+ env[PATH_INFO] = EMPTY_STRING if env[PATH_INFO] == SLASH
22
+ env[SCRIPT_NAME] = Utils.normalize_path(env[SCRIPT_NAME].to_s + @prefix)
32
23
  @app.call(env)
24
+ ensure
25
+ env[PATH_INFO] = old_path_info
26
+ env[SCRIPT_NAME] = old_script_name
33
27
  end
34
28
  end
35
29
  end
@@ -39,7 +39,6 @@ module Rack::Mount
39
39
 
40
40
  container.each_with_index { |route, i|
41
41
  body << "route = self[#{i}]"
42
- body << 'matches = {}'
43
42
  body << 'params = route.defaults.dup'
44
43
 
45
44
  conditions = []
@@ -47,11 +46,10 @@ module Rack::Mount
47
46
  b = []
48
47
  if condition.is_a?(Regexp)
49
48
  b << "if m = obj.#{method}.match(#{condition.inspect})"
50
- b << "matches[:#{method}] = m"
51
49
  if (named_captures = route.named_captures[method]) && named_captures.any?
52
- b << 'captures = m.captures'
50
+ b << 'matches = m.captures'
53
51
  b << 'p = nil'
54
- b << named_captures.map { |k, j| "params[#{k.inspect}] = p if p = captures[#{j}]" }.join('; ')
52
+ b << named_captures.map { |k, j| "params[#{k.inspect}] = p if p = matches[#{j}]" }.join('; ')
55
53
  end
56
54
  else
57
55
  b << "if m = obj.#{method} == route.conditions[:#{method}]"
@@ -63,7 +61,7 @@ module Rack::Mount
63
61
 
64
62
  body << <<-RUBY
65
63
  if #{conditions.join(' && ')}
66
- yield route, matches, params
64
+ yield route, params
67
65
  end
68
66
  RUBY
69
67
  }
@@ -78,7 +76,7 @@ module Rack::Mount
78
76
 
79
77
  def optimize_recognize!
80
78
  keys = @recognition_keys.map { |key|
81
- if key.respond_to?(:call_source)
79
+ if key.is_a?(Array)
82
80
  key.call_source(:cache, :obj)
83
81
  else
84
82
  "obj.#{key}"
@@ -96,12 +94,12 @@ module Rack::Mount
96
94
  optimize_container_iterator(container) unless container.respond_to?(:optimized_each)
97
95
 
98
96
  if block_given?
99
- container.optimized_each(obj) do |route, matches, params|
100
- yield route, matches, params
97
+ container.optimized_each(obj) do |route, params|
98
+ yield route, params
101
99
  end
102
100
  else
103
- container.optimized_each(obj) do |route, matches, params|
104
- return route, matches, params
101
+ container.optimized_each(obj) do |route, params|
102
+ return route, params
105
103
  end
106
104
  end
107
105
 
@@ -17,31 +17,16 @@ module Rack::Mount
17
17
  }.freeze
18
18
  }
19
19
  @named_captures.freeze
20
-
21
- if @conditions.has_key?(:path_info) &&
22
- !Utils.regexp_anchored?(@conditions[:path_info])
23
- @prefix = true
24
- @app = Prefix.new(@app)
25
- else
26
- @prefix = false
27
- end
28
- end
29
-
30
- def prefix?
31
- @prefix
32
20
  end
33
21
 
34
22
  def recognize(obj)
35
- matches = {}
36
- params = @defaults.dup
37
-
23
+ params = @defaults.dup
38
24
  if @conditions.all? { |method, condition|
39
25
  value = obj.send(method)
40
26
  if condition.is_a?(Regexp) && (m = value.match(condition))
41
- matches[method] = m
42
- captures = m.captures
27
+ matches = m.captures
43
28
  @named_captures[method].each { |k, i|
44
- if v = captures[i]
29
+ if v = matches[i]
45
30
  params[k] = v
46
31
  end
47
32
  }
@@ -52,7 +37,7 @@ module Rack::Mount
52
37
  false
53
38
  end
54
39
  }
55
- return matches, params
40
+ params
56
41
  else
57
42
  nil
58
43
  end
@@ -26,19 +26,18 @@ module Rack::Mount
26
26
 
27
27
  cache = {}
28
28
  keys = @recognition_keys.map { |key|
29
- if key.respond_to?(:call_source)
29
+ if key.is_a?(Array)
30
30
  key.call(cache, obj)
31
31
  else
32
32
  obj.send(key)
33
33
  end
34
34
  }
35
35
  @recognition_graph[*keys].each do |route|
36
- matches, params = route.recognize(obj)
37
- if matches && params
36
+ if params = route.recognize(obj)
38
37
  if block_given?
39
- yield route, matches, params
38
+ yield route, params
40
39
  else
41
- return route, matches, params
40
+ return route, params
42
41
  end
43
42
  end
44
43
  end
@@ -63,14 +62,10 @@ module Rack::Mount
63
62
 
64
63
  request = nil
65
64
  req = @request_class.new(env)
66
- recognize(req) do |route, matches, params|
65
+ recognize(req) do |route, params|
67
66
  # TODO: We only want to unescape params from uri related methods
68
67
  params.each { |k, v| params[k] = Utils.unescape_uri(v) if v.is_a?(String) }
69
68
 
70
- if route.prefix?
71
- env[Prefix::KEY] = matches[:path_info].to_s
72
- end
73
-
74
69
  env[@parameters_key] = params
75
70
  result = route.app.call(env)
76
71
  return result unless result[1][X_CASCADE] == PASS
@@ -86,15 +81,6 @@ module Rack::Mount
86
81
  super
87
82
  end
88
83
 
89
- protected
90
- def recognition_stats
91
- { :keys => @recognition_keys,
92
- :keys_size => @recognition_keys.size,
93
- :graph_size => @recognition_graph.size,
94
- :graph_height => @recognition_graph.height,
95
- :graph_average_height => @recognition_graph.average_height }
96
- end
97
-
98
84
  private
99
85
  def expire!
100
86
  @recognition_keys = @recognition_graph = nil
@@ -21,14 +21,13 @@ module Rack::Mount
21
21
  #
22
22
  # Strexp.compile('src/*files')
23
23
  # # => %r{\Asrc/(?<files>.+)\Z}
24
- def initialize(str, requirements = {}, separators = [], anchor = true)
24
+ def initialize(str, requirements = {}, separators = [])
25
25
  return super(str) if str.is_a?(Regexp)
26
26
 
27
27
  requirements = requirements ? requirements.dup : {}
28
28
  normalize_requirements!(requirements, separators)
29
29
 
30
30
  parser = StrexpParser.new
31
- parser.anchor = anchor
32
31
  parser.requirements = requirements
33
32
 
34
33
  begin
@@ -20,7 +20,7 @@ else
20
20
  REGEXP_NAMED_CAPTURE = '(?:<%s>%s)'.freeze
21
21
  end
22
22
 
23
- attr_accessor :anchor, :requirements
23
+ attr_accessor :requirements
24
24
  ##### State transition tables begin ###
25
25
 
26
26
  racc_action_table = [
@@ -114,7 +114,7 @@ Racc_debug_parser = false
114
114
  # reduce 0 omitted
115
115
 
116
116
  def _reduce_1(val, _values, result)
117
- result = anchor ? "\\A#{val.join}\\Z" : "\\A#{val.join}"
117
+ result = "\\A#{val.join}\\Z"
118
118
  result
119
119
  end
120
120
 
@@ -88,17 +88,6 @@ module Rack::Mount
88
88
  end
89
89
  module_function :build_nested_query
90
90
 
91
- # Determines whether the regexp must match the entire string.
92
- #
93
- # regexp_anchored?(/^foo$/) # => true
94
- # regexp_anchored?(/foo/) # => false
95
- # regexp_anchored?(/^foo/) # => false
96
- # regexp_anchored?(/foo$/) # => false
97
- def regexp_anchored?(regexp)
98
- regexp.source =~ /\A(\\A|\^).*(\\Z|\$)\Z/ ? true : false
99
- end
100
- module_function :regexp_anchored?
101
-
102
91
  def normalize_extended_expression(regexp)
103
92
  return regexp unless regexp.options & Regexp::EXTENDED != 0
104
93
  source = regexp.source
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.6
4
+ version: 0.4.7
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: 2010-02-08 00:00:00 -06:00
12
+ date: 2010-02-09 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency