Class: Routes
Instance Attribute Summary collapse
-
#path_matchers ⇒ Object
readonly
Returns the value of attribute path_matchers.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
-
#add_path_matcher(sections) ⇒ Object
TODO: This is slow, optimize with a DFA or NFA.
- #define(&block) ⇒ Object
- #get(path, options = {}) ⇒ Object
-
#initialize ⇒ Routes
constructor
A new instance of Routes.
-
#params_for_path(path) ⇒ Object
Takes in a path and returns the matching params.
-
#url_for_params(params) ⇒ Object
Takes in params and generates a path and the remaining params that should be shown in the url.
Constructor Details
Instance Attribute Details
#path_matchers ⇒ Object (readonly)
Returns the value of attribute path_matchers.
4 5 6 |
# File 'lib/volt/router/routes.rb', line 4 def path_matchers @path_matchers end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
4 5 6 |
# File 'lib/volt/router/routes.rb', line 4 def routes @routes end |
Instance Method Details
#add_path_matcher(sections) ⇒ Object
TODO: This is slow, optimize with a DFA or NFA
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/volt/router/routes.rb', line 51 def add_path_matcher(sections) match_path = '' sections.each do |section| if section[0] == '{' && section[-1] == '}' match_path << "[^\/]+" else match_path << section end end @path_matchers << (/^#{match_path}$/) end |
#define(&block) ⇒ Object
14 15 16 17 18 |
# File 'lib/volt/router/routes.rb', line 14 def define(&block) instance_eval(&block) return self end |
#get(path, options = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/volt/router/routes.rb', line 20 def get(path, ={}) if path.index('{') && path.index('}') sections = path.split(/(\{[^\}]+\})/) sections = sections.reject {|v| v == '' } sections.each do |section| if section[0] == '{' && section[-1] == '}' [section[1..-2]] = nil end end add_path_matcher(sections) if Volt.server? path = Proc.new do |params| # Create a path using the params in the path sections.map do |section| if section[0] == '{' && section[-1] == '}' params[section[1..-2]] else section end end.join('') end else add_path_matcher([path]) if Volt.server? end @routes << [path, ] end |
#params_for_path(path) ⇒ Object
Takes in a path and returns the matching params.
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/volt/router/routes.rb', line 77 def params_for_path(path) routes.each do |route| if route[0] == path # Found the matching route return route[1] end end return {} end |
#url_for_params(params) ⇒ Object
Takes in params and generates a path and the remaining params that should be shown in the url.
66 67 68 69 70 71 72 73 74 |
# File 'lib/volt/router/routes.rb', line 66 def url_for_params(params) routes.each do |route| if (params, route[1]) return path_and_params(params, route[0], route[1]) end end return '/', params end |