Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 220 lines (176 sloc) 4.835 kB
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
1 # encoding: utf-8
2dea0dd @thedarkone Replace global Hash with TS::Cache.
thedarkone authored
2
3 require 'thread_safe'
4
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
5 module ActionDispatch
a36ae63 :nodoc: Journey because is not part of the public API [ci skip]
Francesco Rodriguez authored
6 module Journey # :nodoc:
7 module Visitors # :nodoc:
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
8 class Visitor # :nodoc:
2dea0dd @thedarkone Replace global Hash with TS::Cache.
thedarkone authored
9 DISPATCH_CACHE = ThreadSafe::Cache.new { |h,k|
5e356de @charliesome store a symbol directly inside DISPATCH_CACHE for extra roflscale
charliesome authored
10 h[k] = :"visit_#{k}"
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
11 }
12
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
13 def accept(node)
14 visit(node)
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
15 end
16
17 private
18
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
19 def visit node
20 send(DISPATCH_CACHE[node.type], node)
21 end
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
22
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
23 def binary(node)
24 visit(node.left)
25 visit(node.right)
26 end
27 def visit_CAT(n); binary(n); end
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
28
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
29 def nary(node)
30 node.children.each { |c| visit(c) }
31 end
32 def visit_OR(n); nary(n); end
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
33
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
34 def unary(node)
35 visit(node.left)
36 end
37 def visit_GROUP(n); unary(n); end
38 def visit_STAR(n); unary(n); end
39
40 def terminal(node); end
41 %w{ LITERAL SYMBOL SLASH DOT }.each do |t|
42 class_eval %{ def visit_#{t}(n); terminal(n); end }, __FILE__, __LINE__
43 end
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
44 end
45
46 # Loop through the requirements AST
47 class Each < Visitor # :nodoc:
48 attr_reader :block
49
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
50 def initialize(block)
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
51 @block = block
52 end
53
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
54 def visit(node)
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
55 super
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
56 block.call(node)
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
57 end
58 end
59
a36ae63 :nodoc: Journey because is not part of the public API [ci skip]
Francesco Rodriguez authored
60 class String < Visitor # :nodoc:
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
61 private
62
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
63 def binary(node)
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
64 [visit(node.left), visit(node.right)].join
65 end
66
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
67 def nary(node)
68 node.children.map { |c| visit(c) }.join '|'
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
69 end
70
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
71 def terminal(node)
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
72 node.left
73 end
74
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
75 def visit_GROUP(node)
76 "(#{visit(node.left)})"
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
77 end
78 end
79
d017e92 @pixeltrix Use a custom route vistor for optimized route generation
pixeltrix authored
80 class OptimizedPath < Visitor # :nodoc:
81 def accept(node)
82 Array(visit(node))
83 end
84
86cf7a2 @pixeltrix Use custom visitor class for optimized url helpers
pixeltrix authored
85 private
86
d017e92 @pixeltrix Use a custom route vistor for optimized route generation
pixeltrix authored
87 def visit_CAT(node)
88 [visit(node.left), visit(node.right)].flatten
89 end
90
91 def visit_SYMBOL(node)
92 node.left[1..-1].to_sym
93 end
94
95 def visit_STAR(node)
96 visit(node.left)
97 end
98
99 def visit_GROUP(node)
100 []
101 end
102
103 %w{ LITERAL SLASH DOT }.each do |t|
104 class_eval %{ def visit_#{t}(n); n.left; end }, __FILE__, __LINE__
105 end
86cf7a2 @pixeltrix Use custom visitor class for optimized url helpers
pixeltrix authored
106 end
107
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
108 # Used for formatting urls (url_for)
a36ae63 :nodoc: Journey because is not part of the public API [ci skip]
Francesco Rodriguez authored
109 class Formatter < Visitor # :nodoc:
5c224de @bogdan Rewrite journey routes formatter for performance
bogdan authored
110 attr_reader :options
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
111
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
112 def initialize(options)
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
113 @options = options
114 end
115
116 private
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
117
5c224de @bogdan Rewrite journey routes formatter for performance
bogdan authored
118 def visit(node, optional = false)
119 case node.type
120 when :LITERAL, :SLASH, :DOT
121 node.left
122 when :STAR
123 visit(node.left)
124 when :GROUP
125 visit(node.left, true)
126 when :CAT
127 visit_CAT(node, optional)
128 when :SYMBOL
129 visit_SYMBOL(node)
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
130 end
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
131 end
132
5c224de @bogdan Rewrite journey routes formatter for performance
bogdan authored
133 def visit_CAT(node, optional)
134 left = visit(node.left, optional)
135 right = visit(node.right, optional)
9357e5e @rafaelfranca Use join to concat the both side of the AST
rafaelfranca authored
136
5c224de @bogdan Rewrite journey routes formatter for performance
bogdan authored
137 if optional && !(right && left)
138 ""
139 else
9357e5e @rafaelfranca Use join to concat the both side of the AST
rafaelfranca authored
140 [left, right].join
5c224de @bogdan Rewrite journey routes formatter for performance
bogdan authored
141 end
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
142 end
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
143
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
144 def visit_SYMBOL(node)
5c224de @bogdan Rewrite journey routes formatter for performance
bogdan authored
145 if value = options[node.to_sym]
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
146 Router::Utils.escape_path(value)
147 end
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
148 end
149 end
150
a36ae63 :nodoc: Journey because is not part of the public API [ci skip]
Francesco Rodriguez authored
151 class Dot < Visitor # :nodoc:
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
152 def initialize
153 @nodes = []
154 @edges = []
155 end
156
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
157 def accept(node)
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
158 super
159 <<-eodot
160 digraph parse_tree {
161 size="8,5"
162 node [shape = none];
163 edge [dir = none];
164 #{@nodes.join "\n"}
165 #{@edges.join("\n")}
166 }
167 eodot
168 end
169
170 private
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
171
172 def binary(node)
173 node.children.each do |c|
174 @edges << "#{node.object_id} -> #{c.object_id};"
175 end
176 super
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
177 end
178
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
179 def nary(node)
180 node.children.each do |c|
181 @edges << "#{node.object_id} -> #{c.object_id};"
182 end
183 super
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
184 end
185
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
186 def unary(node)
187 @edges << "#{node.object_id} -> #{node.left.object_id};"
188 super
189 end
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
190
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
191 def visit_GROUP(node)
192 @nodes << "#{node.object_id} [label=\"()\"];"
193 super
194 end
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
195
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
196 def visit_CAT(node)
197 @nodes << "#{node.object_id} [label=\"\"];"
198 super
199 end
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
200
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
201 def visit_STAR(node)
202 @nodes << "#{node.object_id} [label=\"*\"];"
203 super
204 end
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
205
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
206 def visit_OR(node)
207 @nodes << "#{node.object_id} [label=\"|\"];"
208 super
209 end
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
210
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
211 def terminal(node)
212 value = node.left
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
213
eb493f5 update AD::Journey to follow Rails coding conventions
Francesco Rodriguez authored
214 @nodes << "#{node.object_id} [label=\"#{value}\"];"
215 end
56fee39 @pixeltrix Integrate Journey into Action Dispatch
pixeltrix authored
216 end
217 end
218 end
219 end
Something went wrong with that request. Please try again.