rory 0.3.5 → 0.3.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rory/application.rb +5 -5
- data/lib/rory/dispatcher.rb +41 -31
- data/lib/rory/route_mapper.rb +10 -0
- data/lib/rory/support.rb +2 -13
- data/lib/rory/version.rb +1 -1
- data/rory.gemspec +1 -0
- data/spec/application_spec.rb +10 -9
- data/spec/dispatcher_spec.rb +32 -30
- data/spec/fixture_app/config/routes.rb +3 -0
- data/spec/fixture_app/controllers/goose/lumpies_controller.rb +12 -0
- data/spec/spec_helper.rb +4 -1
- data/spec/support_spec.rb +5 -13
- metadata +19 -2
data/lib/rory/application.rb
CHANGED
@@ -40,13 +40,13 @@ module Rory
|
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
|
-
def
|
44
|
-
@
|
43
|
+
def auto_require_paths
|
44
|
+
@auto_require_paths ||= %w(models controllers helpers)
|
45
45
|
end
|
46
46
|
|
47
|
-
def
|
48
|
-
|
49
|
-
Rory::Support.
|
47
|
+
def require_all_files
|
48
|
+
auto_require_paths.each do |path|
|
49
|
+
Rory::Support.require_all_files_in_directory root_path.join(path)
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
data/lib/rory/dispatcher.rb
CHANGED
@@ -5,46 +5,21 @@ module Rory
|
|
5
5
|
attr_reader :request
|
6
6
|
def initialize(rack_request, context = nil)
|
7
7
|
@request = rack_request
|
8
|
-
@request[:route] ||= nil
|
9
|
-
@request[:dispatcher] = self
|
10
8
|
@context = context
|
11
9
|
end
|
12
10
|
|
13
|
-
def
|
14
|
-
@
|
15
|
-
end
|
16
|
-
|
17
|
-
def get_route
|
18
|
-
match = nil
|
19
|
-
route = route_map.detect do |route_hash|
|
20
|
-
path_name = @request.path_info[1..-1] || ''
|
21
|
-
match = route_hash[:regex].match(path_name)
|
22
|
-
methods = route_hash[:methods] || []
|
23
|
-
match && (methods.empty? || methods.include?(method.to_sym))
|
24
|
-
end
|
25
|
-
if route
|
26
|
-
symbolized_param_names = match.names.map { |name| name.to_sym }
|
27
|
-
@request.params.merge! Hash[symbolized_param_names.zip(match.captures)]
|
28
|
-
end
|
29
|
-
route
|
11
|
+
def route
|
12
|
+
@request[:route] ||= get_route
|
30
13
|
end
|
31
14
|
|
32
15
|
def dispatch
|
33
|
-
|
34
|
-
|
35
|
-
if route
|
36
|
-
controller_name = Rory::Support.camelize("#{route[:controller]}_controller")
|
37
|
-
controller_class = Object.const_get(controller_name)
|
38
|
-
controller_class.new(@request, @context).present
|
16
|
+
if controller
|
17
|
+
controller.present
|
39
18
|
else
|
40
19
|
render_not_found
|
41
20
|
end
|
42
21
|
end
|
43
22
|
|
44
|
-
def set_route_if_empty
|
45
|
-
@request[:route] ||= get_route
|
46
|
-
end
|
47
|
-
|
48
23
|
def method
|
49
24
|
override_method = @request.params.delete('_method')
|
50
25
|
method = if override_method && ['put', 'patch', 'delete'].include?(override_method.downcase)
|
@@ -66,8 +41,43 @@ module Rory
|
|
66
41
|
return [ 404, {'Content-type' => 'text/html' }, ['Four, oh, four.'] ]
|
67
42
|
end
|
68
43
|
|
69
|
-
|
70
|
-
|
44
|
+
private
|
45
|
+
|
46
|
+
def controller
|
47
|
+
if klass = controller_class
|
48
|
+
klass.new(@request.merge(:dispatcher => self), @context)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def controller_class
|
53
|
+
if route
|
54
|
+
controller_name = Rory::Support.camelize("#{route[:controller]}_controller")
|
55
|
+
const_scope = if route[:module]
|
56
|
+
Object.const_get(Rory::Support.camelize("#{route[:module]}"))
|
57
|
+
else
|
58
|
+
Object
|
59
|
+
end
|
60
|
+
const_scope.const_get(controller_name)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def get_route
|
65
|
+
match = nil
|
66
|
+
mapped_route = route_map.detect do |route_hash|
|
67
|
+
path_name = @request.path_info[1..-1] || ''
|
68
|
+
match = route_hash[:regex].match(path_name)
|
69
|
+
methods = route_hash[:methods] || []
|
70
|
+
match && (methods.empty? || methods.include?(method.to_sym))
|
71
|
+
end
|
72
|
+
if mapped_route
|
73
|
+
symbolized_param_names = match.names.map { |name| name.to_sym }
|
74
|
+
@request.params.merge! Hash[symbolized_param_names.zip(match.captures)]
|
75
|
+
end
|
76
|
+
mapped_route
|
77
|
+
end
|
78
|
+
|
79
|
+
def route_map
|
80
|
+
@context ? @context.routes : []
|
71
81
|
end
|
72
82
|
end
|
73
83
|
end
|
data/lib/rory/route_mapper.rb
CHANGED
@@ -12,13 +12,22 @@ module Rory
|
|
12
12
|
|
13
13
|
def initialize
|
14
14
|
@routes = []
|
15
|
+
@scope_options = {}
|
15
16
|
end
|
16
17
|
|
17
18
|
def routing_map
|
18
19
|
@routes
|
19
20
|
end
|
20
21
|
|
22
|
+
def scope(options = {}, &block)
|
23
|
+
previous_options, @scope_options =
|
24
|
+
@scope_options, @scope_options.merge(options)
|
25
|
+
yield
|
26
|
+
@scope_options = previous_options
|
27
|
+
end
|
28
|
+
|
21
29
|
def match(mask, options = {})
|
30
|
+
options.merge!(@scope_options)
|
22
31
|
options[:to] ||= mask.split('/').first
|
23
32
|
mask.gsub!(/^\//, '')
|
24
33
|
regex = /^#{mask.gsub(/:([\w_]+)/, "(?<\\1>\[\^\\\/\]+)")}$/
|
@@ -28,6 +37,7 @@ module Rory
|
|
28
37
|
:action => action,
|
29
38
|
:regex => regex
|
30
39
|
}
|
40
|
+
route[:module] = options[:module] if options[:module]
|
31
41
|
route[:methods] = options[:methods] if options[:methods]
|
32
42
|
@routes << route
|
33
43
|
end
|
data/lib/rory/support.rb
CHANGED
@@ -9,20 +9,9 @@ module Rory
|
|
9
9
|
string = string.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }.gsub('/', '::')
|
10
10
|
end
|
11
11
|
|
12
|
-
def
|
13
|
-
name = File.basename(path).sub(/(.*)\.rb$/, '\1')
|
14
|
-
name = camelize(name)
|
15
|
-
end
|
16
|
-
|
17
|
-
def autoload_file(path)
|
18
|
-
path = File.expand_path(path)
|
19
|
-
name = extract_class_name_from_path(path)
|
20
|
-
Object.autoload name.to_sym, path
|
21
|
-
end
|
22
|
-
|
23
|
-
def autoload_all_files_in_directory(path)
|
12
|
+
def require_all_files_in_directory(path)
|
24
13
|
Dir[Pathname.new(path).join('**', '*.rb')].each do |file|
|
25
|
-
|
14
|
+
require file
|
26
15
|
end
|
27
16
|
end
|
28
17
|
end
|
data/lib/rory/version.rb
CHANGED
data/rory.gemspec
CHANGED
data/spec/application_spec.rb
CHANGED
@@ -76,6 +76,7 @@ describe Rory::Application do
|
|
76
76
|
{ :controller => 'foo', :action => 'bar', :regex => /^foo\/(?<id>[^\/]+)\/bar$/, :methods => [:get, :post] },
|
77
77
|
{ :controller => 'monkeys', :action => nil, :regex => /^foo$/, :methods => [:put] },
|
78
78
|
{ :controller => 'awesome', :action => 'rad', :regex => /^this\/(?<path>[^\/]+)\/is\/(?<very_awesome>[^\/]+)$/},
|
79
|
+
{ :controller => 'lumpies', :action => 'show', :regex => /^lumpies\/(?<lump>[^\/]+)$/, :module => 'goose', :methods => [:get] },
|
79
80
|
{ :controller => 'root', :action => 'vegetable', :regex => /^$/, :methods => [:get] }
|
80
81
|
].inspect
|
81
82
|
end
|
@@ -90,27 +91,27 @@ describe Rory::Application do
|
|
90
91
|
|
91
92
|
describe '.autoload_paths' do
|
92
93
|
after(:each) do
|
93
|
-
Fixture::Application.instance.instance_variable_set(:@
|
94
|
+
Fixture::Application.instance.instance_variable_set(:@auto_require_paths, nil)
|
94
95
|
end
|
95
96
|
|
96
97
|
it 'includes models, controllers, and helpers by default' do
|
97
|
-
Fixture::Application.
|
98
|
+
Fixture::Application.auto_require_paths.should == ['models', 'controllers', 'helpers']
|
98
99
|
end
|
99
100
|
|
100
101
|
it 'accepts new paths' do
|
101
|
-
Fixture::Application.
|
102
|
-
Fixture::Application.
|
102
|
+
Fixture::Application.auto_require_paths << 'chocolates'
|
103
|
+
Fixture::Application.auto_require_paths.should == ['models', 'controllers', 'helpers', 'chocolates']
|
103
104
|
end
|
104
105
|
end
|
105
106
|
|
106
|
-
describe '.
|
107
|
-
it '
|
108
|
-
Fixture::Application.any_instance.stub(:
|
107
|
+
describe '.require_all_files' do
|
108
|
+
it 'requires all files in auto_require_paths' do
|
109
|
+
Fixture::Application.any_instance.stub(:auto_require_paths).and_return(['goats', 'rhubarbs'])
|
109
110
|
[:goats, :rhubarbs].each do |folder|
|
110
|
-
Rory::Support.should_receive(:
|
111
|
+
Rory::Support.should_receive(:require_all_files_in_directory).
|
111
112
|
with(Pathname.new(Fixture::Application.root).join("#{folder}"))
|
112
113
|
end
|
113
|
-
Fixture::Application.
|
114
|
+
Fixture::Application.require_all_files
|
114
115
|
end
|
115
116
|
end
|
116
117
|
end
|
data/spec/dispatcher_spec.rb
CHANGED
@@ -22,52 +22,54 @@ describe Rory::Dispatcher do
|
|
22
22
|
end
|
23
23
|
|
24
24
|
describe "#dispatch" do
|
25
|
+
let(:dispatcher) { Rory::Dispatcher.new(request, Fixture::Application) }
|
26
|
+
let(:request) {
|
27
|
+
request = { :whatever => :yay }
|
28
|
+
request.stub(:path_info => '/', :request_method => 'GET', :params => {})
|
29
|
+
request
|
30
|
+
}
|
31
|
+
|
25
32
|
it "renders a 404 if the requested path is invalid" do
|
26
|
-
|
27
|
-
|
28
|
-
@dispatcher = Rory::Dispatcher.new(@request, Fixture::Application)
|
29
|
-
@dispatcher.stub(:get_route).and_return(nil)
|
30
|
-
@dispatcher.dispatch[0..1].should == [404, {"Content-type"=>"text/html"}]
|
33
|
+
dispatcher.stub(:get_route).and_return(nil)
|
34
|
+
dispatcher.dispatch[0..1].should == [404, {"Content-type"=>"text/html"}]
|
31
35
|
end
|
32
36
|
|
33
37
|
it "instantiates a controller with the parsed request and calls present" do
|
34
|
-
|
35
|
-
|
36
|
-
@dispatcher = Rory::Dispatcher.new(@request, Fixture::Application)
|
37
|
-
route = { :controller => 'stub' }
|
38
|
-
@dispatcher.stub(:get_route).and_return(route)
|
39
|
-
@dispatcher.dispatch.should == {
|
38
|
+
request[:route] = { :controller => 'stub' }
|
39
|
+
dispatcher.dispatch.should == {
|
40
40
|
:whatever => :yay,
|
41
|
-
:route => route,
|
42
|
-
:dispatcher =>
|
41
|
+
:route => request[:route],
|
42
|
+
:dispatcher => dispatcher,
|
43
43
|
:present_called => true # see StubController in /spec/fixture_app
|
44
44
|
}
|
45
45
|
end
|
46
46
|
|
47
|
-
it "
|
48
|
-
|
49
|
-
|
50
|
-
@dispatcher = Rory::Dispatcher.new(@request, Fixture::Application)
|
51
|
-
@dispatcher.should_receive(:get_route).never
|
52
|
-
@dispatcher.dispatch.should == {
|
47
|
+
it "dispatches properly to a scoped controller" do
|
48
|
+
request[:route] = { :controller => 'lumpies', :module => 'goose' }
|
49
|
+
dispatcher.dispatch.should == {
|
53
50
|
:whatever => :yay,
|
54
|
-
:route =>
|
55
|
-
:dispatcher =>
|
56
|
-
:
|
51
|
+
:route => request[:route],
|
52
|
+
:dispatcher => dispatcher,
|
53
|
+
:in_scoped_controller => true # see Goose::LumpiesController in /spec/fixture_app
|
57
54
|
}
|
58
55
|
end
|
59
56
|
end
|
60
57
|
|
61
|
-
describe "#
|
58
|
+
describe "#route" do
|
62
59
|
before(:each) do
|
63
60
|
@request = {}
|
64
61
|
@request.stub(:params => {})
|
65
62
|
@dispatcher = Rory::Dispatcher.new(@request, Fixture::Application)
|
66
63
|
end
|
67
64
|
|
65
|
+
it "returns route from request if already set" do
|
66
|
+
@request[:route] = 'snaky pigeons'
|
67
|
+
@dispatcher.route.should == 'snaky pigeons'
|
68
|
+
end
|
69
|
+
|
68
70
|
it "matches the path from the request to the routes table" do
|
69
71
|
@request.stub(:path_info => '/foo', :request_method => 'PUT')
|
70
|
-
@dispatcher.
|
72
|
+
@dispatcher.route.should == {
|
71
73
|
:controller => 'monkeys',
|
72
74
|
:action => nil,
|
73
75
|
:regex => /^foo$/,
|
@@ -77,7 +79,7 @@ describe Rory::Dispatcher do
|
|
77
79
|
|
78
80
|
it "works with empty path" do
|
79
81
|
@request.stub(:path_info => '', :request_method => 'GET')
|
80
|
-
@dispatcher.
|
82
|
+
@dispatcher.route.should == {
|
81
83
|
:controller => 'root',
|
82
84
|
:action => 'vegetable',
|
83
85
|
:regex => /^$/,
|
@@ -87,7 +89,7 @@ describe Rory::Dispatcher do
|
|
87
89
|
|
88
90
|
it "works with root url represented by slash" do
|
89
91
|
@request.stub(:path_info => '/', :request_method => 'GET')
|
90
|
-
@dispatcher.
|
92
|
+
@dispatcher.route.should == {
|
91
93
|
:controller => 'root',
|
92
94
|
:action => 'vegetable',
|
93
95
|
:regex => /^$/,
|
@@ -97,22 +99,22 @@ describe Rory::Dispatcher do
|
|
97
99
|
|
98
100
|
it "returns nil if no route found" do
|
99
101
|
@request.stub(:path_info => '/umbrellas', :request_method => 'GET')
|
100
|
-
@dispatcher.
|
102
|
+
@dispatcher.route.should be_nil
|
101
103
|
end
|
102
104
|
|
103
105
|
it "returns nil if no context" do
|
104
106
|
@dispatcher = Rory::Dispatcher.new(@request)
|
105
|
-
@dispatcher.
|
107
|
+
@dispatcher.route.should be_nil
|
106
108
|
end
|
107
109
|
|
108
110
|
it "returns nil if route found but method is not allowed" do
|
109
111
|
@request.stub(:path_info => '/foo', :request_method => 'GET')
|
110
|
-
@dispatcher.
|
112
|
+
@dispatcher.route.should be_nil
|
111
113
|
end
|
112
114
|
|
113
115
|
it "assigns named matches to params hash" do
|
114
116
|
@request.stub(:path_info => '/this/some-thing_or-other/is/wicked', :request_method => 'GET')
|
115
|
-
@dispatcher.
|
117
|
+
@dispatcher.route.inspect.should == {
|
116
118
|
:controller => 'awesome',
|
117
119
|
:action => 'rad',
|
118
120
|
:regex => /^this\/(?<path>[^\/]+)\/is\/(?<very_awesome>[^\/]+)$/,
|
@@ -2,5 +2,8 @@ Fixture::Application.set_routes do
|
|
2
2
|
match 'foo/:id/bar', :to => 'foo#bar', :methods => [:get, :post]
|
3
3
|
match '/foo', :to => 'monkeys', :methods => [:put]
|
4
4
|
match 'this/:path/is/:very_awesome', :to => 'awesome#rad'
|
5
|
+
scope :module => 'goose' do
|
6
|
+
match 'lumpies/:lump', :to => 'lumpies#show', :methods => [:get]
|
7
|
+
end
|
5
8
|
match '/', :to => 'root#vegetable', :methods => [:get]
|
6
9
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start
|
3
|
+
|
1
4
|
ENV['RORY_STAGE'] = 'test'
|
2
5
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
6
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
@@ -12,7 +15,7 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
|
12
15
|
require_relative 'fixture_app/config/application'
|
13
16
|
Fixture::Application.root = File.join(File.dirname(__FILE__), 'fixture_app')
|
14
17
|
|
15
|
-
Fixture::Application.
|
18
|
+
Fixture::Application.require_all_files
|
16
19
|
|
17
20
|
RSpec.configure do |config|
|
18
21
|
|
data/spec/support_spec.rb
CHANGED
@@ -7,21 +7,13 @@ describe Rory::Support do
|
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
-
describe '.
|
11
|
-
it '
|
12
|
-
path = '/fake_root/gas/is/cheap/in/good_old_america.rb'
|
13
|
-
Object.should_receive(:autoload).with(:GoodOldAmerica, path)
|
14
|
-
Rory::Support.autoload_file(path)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
describe '.autoload_all_files_in_directory' do
|
19
|
-
it 'autoloads all files from given path' do
|
10
|
+
describe '.require_all_files_in_directory' do
|
11
|
+
it 'requires all files from given path' do
|
20
12
|
Dir.stub(:[]).with(Pathname.new('spinach').join('**', '*.rb')).
|
21
13
|
and_return(["pumpkins", "some_guy_dressed_as_liberace"])
|
22
|
-
Rory::Support.should_receive(:
|
23
|
-
Rory::Support.should_receive(:
|
24
|
-
Rory::Support.
|
14
|
+
Rory::Support.should_receive(:require).with("pumpkins")
|
15
|
+
Rory::Support.should_receive(:require).with("some_guy_dressed_as_liberace")
|
16
|
+
Rory::Support.require_all_files_in_directory('spinach')
|
25
17
|
end
|
26
18
|
end
|
27
19
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-03-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
@@ -123,6 +123,22 @@ dependencies:
|
|
123
123
|
- - ! '>='
|
124
124
|
- !ruby/object:Gem::Version
|
125
125
|
version: '0'
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: simplecov
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ! '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
type: :development
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ! '>='
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
126
142
|
- !ruby/object:Gem::Dependency
|
127
143
|
name: bundler
|
128
144
|
requirement: !ruby/object:Gem::Requirement
|
@@ -172,6 +188,7 @@ files:
|
|
172
188
|
- spec/dispatcher_spec.rb
|
173
189
|
- spec/fixture_app/config/application.rb
|
174
190
|
- spec/fixture_app/config/routes.rb
|
191
|
+
- spec/fixture_app/controllers/goose/lumpies_controller.rb
|
175
192
|
- spec/fixture_app/controllers/stub_controller.rb
|
176
193
|
- spec/fixture_app/views/layouts/surround.html.erb
|
177
194
|
- spec/fixture_app/views/test/dynamic.html.erb
|