rack-mount 0.6.1 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. data/lib/rack/mount.rb +3 -13
  2. data/lib/rack/mount/analysis/frequency.rb +0 -2
  3. data/lib/rack/mount/analysis/splitting.rb +2 -2
  4. data/lib/rack/mount/code_generation.rb +113 -0
  5. data/lib/rack/mount/generatable_regexp.rb +1 -1
  6. data/lib/rack/mount/route.rb +69 -5
  7. data/lib/rack/mount/route_set.rb +289 -46
  8. data/lib/rack/mount/strexp.rb +53 -50
  9. data/lib/rack/mount/utils.rb +7 -7
  10. data/lib/rack/mount/vendor/reginald/reginald.rb +2 -1
  11. data/lib/rack/mount/vendor/reginald/reginald/alternation.rb +11 -1
  12. data/lib/rack/mount/vendor/reginald/reginald/atom.rb +17 -5
  13. data/lib/rack/mount/vendor/reginald/reginald/character.rb +11 -2
  14. data/lib/rack/mount/vendor/reginald/reginald/character_class.rb +8 -20
  15. data/lib/rack/mount/vendor/reginald/reginald/collection.rb +10 -6
  16. data/lib/rack/mount/vendor/reginald/reginald/expression.rb +30 -20
  17. data/lib/rack/mount/vendor/reginald/reginald/group.rb +22 -8
  18. data/lib/rack/mount/vendor/reginald/reginald/options.rb +47 -0
  19. data/lib/rack/mount/vendor/reginald/reginald/parser.rb +137 -273
  20. data/lib/rack/mount/vendor/reginald/reginald/tokenizer.rb +1 -1
  21. data/lib/rack/mount/vendor/reginald/reginald/version.rb +3 -0
  22. data/lib/rack/mount/version.rb +1 -1
  23. metadata +6 -10
  24. data/lib/rack/mount/exceptions.rb +0 -3
  25. data/lib/rack/mount/generation/route.rb +0 -59
  26. data/lib/rack/mount/generation/route_set.rb +0 -200
  27. data/lib/rack/mount/mixover.rb +0 -60
  28. data/lib/rack/mount/recognition/code_generation.rb +0 -124
  29. data/lib/rack/mount/recognition/route.rb +0 -62
  30. data/lib/rack/mount/recognition/route_set.rb +0 -121
@@ -1,62 +0,0 @@
1
- require 'rack/mount/utils'
2
-
3
- module Rack::Mount
4
- module Recognition
5
- module Route #:nodoc:
6
- attr_reader :named_captures
7
-
8
- def initialize(*args)
9
- super
10
-
11
- @named_captures = {}
12
- @conditions.map { |method, condition|
13
- next unless condition.respond_to?(:named_captures)
14
- @named_captures[method] = condition.named_captures.inject({}) { |named_captures, (k, v)|
15
- named_captures[k.to_sym] = v.last - 1
16
- named_captures
17
- }.freeze
18
- }
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
- end
33
-
34
- def recognize(obj)
35
- matches = {}
36
- params = @defaults.dup
37
-
38
- if @conditions.all? { |method, condition|
39
- value = obj.send(method)
40
- if condition.is_a?(Regexp) && (m = value.match(condition))
41
- matches[method] = m
42
- captures = m.captures
43
- @named_captures[method].each { |k, i|
44
- if v = captures[i]
45
- params[k] = v
46
- end
47
- }
48
- true
49
- elsif value == condition
50
- true
51
- else
52
- false
53
- end
54
- }
55
- return matches, params
56
- else
57
- nil
58
- end
59
- end
60
- end
61
- end
62
- end
@@ -1,121 +0,0 @@
1
- require 'rack/mount/utils'
2
-
3
- module Rack::Mount
4
- module Recognition
5
- module RouteSet
6
- attr_reader :parameters_key
7
-
8
- # Adds recognition related concerns to RouteSet.new.
9
- def initialize(options = {})
10
- @parameters_key = options.delete(:parameters_key) || 'rack.routing_args'
11
- @parameters_key.freeze
12
- @recognition_key_analyzer = Analysis::Frequency.new_with_module(Analysis::Splitting)
13
-
14
- super
15
- end
16
-
17
- # Adds recognition aspects to RouteSet#add_route.
18
- def add_route(*args)
19
- route = super
20
- @recognition_key_analyzer << route.conditions
21
- route
22
- end
23
-
24
- def recognize(obj)
25
- raise 'route set not finalized' unless @recognition_graph
26
-
27
- cache = {}
28
- keys = @recognition_keys.map { |key|
29
- if key.respond_to?(:call_source)
30
- key.call(cache, obj)
31
- else
32
- obj.send(key)
33
- end
34
- }
35
- @recognition_graph[*keys].each do |route|
36
- matches, params = route.recognize(obj)
37
- if matches && params
38
- if block_given?
39
- yield route, matches, params
40
- else
41
- return route, matches, params
42
- end
43
- end
44
- end
45
-
46
- nil
47
- end
48
-
49
- X_CASCADE = 'X-Cascade'.freeze
50
- PASS = 'pass'.freeze
51
- PATH_INFO = 'PATH_INFO'.freeze
52
-
53
- # Rack compatible recognition and dispatching method. Routes are
54
- # tried until one returns a non-catch status code. If no routes
55
- # match, the catch status code is returned.
56
- #
57
- # This method can only be invoked after the RouteSet has been
58
- # finalized.
59
- def call(env)
60
- raise 'route set not finalized' unless @recognition_graph
61
-
62
- env[PATH_INFO] = Utils.normalize_path(env[PATH_INFO])
63
-
64
- request = nil
65
- req = @request_class.new(env)
66
- recognize(req) do |route, matches, params|
67
- # TODO: We only want to unescape params from uri related methods
68
- params.each { |k, v| params[k] = Utils.unescape_uri(v) if v.is_a?(String) }
69
-
70
- if route.prefix?
71
- env[Prefix::KEY] = matches[:path_info].to_s
72
- end
73
-
74
- env[@parameters_key] = params
75
- result = route.app.call(env)
76
- return result unless result[1][X_CASCADE] == PASS
77
- end
78
-
79
- request || [404, {'Content-Type' => 'text/html', 'X-Cascade' => 'pass'}, ['Not Found']]
80
- end
81
-
82
- def rehash #:nodoc:
83
- @recognition_keys = build_recognition_keys
84
- @recognition_graph = build_recognition_graph
85
-
86
- super
87
- end
88
-
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
- private
99
- def expire!
100
- @recognition_keys = @recognition_graph = nil
101
- @recognition_key_analyzer.expire!
102
- super
103
- end
104
-
105
- def flush!
106
- @recognition_key_analyzer = nil
107
- super
108
- end
109
-
110
- def build_recognition_graph
111
- build_nested_route_set(@recognition_keys) { |k, i|
112
- @recognition_key_analyzer.possible_keys[i][k]
113
- }
114
- end
115
-
116
- def build_recognition_keys
117
- @recognition_key_analyzer.report
118
- end
119
- end
120
- end
121
- end