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.



6
7
8
9
10
11
12
# File 'lib/volt/router/routes.rb', line 6

def initialize
  @routes = []
  
  if Volt.server?
    @path_matchers = []
  end
end

Instance Attribute Details

#path_matchersObject (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

#routesObject (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



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/volt/router/routes.rb', line 63

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
60
# File 'lib/volt/router/routes.rb', line 35

def build_path_matcher(path, options)
  sections = path.split(/(\{[^\}]+\})/)
  sections = sections.reject {|v| v == '' }
  
  sections.each do |section|
    if section[0] == '{' && section[-1] == '}'
      options[section[1..-2]] = nil
    end
  end
  
  add_path_matcher(sections) if Volt.server?
  
  # Create a path that takes in the params and returns the main
  # part of the url with the params filled in.
  path = Proc.new do |params|
    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, options={})
  if path.index('{') && path.index('}')
    # The path contains bindings.
    path = build_path_matcher(path, options)
  else
    add_path_matcher([path]) if Volt.server?
  end
  
  @routes << [path, options]
end

#params_for_path(path) ⇒ Object

Takes in a path and returns the matching params. TODO: Slow, need dfa



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/volt/router/routes.rb', line 90

def params_for_path(path)
  routes.each do |route|
    # TODO: Finish nested routes
    if false && route[0].class == Proc
      # puts route[0].call(params).inspect
      
      return false
    elsif 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.



78
79
80
81
82
83
84
85
86
# File 'lib/volt/router/routes.rb', line 78

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