sinatra-named_route 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -0
- data/README.md +17 -5
- data/lib/sinatra/named_route.rb +15 -5
- data/lib/sinatra/named_route/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a745ec1689e8bb31a0c5c4d6be40ffbe990be01e
|
4
|
+
data.tar.gz: c6f4304f45787485f700c5cf9a8f0ac835dc8823
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 225d4bdd8bc42207c996eebe8ccc985b10df5aa1d147a583b2b7e113f1b9b9b69ff07c6548a251b92b59fcad47118e6ccc32e4b659f7563b960f8f9570e288b3
|
7
|
+
data.tar.gz: 65065f60dcc1f71c6d7ad6510706fa55763cf1b2b053f854eccfc3acd3abaeacf049d9f9fa2e99e34c06e2c75cef34f5ac893f1ce547b6077121fa8934f9bcd4
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# Sinatra::NamedRoute
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Supports named routes and makes it easy to access to URI path.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -22,7 +20,21 @@ Or install it yourself as:
|
|
22
20
|
|
23
21
|
## Usage
|
24
22
|
|
25
|
-
|
23
|
+
It's very simple.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class App < Sinatra::Base
|
27
|
+
register Sinatra::NamedRoute
|
28
|
+
|
29
|
+
get '/', name: :index do
|
30
|
+
redirect uri(:with_params, id: 1234, name: 'namusyaka') #=> Redirect to /params/1234/names/namusyaka
|
31
|
+
end
|
32
|
+
|
33
|
+
get '/params/:id/names/:name', name: :with_params do
|
34
|
+
"index is #{url(:index)}" #=> index is /
|
35
|
+
end
|
36
|
+
end
|
37
|
+
```
|
26
38
|
|
27
39
|
## Development
|
28
40
|
|
@@ -32,7 +44,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
32
44
|
|
33
45
|
## Contributing
|
34
46
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
47
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/namusyaka/sinatra-named_route.
|
36
48
|
|
37
49
|
|
38
50
|
## License
|
data/lib/sinatra/named_route.rb
CHANGED
@@ -6,9 +6,12 @@ module Sinatra
|
|
6
6
|
|
7
7
|
def self.registered(app)
|
8
8
|
app.helpers Helpers
|
9
|
+
app.set :named_routes_cache, {}
|
9
10
|
app.set :named_routes, named_routes
|
10
11
|
app.set :name do |key|
|
11
|
-
settings.named_routes << {
|
12
|
+
settings.named_routes << {
|
13
|
+
name: key
|
14
|
+
} unless settings.named_routes.any? { |signature| key == signature[:name] }
|
12
15
|
end
|
13
16
|
end
|
14
17
|
|
@@ -24,10 +27,17 @@ module Sinatra
|
|
24
27
|
module Helpers
|
25
28
|
def uri(*args)
|
26
29
|
return super unless args.first.is_a?(Symbol)
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
30
|
+
fetch_uri(args.first, cache_key: args) do |key|
|
31
|
+
signature = settings.named_routes.find { |name:, **o| key == name }
|
32
|
+
fail RouteNotFound unless signature
|
33
|
+
signature[:pattern].expand(args[1] || {})
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def fetch_uri(key, cache_key: nil)
|
38
|
+
cache = settings.named_routes_cache
|
39
|
+
return cache[cache_key] if cache[cache_key]
|
40
|
+
cache[cache_key] = yield(key)
|
31
41
|
end
|
32
42
|
end
|
33
43
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-named_route
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- namusyaka
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mustermann
|