sinatra-named_route 0.1.0 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ed79558e6119e52d4cec5a13ed51ce496f4f222d
4
- data.tar.gz: f24ecc94950bd59d22ba6bea2841f1e5648a4efc
3
+ metadata.gz: a745ec1689e8bb31a0c5c4d6be40ffbe990be01e
4
+ data.tar.gz: c6f4304f45787485f700c5cf9a8f0ac835dc8823
5
5
  SHA512:
6
- metadata.gz: 597b48546d4c64f2e3f36b73a623f19f192c52e205e79bf345cc404f091790d7fa565e515fe6542d0c97be12cd980aa8379ad72bb84b26bbdb55928e54b0bf12
7
- data.tar.gz: aa33f646a99d1bf3aec4374634e85959347a3d02bde14ff67b2135608e4950e4872cfe2833215c310d2257f142af3ec02baf7cd40f971347eb8d273731e5922e
6
+ metadata.gz: 225d4bdd8bc42207c996eebe8ccc985b10df5aa1d147a583b2b7e113f1b9b9b69ff07c6548a251b92b59fcad47118e6ccc32e4b659f7563b960f8f9570e288b3
7
+ data.tar.gz: 65065f60dcc1f71c6d7ad6510706fa55763cf1b2b053f854eccfc3acd3abaeacf049d9f9fa2e99e34c06e2c75cef34f5ac893f1ce547b6077121fa8934f9bcd4
data/Gemfile CHANGED
@@ -4,5 +4,6 @@ gem 'rake'
4
4
  gem 'minitest'
5
5
  gem 'rack-test'
6
6
  gem 'sinatra', '= 1.4.5', require: 'sinatra/base'
7
+ gem 'mocha'
7
8
  # Specify your gem's dependencies in sinatra-named_route.gemspec
8
9
  gemspec
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Sinatra::NamedRoute
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/sinatra/named_route`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Write usage instructions here
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/[USERNAME]/sinatra-named_route.
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
@@ -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 << { name: key }
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
- key = args.first
28
- signature = settings.named_routes.find { |name:, **o| key == name }
29
- fail RouteNotFound unless signature
30
- signature[:pattern].expand(**(args[1] || {}))
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
@@ -1,5 +1,5 @@
1
1
  module Sinatra
2
2
  module NamedRoute
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  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.0
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-14 00:00:00.000000000 Z
11
+ date: 2016-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mustermann