Class: Routes
Instance Attribute Summary collapse
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
- #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
#initialize ⇒ Routes
Returns a new instance of Routes.
4 5 6 |
# File 'lib/volt/router/routes.rb', line 4 def initialize @routes = [] end |
Instance Attribute Details
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
2 3 4 |
# File 'lib/volt/router/routes.rb', line 2 def routes @routes end |
Instance Method Details
#define(&block) ⇒ Object
8 9 10 11 12 |
# File 'lib/volt/router/routes.rb', line 8 def define(&block) instance_eval(&block) return self end |
#get(path, options) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/volt/router/routes.rb', line 14 def get(path, ) if path.index(':') sections = path.split(/([:][^:\/]+)/) sections.each do |section| if section[0] == ':' [section[1..-1]] = nil end end path = Proc.new do |params| # Create a path using the params in the path sections.map do |section| if section[0] == ':' params[section[1..-1]] else section end end.join('') end end @routes << [path, ] end |
#params_for_path(path) ⇒ Object
Takes in a path and returns the matching params.
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/volt/router/routes.rb', line 51 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.
40 41 42 43 44 45 46 47 48 |
# File 'lib/volt/router/routes.rb', line 40 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 |