joshbuddy-usher 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -8,7 +8,7 @@ This is a tree-based router (based on Ilya Grigorik suggestion). Turns out looki
8
8
 
9
9
  Here are some examples of routes recognized by usher (so far)
10
10
 
11
- _Route_ _Matches_
11
+ *Route* *Matches*
12
12
  /path/to/something /path/to/something
13
13
  /path/:variable/more /path/foo/more, /path/bar/more ...
14
14
  /show/*tags /show/hot /show/hot/coffee /show/some/more/hot/coffee ...
@@ -48,10 +48,10 @@ Here are some examples of routes recognized by usher (so far)
48
48
  == DONE
49
49
 
50
50
  * add support for () optional parts
51
+ * Add support for arbitrary HTTP header checks
51
52
 
52
53
  == TODO
53
54
 
54
- * Add support for arbitrary HTTP header checks
55
55
  * Make it integrate with merb
56
56
  * Make it integrate with rails3
57
57
  * Create decent DSL for use with rack
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
+ :patch: 2
2
3
  :major: 0
3
4
  :minor: 1
4
- :patch: 1
@@ -1,4 +1,5 @@
1
1
  class Usher
2
2
  class UnrecognizedException < RuntimeError; end
3
3
  class ValidationException < RuntimeError; end
4
+ class MissingParameterException < RuntimeError; end
4
5
  end
data/lib/usher.rb CHANGED
@@ -90,7 +90,7 @@ class Usher
90
90
  # * :transformers - Transforms a variable before it gets to the conditions and requirements. Takes either a +proc+ or a +symbol+. If its a +symbol+, calls the method on the incoming parameter. If its a +proc+, its called with the variable.
91
91
  # * :requirements - After transformation, tests the condition using ===. If it returns false, it raises an +Usher::ValidationException+
92
92
  # * :conditions - Accepts any of the following +:protocol+, +:domain+, +:port+, +:query_string+, +:remote_ip+, +:user_agent+, +:referer+ and +:method+. This can be either a +string+ or a +regexp+.
93
- #
93
+ # * any other key is interpreted as a requirement for the variable of its name.
94
94
  def add_route(path, options = {})
95
95
  transformers = options.delete(:transformers) || {}
96
96
  conditions = options.delete(:conditions) || {}
@@ -120,10 +120,22 @@ class Usher
120
120
  @tree.find(request)
121
121
  end
122
122
 
123
+ # Recognizes a set of +parameters+ and gets the closest matching Usher::Route::Path or +nil+ if no route exists.
124
+ #
125
+ # set = Usher.new
126
+ # route = set.add_route('/:controller/:action')
127
+ # set.route_for_options({:controller => 'test', :action => 'action'}) == path.route => true
123
128
  def route_for_options(options)
124
129
  Grapher.instance.find_matching_path(options)
125
130
  end
126
131
 
132
+ # Generates a completed URL based on a +route+ or set of +params+
133
+ #
134
+ # set = Usher.new
135
+ # route = set.add_named_route(:test_route, '/:controller/:action')
136
+ # set.generate_url(nil, {:controller => 'c', :action => 'a'}) == '/c/a' => true
137
+ # set.generate_url(:test_route, {:controller => 'c', :action => 'a'}) == '/c/a' => true
138
+ # set.generate_url(route.primary_path, {:controller => 'c', :action => 'a'}) == '/c/a' => true
127
139
  def generate_url(route, params)
128
140
  path = case route
129
141
  when Symbol
@@ -138,9 +150,9 @@ class Usher
138
150
  param_list = case params
139
151
  when Hash
140
152
  params_hash = params
141
- path.dynamic_parts.collect{|k| params_hash.delete(k.name)}
153
+ path.dynamic_parts.collect{|k| params_hash.delete(k.name) {|el| raise MissingParameterException.new(k.name)} }
142
154
  when Array
143
- params
155
+ path.dynamic_parts.size == params.size ? params : raise(MissingParameterException.new("got #{params.size} arguments, expected #{path.dynamic_parts.size}"))
144
156
  else
145
157
  Array(params)
146
158
  end
@@ -58,4 +58,31 @@ describe "Usher URL generation" do
58
58
  route_set.generate_url(:sample, {:action => 'action', :format => 'html'}).should == '/sample/action.html'
59
59
  end
60
60
 
61
+ it "should generate from parameters" do
62
+ caf = route_set.add_route('/:controller/:action.:format')
63
+ ca = route_set.add_route('/:controller/:action')
64
+ route_set.generate_url(nil, {:controller => 'controller', :action => 'action'}).should == '/controller/action'
65
+ route_set.generate_url(nil, {:controller => 'controller', :action => 'action', :format => 'html'}).should == '/controller/action.html'
66
+ end
67
+
68
+ it "should use the first route when generating a URL from two ambiguous routes" do
69
+ route_set.add_route('/:controller/:action')
70
+ route_set.add_route('/:action/:controller')
71
+ route_set.generate_url(nil, {:controller => 'controller', :action => 'action'}).should == '/controller/action'
72
+ end
73
+
74
+ it "should accept an array of parameters" do
75
+ caf = route_set.add_named_route(:name, '/:controller/:action.:format')
76
+ route_set.generate_url(:name, ['controller', 'action', 'html']).should == '/controller/action.html'
77
+ end
78
+
79
+ it "should require all the parameters (hash) to generate a route" do
80
+ proc {route_set.generate_url(route_set.add_route('/:controller/:action').primary_path, {:controller => 'controller'})}.should raise_error Usher::MissingParameterException
81
+ end
82
+
83
+ it "should require all the parameters (array) to generate a route" do
84
+ route_set.add_named_route(:name, '/:controller/:action.:format')
85
+ proc {route_set.generate_url(:name, ['controller', 'action'])}.should raise_error Usher::MissingParameterException
86
+ end
87
+
61
88
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: joshbuddy-usher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Hull
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-27 00:00:00 -07:00
12
+ date: 2009-03-28 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency