Class: Volt::Routes

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

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

Constructor Details

#initializeRoutes

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



88
89
90
# File 'lib/volt/router/routes.rb', line 88

def delete(path, params)
  create_route(:delete, path, params)
end

#get(path, params) ⇒ Object

Add server side routes



72
73
74
# File 'lib/volt/router/routes.rb', line 72

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.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/volt/router/routes.rb', line 126

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



80
81
82
# File 'lib/volt/router/routes.rb', line 80

def patch(path, params)
  create_route(:patch, path, params)
end

#post(path, params) ⇒ Object



76
77
78
# File 'lib/volt/router/routes.rb', line 76

def post(path, params)
  create_route(:post, path, params)
end

#put(path, params) ⇒ Object



84
85
86
# File 'lib/volt/router/routes.rb', line 84

def put(path, params)
  create_route(:put, path, params)
end

#rest(path, params) ⇒ Object

Create rest endpoints



93
94
95
96
97
98
99
# File 'lib/volt/router/routes.rb', line 93

def rest(path, params)
  endpoints = (params.delete(:only) || [:index, :show, :create, :update, :destroy]).to_a
  endpoints = endpoints - params.delete(:except).to_a
  endpoints.each do |endpoint|
    self.send(('restful_' + endpoint.to_s).to_sym, path, params)
  end
end

#restful_create(base_path, params) ⇒ Object



105
106
107
# File 'lib/volt/router/routes.rb', line 105

def restful_create(base_path, params)
  post(base_path, params.merge(action: 'create'))
end

#restful_destroy(base_path, params) ⇒ Object



117
118
119
# File 'lib/volt/router/routes.rb', line 117

def restful_destroy(base_path, params)
  delete(path_with_id(base_path), params.merge(action: 'destroy'))
end

#restful_index(base_path, params) ⇒ Object



101
102
103
# File 'lib/volt/router/routes.rb', line 101

def restful_index(base_path, params)
  get(base_path, params.merge(action: 'index'))
end

#restful_show(base_path, params) ⇒ Object



109
110
111
# File 'lib/volt/router/routes.rb', line 109

def restful_show(base_path, params)
  get(path_with_id(base_path), params.merge(action: 'show'))
end

#restful_update(base_path, params) ⇒ Object



113
114
115
# File 'lib/volt/router/routes.rb', line 113

def restful_update(base_path, params)
  put(path_with_id(base_path), params.merge(action: 'update'))
end

#url_to_params(*args) ⇒ Object

Takes in a path and returns the matching params. returns params as a hash



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/volt/router/routes.rb', line 148

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