Class: Routes

Inherits:
Object show all
Defined in:
lib/volt/router/routes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRoutes

Returns a new instance of Routes.



4
5
6
# File 'lib/volt/router/routes.rb', line 4

def initialize
  @routes = []
end

Instance Attribute Details

#routesObject (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, options)
  if path.index(':')
    sections = path.split(/([:][^:\/]+)/)
    
    sections.each do |section|
      if section[0] == ':'
        options[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, options]
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_match_options?(params, route[1])
      return path_and_params(params, route[0], route[1])
    end
  end
  
  return '/', params
end