derailleur 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/TODO CHANGED
@@ -1 +1,3 @@
1
+ * spec for get_route_silent
1
2
  * separate classes to hold handoff and grafting logic in trienodes
3
+ * filesystem trees for non-HTTP things
@@ -1,6 +1,6 @@
1
1
 
2
2
  module Derailleur
3
- VERSION = "0.0.5"
3
+ VERSION = "0.0.6"
4
4
  AUTHORS = ['crapooze']
5
5
  WEBSITE = "http://github.com/crapooze/derailleur"
6
6
  LICENCE = "MIT"
@@ -79,18 +79,31 @@ module Derailleur
79
79
  # Will (optionally) consecutively yield all the [node, chunk_name]
80
80
  # this is useful when you want to interpret the members of the path
81
81
  # as a parameter.
82
- def get_route(path)
82
+ def get_route_silent(path)
83
83
  current_node = routes
84
84
  chunk_path(path).each do |chunk|
85
85
  unless current_node.absorbent?
86
86
  current_node = current_node.child_for_name(chunk)
87
- raise NoSuchRoute, "no such path #{path}" unless current_node
87
+ return nil unless current_node
88
88
  end
89
89
  yield current_node, chunk if block_given?
90
90
  end
91
91
  current_node
92
92
  end
93
-
93
+
94
+ # Same as get_route_silent but raise a
95
+ # NoSuchRoute error if there is no matching route.
96
+ def get_route(path)
97
+ node = if block_given?
98
+ get_route_silent(path) do |node,chunk|
99
+ yield node, chunk
100
+ end
101
+ else
102
+ get_route_silent(path)
103
+ end
104
+ raise NoSuchRoute, "no such path #{path}" unless node
105
+ node
106
+ end
94
107
 
95
108
  # Registers an handler for a given path.
96
109
  # The path will be interpreted as an absolute path prefixed by '/' .
@@ -155,6 +168,25 @@ module Derailleur
155
168
  app_node.graft!(split_node)
156
169
  end
157
170
 
171
+ # Similar to get route, but also interprets nodes names as keys for a hash.
172
+ # The values in the parameters hash are the string corresponding to the
173
+ # nodes in the path.
174
+ # A specific key is :splat, which correspond to the remaining chunks in the
175
+ # paths.
176
+ # Does NOT take care of key collisions. This should be taken care of at the
177
+ # application level.
178
+ def get_route_with_params(path)
179
+ params = {:splat => []}
180
+ route = get_route(path) do |node, val|
181
+ if node.wildcard?
182
+ params[node.name] = val
183
+ elsif node.absorbent?
184
+ params[:splat] << val
185
+ end
186
+ end
187
+ [route, params]
188
+ end
189
+
158
190
  # Method implemented to comply to the Rack specification. see
159
191
  # http://rack.rubyforge.org/doc/files/SPEC.html to understand what to
160
192
  # return.
@@ -176,14 +208,7 @@ module Derailleur
176
208
  begin
177
209
  path = env['PATH_INFO'].sub(/\.\w+$/,'') #ignores the extension if any
178
210
  ctx = {}
179
- params = {:splat => []}
180
- route = get_route(path) do |node, val|
181
- if node.wildcard?
182
- params[node.name] = val
183
- elsif node.absorbent?
184
- params[:splat] << val
185
- end
186
- end
211
+ route, params = get_route_with_params(path)
187
212
  ctx['derailleur.node'] = route
188
213
  ctx['derailleur.params'] = params
189
214
  ctx['derailleur'] = self
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: derailleur
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 0
9
- - 5
10
- version: 0.0.5
8
+ - 6
9
+ version: 0.0.6
11
10
  platform: ruby
12
11
  authors:
13
12
  - crapooze
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2011-02-12 00:00:00 +01:00
17
+ date: 2011-03-19 00:00:00 -04:00
19
18
  default_executable:
20
19
  dependencies: []
21
20
 
@@ -56,7 +55,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
56
55
  requirements:
57
56
  - - ">="
58
57
  - !ruby/object:Gem::Version
59
- hash: 3
60
58
  segments:
61
59
  - 0
62
60
  version: "0"
@@ -65,7 +63,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
63
  requirements:
66
64
  - - ">="
67
65
  - !ruby/object:Gem::Version
68
- hash: 3
69
66
  segments:
70
67
  - 0
71
68
  version: "0"