sinatra-named_routes 0.0.1
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/.gitignore +17 -0
- data/Gemfile +3 -0
- data/LICENSE +22 -0
- data/README.md +70 -0
- data/Rakefile +8 -0
- data/lib/sinatra/named_routes/router.rb +56 -0
- data/lib/sinatra/named_routes/version.rb +5 -0
- data/lib/sinatra/named_routes.rb +51 -0
- data/sinatra-named_routes.gemspec +23 -0
- data/spec/sinatra/named_routes/router_spec.rb +33 -0
- data/spec/sinatra/named_routes_spec.rb +136 -0
- metadata +119 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Federico Romero
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# Sinatra::NamedRoutes
|
2
|
+
|
3
|
+
Allows you to name routes on definition and build them by name using the `url_for` helper.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'sinatra-named_routes'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install sinatra-named_routes
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
1- Register the extension
|
22
|
+
|
23
|
+
register Sinatra::NamedRoutes
|
24
|
+
|
25
|
+
2- Name your routes when defining them
|
26
|
+
|
27
|
+
get named(:name, '/path') do
|
28
|
+
...
|
29
|
+
end
|
30
|
+
|
31
|
+
get named(:short) do
|
32
|
+
...
|
33
|
+
end
|
34
|
+
|
35
|
+
3- Access the routes in your views
|
36
|
+
|
37
|
+
url_for(:name) #=> '/path'
|
38
|
+
url_for(:short) #=> '/short'
|
39
|
+
|
40
|
+
### Params
|
41
|
+
|
42
|
+
You can pass paramaters to the route like so:
|
43
|
+
|
44
|
+
get named(:with_params, '/path/:id') do
|
45
|
+
...
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
url_for(:with_params, id: 2) #=> '/path/2'
|
50
|
+
|
51
|
+
|
52
|
+
### Namespaces
|
53
|
+
|
54
|
+
Named routes work with namespaces too:
|
55
|
+
|
56
|
+
namespace name(:admin) do
|
57
|
+
get named(:page, '/page/:id') do
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
url_for(:admin, :page, id: 3) #=> '/admin/page/3'
|
63
|
+
|
64
|
+
## Contributing
|
65
|
+
|
66
|
+
1. Fork it
|
67
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
68
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
69
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
70
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'tree'
|
2
|
+
|
3
|
+
module Sinatra
|
4
|
+
module NamedRoutes
|
5
|
+
module Exceptions
|
6
|
+
class NoRouteForName < Exception; end
|
7
|
+
class RouteNameTaken < Exception; end
|
8
|
+
end
|
9
|
+
class Router < Tree::TreeNode
|
10
|
+
def self.default
|
11
|
+
@@default ||= Router.new(:root, '')
|
12
|
+
end
|
13
|
+
|
14
|
+
def traverse path
|
15
|
+
base = self
|
16
|
+
path.each do |step|
|
17
|
+
base = base[step]
|
18
|
+
yield(base) if block_given?
|
19
|
+
end
|
20
|
+
base
|
21
|
+
end
|
22
|
+
|
23
|
+
def set pattern, name, path = []
|
24
|
+
node = traverse(path)
|
25
|
+
raise Sinatra::NamedRoutes::Exceptions::RouteNameTaken if node[name] && node[name].content != pattern
|
26
|
+
node << Tree::TreeNode.new(name, pattern) unless node[name]
|
27
|
+
end
|
28
|
+
|
29
|
+
def get *args
|
30
|
+
options = args.last.is_a?(::Hash) ? args.pop : {}
|
31
|
+
path = ''
|
32
|
+
traverse(args) do |node|
|
33
|
+
begin
|
34
|
+
path += node.content
|
35
|
+
rescue
|
36
|
+
raise Sinatra::NamedRoutes::Exceptions::NoRouteForName, "No route for name #{args.inspect}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
query = []
|
41
|
+
options.each do |k, v|
|
42
|
+
regex = /(:#{k})/
|
43
|
+
if path =~ regex
|
44
|
+
path.gsub!(regex, v.to_s)
|
45
|
+
else
|
46
|
+
query << "#{k}=#{v}"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
path += '?'+query.join("&") unless query.empty?
|
51
|
+
|
52
|
+
path
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require "sinatra/named_routes/version"
|
2
|
+
require 'sinatra/named_routes/router'
|
3
|
+
|
4
|
+
require 'sinatra/base'
|
5
|
+
|
6
|
+
module Sinatra
|
7
|
+
module Namespace
|
8
|
+
module SharedMethods
|
9
|
+
def named(name, pattern = "/#{name}")
|
10
|
+
router.set(pattern, name, get_namespaces)
|
11
|
+
pattern
|
12
|
+
end
|
13
|
+
|
14
|
+
def get_namespaces
|
15
|
+
patterns = [@pattern]
|
16
|
+
next_base = base
|
17
|
+
while(next_base)
|
18
|
+
next_base, pattern = next_base.class_eval {
|
19
|
+
[defined?(base) && base, @pattern]
|
20
|
+
}
|
21
|
+
patterns << pattern if pattern
|
22
|
+
end
|
23
|
+
|
24
|
+
node = router
|
25
|
+
|
26
|
+
patterns.reverse.map do |p|
|
27
|
+
node = node.children.find {|c| c.content == p}
|
28
|
+
node.name
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
module NamedRoutes
|
35
|
+
def self.registered(app)
|
36
|
+
app.set :router, Router.default
|
37
|
+
end
|
38
|
+
|
39
|
+
def named(name, pattern = "/#{name}")
|
40
|
+
router.set(pattern, name)
|
41
|
+
pattern
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
module Helpers
|
46
|
+
def url_for(*args)
|
47
|
+
self.class.router.get(*args)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/sinatra/named_routes/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Federico Romero"]
|
6
|
+
gem.email = ["federomero@gmail.com"]
|
7
|
+
gem.description = %q{Sinatra extension that allows you to use name your routes}
|
8
|
+
gem.summary = %q{Sinatra named routes}
|
9
|
+
gem.homepage = "https://github.com/federomero/sinatra-named_routes"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "sinatra-named_routes"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Sinatra::NamedRoutes::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'sinatra'
|
19
|
+
gem.add_dependency 'tree'
|
20
|
+
gem.add_development_dependency 'sinatra-contrib'
|
21
|
+
gem.add_development_dependency 'rake'
|
22
|
+
gem.add_development_dependency 'minitest', '3.2.0'
|
23
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
|
3
|
+
require 'sinatra/named_routes/router'
|
4
|
+
|
5
|
+
describe Sinatra::NamedRoutes::Router do
|
6
|
+
|
7
|
+
subject do
|
8
|
+
Sinatra::NamedRoutes::Router.new(:root, '')
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should handle a simple route' do
|
12
|
+
subject.set('/admin', :admin)
|
13
|
+
subject.get(:admin).must_equal '/admin'
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should allow to set a route with params' do
|
17
|
+
subject.set('/petition/:id', :petition)
|
18
|
+
subject.get(:petition, id: 2).must_equal '/petition/2'
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should allow to nest routes' do
|
22
|
+
subject.set('/admin', :admin)
|
23
|
+
subject.set('/petition/:id', :petition, [:admin])
|
24
|
+
subject.get(:admin, :petition, id: 2).must_equal '/admin/petition/2'
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should not allow to use the same name with different patterns" do
|
28
|
+
proc {
|
29
|
+
subject.set('/one', :name)
|
30
|
+
subject.set('/two', :name)
|
31
|
+
}.must_raise Sinatra::NamedRoutes::Exceptions::RouteNameTaken
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
ENV['RACK_ENV'] = 'test'
|
2
|
+
require 'sinatra/base'
|
3
|
+
require 'sinatra/namespace'
|
4
|
+
require 'minitest/autorun'
|
5
|
+
require 'rack/test'
|
6
|
+
|
7
|
+
require_relative '../../lib/sinatra/named_routes'
|
8
|
+
|
9
|
+
Sinatra::Base.set :environment, :test
|
10
|
+
|
11
|
+
|
12
|
+
class MockApp < Sinatra::Base
|
13
|
+
register Sinatra::Namespace
|
14
|
+
register Sinatra::NamedRoutes
|
15
|
+
helpers Sinatra::NamedRoutes::Helpers
|
16
|
+
|
17
|
+
def self.router
|
18
|
+
@router ||= Sinatra::NamedRoutes::Router.new(:root, nil)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
module Rack::Test::Methods
|
23
|
+
def mock_app(base=MockApp, &block)
|
24
|
+
@app = Sinatra.new(base, &block)
|
25
|
+
end
|
26
|
+
def app
|
27
|
+
@app
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe 'NamedRoutes' do
|
32
|
+
include Rack::Test::Methods
|
33
|
+
|
34
|
+
it 'should handle the root path' do
|
35
|
+
mock_app{ get(named(:root, '/')) { "hi" }}
|
36
|
+
get "/"
|
37
|
+
|
38
|
+
last_response.status.must_equal 200
|
39
|
+
last_response.body.must_equal "hi"
|
40
|
+
end
|
41
|
+
|
42
|
+
describe 'with a literal path' do
|
43
|
+
it 'should find the route correctly' do
|
44
|
+
mock_app{ get(named(:some_name, '/some_path')) { "hi" }}
|
45
|
+
get "/some_path"
|
46
|
+
|
47
|
+
last_response.status.must_equal 200
|
48
|
+
last_response.body.must_equal "hi"
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should generate the route correctly' do
|
52
|
+
mock_app{ get(named(:some_name, '/some_path')) { url_for(:some_name) }}
|
53
|
+
|
54
|
+
get "/some_path"
|
55
|
+
|
56
|
+
last_response.status.must_equal 200
|
57
|
+
last_response.body.must_equal "/some_path"
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
describe 'with a params in path' do
|
64
|
+
it 'should find the route correctly' do
|
65
|
+
mock_app{ get(named(:some_name, '/some_path/:id')) { params[:id] }}
|
66
|
+
get "/some_path/2"
|
67
|
+
|
68
|
+
last_response.status.must_equal 200
|
69
|
+
last_response.body.must_equal "2"
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'should generate the route correctly' do
|
73
|
+
mock_app{ get(named(:some_name, '/some_path/:id')) { url_for(:some_name, id: 3) }}
|
74
|
+
|
75
|
+
get "/some_path/2"
|
76
|
+
|
77
|
+
last_response.status.must_equal 200
|
78
|
+
last_response.body.must_equal "/some_path/3"
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
describe 'with namespaces' do
|
85
|
+
it 'should handle routes inside a namespace' do
|
86
|
+
|
87
|
+
mock_app do
|
88
|
+
namespace named(:admin) do
|
89
|
+
get(named(:some_name, '/some_path/:id')) { url_for(:admin, :some_name, id: 3) }
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
get "/admin/some_path/2"
|
94
|
+
|
95
|
+
last_response.status.must_equal 200
|
96
|
+
last_response.body.must_equal "/admin/some_path/3"
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'should handle routes inside a nested namespace xxx' do
|
100
|
+
|
101
|
+
mock_app do
|
102
|
+
namespace named(:admin) do
|
103
|
+
namespace named(:namespace) do
|
104
|
+
get(named(:some_name, '/some_path/:id')) { url_for(:admin, :namespace, :some_name, id: 3) }
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
get "/admin/namespace/some_path/2"
|
110
|
+
|
111
|
+
last_response.status.must_equal 200
|
112
|
+
last_response.body.must_equal "/admin/namespace/some_path/3"
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'should handle defining the same namespace route twice' do
|
116
|
+
|
117
|
+
mock_app do
|
118
|
+
namespace named(:admin) do
|
119
|
+
namespace named(:namespace) do
|
120
|
+
get(named(:some_name, '/some_path/:id')) { url_for(:admin, :namespace, :some_name, id: 3) }
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
namespace named(:admin) do
|
125
|
+
get(named(:other_name, '/other_path/:id')) { url_for(:admin, :other_name, id: 3) }
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
get "/admin/namespace/some_path/2"
|
130
|
+
|
131
|
+
last_response.status.must_equal 200
|
132
|
+
last_response.body.must_equal "/admin/namespace/some_path/3"
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-named_routes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Federico Romero
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-16 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: sinatra
|
16
|
+
requirement: &70124965638460 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70124965638460
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: tree
|
27
|
+
requirement: &70124965637980 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70124965637980
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: sinatra-contrib
|
38
|
+
requirement: &70124965637520 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70124965637520
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
requirement: &70124965637060 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70124965637060
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: minitest
|
60
|
+
requirement: &70124965636440 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - =
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 3.2.0
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70124965636440
|
69
|
+
description: Sinatra extension that allows you to use name your routes
|
70
|
+
email:
|
71
|
+
- federomero@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/sinatra/named_routes.rb
|
82
|
+
- lib/sinatra/named_routes/router.rb
|
83
|
+
- lib/sinatra/named_routes/version.rb
|
84
|
+
- sinatra-named_routes.gemspec
|
85
|
+
- spec/sinatra/named_routes/router_spec.rb
|
86
|
+
- spec/sinatra/named_routes_spec.rb
|
87
|
+
homepage: https://github.com/federomero/sinatra-named_routes
|
88
|
+
licenses: []
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
segments:
|
100
|
+
- 0
|
101
|
+
hash: -3180515975400098296
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
segments:
|
109
|
+
- 0
|
110
|
+
hash: -3180515975400098296
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 1.8.10
|
114
|
+
signing_key:
|
115
|
+
specification_version: 3
|
116
|
+
summary: Sinatra named routes
|
117
|
+
test_files:
|
118
|
+
- spec/sinatra/named_routes/router_spec.rb
|
119
|
+
- spec/sinatra/named_routes_spec.rb
|