Class: Volt::Routes
Overview
The Routes class takes a set of routes and sets up methods to go from a url to params, and params to url. routes do
client "/about", _view: 'about'
client "/blog/{_id}/edit", _view: 'blog/edit', _action: 'edit'
client "/blog/{_id}", _view: 'blog/show', _action: 'show'
client "/blog", _view: 'blog'
client "/blog/new", _view: 'blog/new', _action: 'new'
client "/cool/{_name}", _view: 'cool'
end
Using the routes above, we would generate the following:
}
– nil represents a terminal – * represents any match – a number for a parameter means use the value in that number section
}
}
Match for params ]
Instance Method Summary collapse
-
#client(path, params = {}) ⇒ Object
Add a route.
- #define(&block) ⇒ Object
- #delete(path, params) ⇒ Object
-
#get(path, params) ⇒ Object
Add server side routes.
-
#initialize ⇒ Routes
constructor
A new instance of Routes.
-
#params_to_url(test_params) ⇒ Object
Takes in params and generates a path and the remaining params that should be shown in the url.
- #patch(path, params) ⇒ Object
- #post(path, params) ⇒ Object
- #put(path, params) ⇒ Object
-
#url_to_params(*args) ⇒ Object
Takes in a path and returns the matching params.
Constructor Details
#initialize ⇒ Routes
Returns a new instance of Routes.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/volt/router/routes.rb', line 42 def initialize # Paths where there are no bindings (an optimization) @direct_routes = {} # Paths with bindings @indirect_routes = {} # Matcher for going from params to url @param_matches = {} [:client, :get, :post, :put, :patch, :delete].each do |method| @direct_routes[method] = {} @indirect_routes[method] = {} @param_matches[method] = [] end end |
Instance Method Details
#client(path, params = {}) ⇒ Object
Add a route
66 67 68 |
# File 'lib/volt/router/routes.rb', line 66 def client(path, params = {}) create_route(:client, path, params) end |
#define(&block) ⇒ Object
59 60 61 62 63 |
# File 'lib/volt/router/routes.rb', line 59 def define(&block) instance_eval(&block) self end |
#delete(path, params) ⇒ Object
87 88 89 |
# File 'lib/volt/router/routes.rb', line 87 def delete(path, params) create_route(:delete, path, params) end |
#get(path, params) ⇒ Object
Add server side routes
71 72 73 |
# File 'lib/volt/router/routes.rb', line 71 def get(path, params) create_route(:get, path, params) end |
#params_to_url(test_params) ⇒ Object
Takes in params and generates a path and the remaining params that should be shown in the url. The extra “unused” params will be tacked onto the end of the url ?param1=value1, etc…
returns the url and new params, or nil, nil if no match is found.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/volt/router/routes.rb', line 96 def params_to_url(test_params) # Extract the desired method from the params method = test_params.delete(:method) || :client method = method.to_sym # Add in underscores test_params = test_params.each_with_object({}) do |(k, v), obj| obj[k.to_sym] = v end @param_matches[method].each do |param_matcher| # TODO: Maybe a deep dup? result, new_params = check_params_match(test_params.dup, param_matcher[0]) return param_matcher[1].call(new_params) if result end [nil, nil] end |
#patch(path, params) ⇒ Object
79 80 81 |
# File 'lib/volt/router/routes.rb', line 79 def patch(path, params) create_route(:patch, path, params) end |
#post(path, params) ⇒ Object
75 76 77 |
# File 'lib/volt/router/routes.rb', line 75 def post(path, params) create_route(:post, path, params) end |
#put(path, params) ⇒ Object
83 84 85 |
# File 'lib/volt/router/routes.rb', line 83 def put(path, params) create_route(:put, path, params) end |
#url_to_params(*args) ⇒ Object
Takes in a path and returns the matching params. returns params as a hash
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/volt/router/routes.rb', line 118 def url_to_params(*args) if args.size < 2 path = args[0] method = :client else path = args[1] method = args[0].to_sym end # First try a direct match result = @direct_routes[method][path] return result if result # Next, split the url and walk the sections parts = url_parts(path) match_path(parts, parts, @indirect_routes[method]) end |