tred-fancyroutes 0.9.2 → 0.9.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +2 -2
- data/lib/fancyroutes.rb +30 -30
- data/spec/fancyroutes_spec.rb +39 -31
- metadata +7 -6
data/Rakefile
CHANGED
@@ -5,7 +5,7 @@ require 'date'
|
|
5
5
|
require 'spec/rake/spectask'
|
6
6
|
|
7
7
|
GEM = "fancyroutes"
|
8
|
-
GEM_VERSION = "0.9.
|
8
|
+
GEM_VERSION = "0.9.3"
|
9
9
|
AUTHOR = "The TRED Team"
|
10
10
|
EMAIL = "tred3000@gmail.com"
|
11
11
|
HOMEPAGE = "http://tred.github.com/fancyroutes"
|
@@ -22,7 +22,7 @@ spec = Gem::Specification.new do |s|
|
|
22
22
|
s.email = EMAIL
|
23
23
|
s.homepage = HOMEPAGE
|
24
24
|
s.summary = SUMMARY
|
25
|
-
|
25
|
+
|
26
26
|
s.require_path = 'lib'
|
27
27
|
s.autorequire = GEM
|
28
28
|
s.files = %w(LICENSE README.md Rakefile) + Dir.glob("{lib,spec}/**/*")
|
data/lib/fancyroutes.rb
CHANGED
@@ -1,91 +1,91 @@
|
|
1
1
|
module FancyRoutes
|
2
2
|
|
3
3
|
class Route
|
4
|
-
|
4
|
+
|
5
5
|
def initialize
|
6
6
|
@segments = []
|
7
7
|
end
|
8
|
-
|
8
|
+
|
9
9
|
def request_method(method)
|
10
10
|
@request_method = method.to_sym
|
11
11
|
self
|
12
12
|
end
|
13
|
-
|
13
|
+
|
14
14
|
%w(get put post delete).each do |meth|
|
15
15
|
define_method(meth) { request_method(meth) }
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
attr_writer :name
|
19
|
-
|
19
|
+
|
20
20
|
def segment(segment)
|
21
21
|
segment = case segment
|
22
|
-
when Symbol
|
22
|
+
when Symbol
|
23
23
|
":#{segment}"
|
24
24
|
when Hash
|
25
25
|
self.send(segment.values.first, segment.keys.first)
|
26
26
|
segment.keys.first.to_s
|
27
|
-
else
|
28
|
-
segment.to_s
|
27
|
+
else
|
28
|
+
segment.to_s
|
29
29
|
end
|
30
30
|
@segments << segment
|
31
31
|
self
|
32
32
|
end
|
33
33
|
alias_method :/, :segment
|
34
|
-
|
34
|
+
|
35
35
|
def controller(controller)
|
36
36
|
@controller = controller.to_s
|
37
37
|
self
|
38
38
|
end
|
39
39
|
alias_method :>>, :controller
|
40
|
-
|
40
|
+
|
41
41
|
def action(action)
|
42
42
|
@action = action.to_s
|
43
43
|
self
|
44
44
|
end
|
45
45
|
alias_method :>, :action
|
46
|
-
|
46
|
+
|
47
47
|
def apply(rails_map)
|
48
48
|
rails_map.send( (@name ? @name : :connect),
|
49
49
|
@segments.join("/"), {
|
50
|
-
:controller => @controller.underscore,
|
51
|
-
:action => @action.underscore,
|
50
|
+
:controller => @controller.try(:underscore),
|
51
|
+
:action => @action.try(:underscore),
|
52
52
|
:conditions => { :method => @request_method }
|
53
53
|
}
|
54
54
|
)
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
# hacky deep clone
|
58
58
|
def copy
|
59
|
-
Marshal::load(Marshal.dump(self))
|
59
|
+
Marshal::load(Marshal.dump(self))
|
60
60
|
end
|
61
61
|
|
62
62
|
end
|
63
|
-
|
63
|
+
|
64
64
|
class RootRoute < Route
|
65
|
-
|
65
|
+
|
66
66
|
def apply(rails_map)
|
67
67
|
rails_map.root :controller => @controller,
|
68
68
|
:action => @action,
|
69
69
|
:conditions => { :method => @request_method }
|
70
70
|
end
|
71
|
-
|
71
|
+
|
72
72
|
end
|
73
73
|
|
74
74
|
# may be clearer to use 3 classes here: RouteSet, MasterSet & NestedSet
|
75
|
-
|
75
|
+
|
76
76
|
class RouteSet
|
77
|
-
|
77
|
+
|
78
78
|
attr_reader :routes
|
79
|
-
|
79
|
+
|
80
80
|
def initialize(template_route = nil)
|
81
81
|
@template_route = template_route
|
82
82
|
@routes = []
|
83
83
|
end
|
84
|
-
|
84
|
+
|
85
85
|
def apply(rails_route_map)
|
86
86
|
@routes.each { |route| route.apply(rails_route_map) }
|
87
87
|
end
|
88
|
-
|
88
|
+
|
89
89
|
# builds a new route
|
90
90
|
def route(meth = nil, name = nil)
|
91
91
|
route = @template_route ? @template_route.copy : (name == :root ? RootRoute.new : Route.new)
|
@@ -94,30 +94,30 @@ module FancyRoutes
|
|
94
94
|
@routes << route
|
95
95
|
route
|
96
96
|
end
|
97
|
-
|
97
|
+
|
98
98
|
%w(get put post delete).each do |meth|
|
99
99
|
define_method(meth) { route(meth) }
|
100
100
|
end
|
101
|
-
|
101
|
+
|
102
102
|
def root
|
103
103
|
route(:get, :root)
|
104
104
|
end
|
105
|
-
|
105
|
+
|
106
106
|
def method_missing(meth,*args)
|
107
107
|
route(nil,meth)
|
108
108
|
end
|
109
|
-
|
109
|
+
|
110
110
|
def with(route, &blk)
|
111
111
|
# this route is used as a template, not as an actual route so we remove
|
112
112
|
# it from the routes collection
|
113
|
-
@routes.pop
|
113
|
+
@routes.pop
|
114
114
|
nested_set = self.class.new(route)
|
115
115
|
nested_set.instance_eval(&blk)
|
116
116
|
@routes += nested_set.routes
|
117
117
|
end
|
118
|
-
|
118
|
+
|
119
119
|
end
|
120
|
-
|
120
|
+
|
121
121
|
end
|
122
122
|
|
123
123
|
def FancyRoutes(m, &blk)
|
data/spec/fancyroutes_spec.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/spec_helper'
|
2
2
|
|
3
3
|
describe "FancyRoutes" do
|
4
|
-
|
4
|
+
|
5
5
|
before(:each) do
|
6
6
|
@map = Object.new
|
7
7
|
end
|
@@ -14,14 +14,14 @@ describe "FancyRoutes" do
|
|
14
14
|
:action => 'my_action',
|
15
15
|
:conditions => { :method => :get },
|
16
16
|
})
|
17
|
-
|
18
|
-
FancyRoutes(@map) do
|
17
|
+
|
18
|
+
FancyRoutes(@map) do
|
19
19
|
route(:get).segment('').controller(:my_controller).action(:my_action)
|
20
20
|
end
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
# an now lets dry up the mocking and route building
|
24
|
-
|
24
|
+
|
25
25
|
def fancyroutes(&blk)
|
26
26
|
FancyRoutes(@map,&blk)
|
27
27
|
end
|
@@ -36,44 +36,52 @@ describe "FancyRoutes" do
|
|
36
36
|
|
37
37
|
example "short form route on root" do
|
38
38
|
expect :get, '', 'my_controller', 'my_action'
|
39
|
-
|
39
|
+
|
40
40
|
fancyroutes do
|
41
41
|
get / '' >> :my_controller > :my_action
|
42
42
|
end
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
example "short form route on order" do
|
46
46
|
expect :get, ':slug/order', 'my_controller', 'my_action'
|
47
|
-
|
48
|
-
fancyroutes do
|
47
|
+
|
48
|
+
fancyroutes do
|
49
49
|
get / :slug / 'order' >> :my_controller > :my_action
|
50
50
|
end
|
51
51
|
end
|
52
|
-
|
52
|
+
|
53
53
|
example "short form route on order as put" do
|
54
54
|
expect :put, ':slug/order', 'my_controller', 'my_action'
|
55
|
-
|
55
|
+
|
56
56
|
fancyroutes do
|
57
57
|
put / :slug / 'order' >> :my_controller > :my_action
|
58
58
|
end
|
59
59
|
end
|
60
|
-
|
60
|
+
|
61
|
+
example "default Rails route" do
|
62
|
+
expect :get, ':controller/:action', nil, nil
|
63
|
+
|
64
|
+
fancyroutes do
|
65
|
+
get / :controller / :action
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
61
69
|
example "short form route nested route" do
|
62
70
|
expect :get, ':slug/order', 'my_controller', 'get_action'
|
63
71
|
expect :post, ':slug/order', 'my_controller', 'post_action'
|
64
|
-
|
65
|
-
fancyroutes do
|
72
|
+
|
73
|
+
fancyroutes do
|
66
74
|
with route / :slug >> :my_controller do
|
67
75
|
get / 'order' > :get_action
|
68
76
|
post / 'order' > :post_action
|
69
77
|
end
|
70
78
|
end
|
71
79
|
end
|
72
|
-
|
80
|
+
|
73
81
|
example "short form route nested route on method only" do
|
74
82
|
expect :get, ':slug/order', 'my_controller', 'get_action'
|
75
83
|
expect :post, ':slug/order', 'my_controller', 'post_action'
|
76
|
-
|
84
|
+
|
77
85
|
fancyroutes do
|
78
86
|
with route / :slug / 'order' >> :my_controller do
|
79
87
|
get > :get_action
|
@@ -81,11 +89,11 @@ describe "FancyRoutes" do
|
|
81
89
|
end
|
82
90
|
end
|
83
91
|
end
|
84
|
-
|
92
|
+
|
85
93
|
example "nested in nested routes" do
|
86
94
|
expect :get, ':slug/order', 'my_controller', 'get_action'
|
87
95
|
expect :post, ':slug/order', 'my_controller', 'post_action'
|
88
|
-
|
96
|
+
|
89
97
|
fancyroutes do
|
90
98
|
with route / :slug do
|
91
99
|
with route >> :my_controller do
|
@@ -95,45 +103,45 @@ describe "FancyRoutes" do
|
|
95
103
|
end
|
96
104
|
end
|
97
105
|
end
|
98
|
-
|
106
|
+
|
99
107
|
example "with named segments" do
|
100
108
|
expect :get, 'my_controller/my_action', 'my_controller', 'my_action'
|
101
|
-
|
102
|
-
fancyroutes do
|
109
|
+
|
110
|
+
fancyroutes do
|
103
111
|
get / {'my_controller' => :controller} / {'my_action' => :action}
|
104
112
|
end
|
105
113
|
end
|
106
|
-
|
114
|
+
|
107
115
|
example "with named segments using dashes" do
|
108
116
|
expect :get, 'my-controller/my-action', 'my_controller', 'my_action'
|
109
|
-
|
110
|
-
fancyroutes do
|
117
|
+
|
118
|
+
fancyroutes do
|
111
119
|
get / {'my-controller' => :controller} / {'my-action' => :action}
|
112
120
|
end
|
113
121
|
end
|
114
|
-
|
122
|
+
|
115
123
|
example "a named route" do
|
116
124
|
mock(@map).my_name('my_controller/:image', {
|
117
125
|
:controller => 'my_controller',
|
118
126
|
:action => 'my_action',
|
119
127
|
:conditions => { :method => :get }
|
120
128
|
})
|
121
|
-
|
122
|
-
fancyroutes do
|
129
|
+
|
130
|
+
fancyroutes do
|
123
131
|
my_name.get / {'my_controller' => :controller} / :image > :my_action
|
124
132
|
end
|
125
133
|
end
|
126
|
-
|
134
|
+
|
127
135
|
example "root route" do
|
128
136
|
mock(@map).root(
|
129
137
|
:controller => 'my_controller',
|
130
138
|
:action => 'my_action',
|
131
139
|
:conditions => { :method => :get }
|
132
140
|
)
|
133
|
-
|
141
|
+
|
134
142
|
fancyroutes do
|
135
143
|
root >> :my_controller > :my_action
|
136
144
|
end
|
137
145
|
end
|
138
|
-
|
139
|
-
end
|
146
|
+
|
147
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tred-fancyroutes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- The TRED Team
|
@@ -9,11 +9,11 @@ autorequire: fancyroutes
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-08-05 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
16
|
-
description:
|
16
|
+
description: ""
|
17
17
|
email: tred3000@gmail.com
|
18
18
|
executables: []
|
19
19
|
|
@@ -29,8 +29,9 @@ files:
|
|
29
29
|
- lib/fancyroutes.rb
|
30
30
|
- spec/fancyroutes_spec.rb
|
31
31
|
- spec/spec_helper.rb
|
32
|
-
has_rdoc:
|
32
|
+
has_rdoc: false
|
33
33
|
homepage: http://tred.github.com/fancyroutes
|
34
|
+
licenses:
|
34
35
|
post_install_message:
|
35
36
|
rdoc_options: []
|
36
37
|
|
@@ -51,9 +52,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
52
|
requirements: []
|
52
53
|
|
53
54
|
rubyforge_project:
|
54
|
-
rubygems_version: 1.
|
55
|
+
rubygems_version: 1.3.5
|
55
56
|
signing_key:
|
56
|
-
specification_version:
|
57
|
+
specification_version: 3
|
57
58
|
summary: Removing the cruft, bringing the HTTP method to the forefront and sporting a neat DRYing block syntax--FancyRoutes is a layer on top of the Rails routing system which provides an elegant API for mapping requests to your application's controllers and actions.
|
58
59
|
test_files: []
|
59
60
|
|