route_matcher 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +23 -0
- data/lib/route_matcher.rb +1 -0
- data/lib/route_matcher/param_matcher.rb +39 -0
- data/lib/route_matcher/url_matcher.rb +0 -4
- data/lib/route_matcher/version.rb +1 -1
- data/route_matcher.gemspec +2 -4
- metadata +21 -6
data/README.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# RouteMatcher
|
2
|
+
Your Rails 3 route helper for pretty URLs sharing the same dynamic path "part"
|
3
|
+
|
4
|
+
For example, have both routes on the same path part
|
5
|
+
|
6
|
+
# in config/routes.rb
|
7
|
+
':brand' => 'brands#show' # /nike
|
8
|
+
':brand_category' => 'brands#category' # /fashion
|
9
|
+
|
10
|
+
With rails 3 you can use constrains to seperate between them. But what if you those constrains are data driven? Then RouteMatcher may come in handy:
|
11
|
+
|
12
|
+
# in config/routes.rb
|
13
|
+
':brand' => 'brands#show', constraints => RouteMatcher::UrlMatcher.new('brands') { Brand.map{|b| "/#{b.slug}" } }
|
14
|
+
|
15
|
+
# in brand.rb
|
16
|
+
after_save { RouteMatcher::UrlMatcher.mark_dirty('brands') }
|
17
|
+
|
18
|
+
* Matcher lazy loads, it prevents database calls at the start of the app (and rake tasks for example)
|
19
|
+
* It caches the allowed URLs in memory to prevent database calls on each request
|
20
|
+
* Uses a timestamp in Rails.cache for cache invalidation
|
21
|
+
|
22
|
+
## Known issues
|
23
|
+
* If you have a multi process production environment (which is pretty likely), it will only work with a shared Rails cache, like memcache (cause of the timestamps)
|
data/lib/route_matcher.rb
CHANGED
@@ -0,0 +1,39 @@
|
|
1
|
+
module RouteMatcher
|
2
|
+
class ParamMatcher
|
3
|
+
CACHE_KEY = "route_matcher_param_matcher_%s"
|
4
|
+
|
5
|
+
def initialize(name, &block)
|
6
|
+
@name, @block = name, block
|
7
|
+
Rails.cache.write(CACHE_KEY % @name, Time.now, :unless_exists => true)
|
8
|
+
end
|
9
|
+
|
10
|
+
def matches?(request)
|
11
|
+
reload if @timestamp != get_stamp
|
12
|
+
|
13
|
+
# make sure new parameters are made for the actual route
|
14
|
+
params = request.params
|
15
|
+
request.env["action_dispatch.request.parameters"] = nil
|
16
|
+
|
17
|
+
case @match_with
|
18
|
+
when Array
|
19
|
+
@match_with.any? {|m| m === params[@name] }
|
20
|
+
when String, Regexp
|
21
|
+
@match_with === params[@name]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def reload
|
26
|
+
@match_with = @block.call
|
27
|
+
@timestamp = get_stamp
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.mark_dirty(name)
|
31
|
+
Rails.cache.write(CACHE_KEY % name, Time.now)
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
def get_stamp
|
36
|
+
Rails.cache.read(CACHE_KEY % @name)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/route_matcher.gemspec
CHANGED
@@ -8,10 +8,8 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.authors = ["Raymond Vellener", "Jaap van der Plas"]
|
9
9
|
s.email = ["raymond@brightin.nl", "jaap@brightin.nl"]
|
10
10
|
s.homepage = "http://brightin.nl"
|
11
|
-
s.summary = %q{
|
12
|
-
s.description = %q{
|
13
|
-
|
14
|
-
s.rubyforge_project = "route_matcher"
|
11
|
+
s.summary = %q{ Your Rails 3 route helper for pretty urls sharing the same dynamic path "part" }
|
12
|
+
s.description = %q{ Your Rails 3 route helper for pretty urls sharing the same dynamic path "part". For example, use brands names and brand categories directly after the domain: /nike, /adidas, but also /sports and /fashion }
|
15
13
|
|
16
14
|
s.files = `git ls-files`.split("\n")
|
17
15
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
metadata
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: route_matcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
4
5
|
prerelease:
|
5
|
-
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 6
|
10
|
+
version: 0.0.6
|
6
11
|
platform: ruby
|
7
12
|
authors:
|
8
13
|
- Raymond Vellener
|
@@ -11,10 +16,11 @@ autorequire:
|
|
11
16
|
bindir: bin
|
12
17
|
cert_chain: []
|
13
18
|
|
14
|
-
date: 2011-06-
|
19
|
+
date: 2011-06-05 00:00:00 +02:00
|
20
|
+
default_executable:
|
15
21
|
dependencies: []
|
16
22
|
|
17
|
-
description: "
|
23
|
+
description: " Your Rails 3 route helper for pretty urls sharing the same dynamic path \"part\". For example, use brands names and brand categories directly after the domain: /nike, /adidas, but also /sports and /fashion "
|
18
24
|
email:
|
19
25
|
- raymond@brightin.nl
|
20
26
|
- jaap@brightin.nl
|
@@ -27,11 +33,14 @@ extra_rdoc_files: []
|
|
27
33
|
files:
|
28
34
|
- .gitignore
|
29
35
|
- Gemfile
|
36
|
+
- README.md
|
30
37
|
- Rakefile
|
31
38
|
- lib/route_matcher.rb
|
39
|
+
- lib/route_matcher/param_matcher.rb
|
32
40
|
- lib/route_matcher/url_matcher.rb
|
33
41
|
- lib/route_matcher/version.rb
|
34
42
|
- route_matcher.gemspec
|
43
|
+
has_rdoc: true
|
35
44
|
homepage: http://brightin.nl
|
36
45
|
licenses: []
|
37
46
|
|
@@ -45,19 +54,25 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
45
54
|
requirements:
|
46
55
|
- - ">="
|
47
56
|
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
48
60
|
version: "0"
|
49
61
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
62
|
none: false
|
51
63
|
requirements:
|
52
64
|
- - ">="
|
53
65
|
- !ruby/object:Gem::Version
|
66
|
+
hash: 3
|
67
|
+
segments:
|
68
|
+
- 0
|
54
69
|
version: "0"
|
55
70
|
requirements: []
|
56
71
|
|
57
|
-
rubyforge_project:
|
58
|
-
rubygems_version: 1.
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.4.2
|
59
74
|
signing_key:
|
60
75
|
specification_version: 3
|
61
|
-
summary:
|
76
|
+
summary: Your Rails 3 route helper for pretty urls sharing the same dynamic path "part"
|
62
77
|
test_files: []
|
63
78
|
|