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.
-
#build_path_matcher(path, options) ⇒ Object
Takes the path and splits it up into sections around any bindings in the path.
- #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
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/volt/router/routes.rb', line 62 def add_path_matcher(sections) match_path = '' sections.each do |section| if section[0] == '{' && section[-1] == '}' match_path = match_path + "[^\/]+" else match_path = match_path + section end end @path_matchers << (/^#{match_path}$/) end |
#build_path_matcher(path, options) ⇒ Object
Takes the path and splits it up into sections around any bindings in the path. Those are then used to create a proc that will return the path with the current params in it. If it matches it will be used.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/volt/router/routes.rb', line 35 def build_path_matcher(path, ) 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 return 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 |
# File 'lib/volt/router/routes.rb', line 20 def get(path, ={}) if path.index('{') && path.index('}') # The path contains bindings. path = build_path_matcher(path, ) 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.
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/volt/router/routes.rb', line 88 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.
77 78 79 80 81 82 83 84 85 |
# File 'lib/volt/router/routes.rb', line 77 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 |