rack-router 0.1.0 → 0.2.0

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/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.3@rack-router --create
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Rack::Router
2
2
 
3
- A simple router for rack apps
3
+ A simple router for rack apps. Requires Ruby 1.9+.
4
4
 
5
5
  ## Installation
6
6
 
data/config.ru CHANGED
@@ -3,7 +3,7 @@ require 'rack/lobster'
3
3
 
4
4
  router = Rack::Router.new do
5
5
  get "/hello/:name" => proc{|env| [200, { "Content-Type" => "text/html" }, ["<h1>Hello, #{env['rack.route_params'][:name]}</h1>"] ] }
6
- get "/lobster" => Rack::Lobster.new
6
+ get "/lobster" => Rack::Lobster.new, :as => "lobster"
7
7
  end
8
8
 
9
9
  run router
data/lib/rack/route.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Rack
2
2
  class Route
3
3
 
4
- attr_accessor :pattern, :app, :constraints
4
+ attr_accessor :pattern, :app, :constraints, :name
5
5
 
6
6
  PATH_INFO = 'PATH_INFO'.freeze
7
7
  DEFAULT_WILDCARD_NAME = :paths
@@ -10,7 +10,7 @@ module Rack
10
10
  NAMED_SEGMENTS_REPLACEMENT_PATTERN = /\/:([^$\/]+)/.freeze
11
11
  DOT = '.'.freeze
12
12
 
13
- def initialize(pattern, app, constraints=nil)
13
+ def initialize(pattern, app, options={})
14
14
  if pattern.to_s.strip.empty?
15
15
  raise ArgumentError.new("pattern cannot be blank")
16
16
  end
@@ -21,7 +21,8 @@ module Rack
21
21
 
22
22
  @pattern = pattern
23
23
  @app = app
24
- @constraints = constraints
24
+ @constraints = options && options[:constraints]
25
+ @name = options && options[:as]
25
26
  end
26
27
 
27
28
  def regexp
data/lib/rack/router.rb CHANGED
@@ -21,9 +21,14 @@ module Rack
21
21
 
22
22
  def initialize(&block)
23
23
  @routes = {}
24
+ @named_routes = {}
24
25
  routes(&block)
25
26
  end
26
27
 
28
+ def [](route_name)
29
+ @named_routes[route_name]
30
+ end
31
+
27
32
  def routes(&block)
28
33
  instance_eval(&block) if block
29
34
  @routes
@@ -46,9 +51,13 @@ module Rack
46
51
  end
47
52
 
48
53
  def route(method, route_spec)
49
- route = Route.new(route_spec.first.first, route_spec.first.last)
54
+ route = Route.new(route_spec.first.first, route_spec.first.last, route_spec.reject{|k,_| k == route_spec.first.first })
50
55
  @routes[method] ||= []
51
56
  @routes[method] << route
57
+ if route_spec && route_spec[:as]
58
+ # Using ||= so the first route with that name will be returned
59
+ @named_routes[route_spec[:as].to_sym] ||= route_spec.first.first
60
+ end
52
61
  route
53
62
  end
54
63
 
data/rack-router.gemspec CHANGED
@@ -11,5 +11,5 @@ Gem::Specification.new do |gem|
11
11
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
12
12
  gem.name = "rack-router"
13
13
  gem.require_paths = ["lib"]
14
- gem.version = "0.1.0"
14
+ gem.version = "0.2.0"
15
15
  end
data/test/route_test.rb CHANGED
@@ -30,9 +30,11 @@ class RouteTest < Test::Unit::TestCase
30
30
 
31
31
  def test_match_with_constraints
32
32
  r = route("/posts/:year/:month/:day/:slug",
33
- :year => /\A\d{4}\Z/,
34
- :month => /\A\d{1,2}\Z/,
35
- :day => /\A\d{1,2}\Z/)
33
+ :constraints => {
34
+ :year => /\A\d{4}\Z/,
35
+ :month => /\A\d{1,2}\Z/,
36
+ :day => /\A\d{1,2}\Z/},
37
+ :as => "article")
36
38
  assert_equal({
37
39
  :year => "2012",
38
40
  :month => "9",
@@ -41,6 +43,7 @@ class RouteTest < Test::Unit::TestCase
41
43
  }, r.match("/posts/2012/9/20/test"))
42
44
  assert_equal(nil, r.match("/posts/2012/9/20"))
43
45
  assert_equal(nil, r.match("/posts/2012/x/20/test"))
46
+ assert_equal "article", r.name
44
47
  end
45
48
 
46
49
  def test_eql
@@ -51,8 +54,8 @@ class RouteTest < Test::Unit::TestCase
51
54
  end
52
55
 
53
56
  private
54
- def route(path, constraints=nil)
55
- Rack::Route.new(path, lambda{|env| [200, {}, [""]] }, constraints)
57
+ def route(path, options={})
58
+ Rack::Route.new(path, lambda{|env| [200, {}, [""]] }, options)
56
59
  end
57
60
 
58
61
  def match(pattern, path, params)
data/test/router_test.rb CHANGED
@@ -7,8 +7,8 @@ class RouterTest < Test::Unit::TestCase
7
7
  app2 = lambda{|env| [200, {}, ["2"]] }
8
8
 
9
9
  router = Rack::Router.new do
10
- post "/stuff" => app2
11
- put "/it" => app2
10
+ post "/stuff" => app2, :as => "stuff"
11
+ put "/it" => app2, :as => :it
12
12
  delete "/remove" => app2
13
13
  get "/:id" => app1
14
14
  end
@@ -21,6 +21,8 @@ class RouterTest < Test::Unit::TestCase
21
21
  }, router.routes)
22
22
 
23
23
  assert_equal ["42"], router.call("REQUEST_METHOD" => "GET", "PATH_INFO" => "/42").last
24
+ assert_equal "/stuff", router[:stuff]
25
+ assert_equal "/it", router[:it]
24
26
  end
25
27
  end
26
28
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-router
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-12 00:00:00.000000000 Z
12
+ date: 2012-10-18 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A simple router for Rack apps
15
15
  email:
@@ -19,6 +19,7 @@ extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
21
  - .gitignore
22
+ - .rvmrc
22
23
  - Gemfile
23
24
  - LICENSE
24
25
  - README.md
@@ -50,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
50
51
  version: '0'
51
52
  requirements: []
52
53
  rubyforge_project:
53
- rubygems_version: 1.8.24
54
+ rubygems_version: 1.8.17
54
55
  signing_key:
55
56
  specification_version: 3
56
57
  summary: A simple router for rack apps