sinatra-controllers 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +1 -0
- data/Gemfile.lock +8 -1
- data/README.rdoc +24 -0
- data/lib/sinatra-controllers.rb +9 -3
- data/lib/sinatra-controllers/version.rb +1 -1
- data/sinatra-controllers.gemspec +4 -0
- data/test/controller_test.rb +63 -53
- data/test/helper.rb +29 -0
- data/test/no_block_test.rb +47 -0
- data/test/path_defined_test.rb +47 -0
- data/test/{fixtures/views → views}/foo.haml +0 -0
- data/test/{fixtures/views → views}/fringe.haml +0 -0
- metadata +55 -9
- data/test/fixtures/sinatra_application.rb +0 -86
data/CHANGELOG
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
* 0.2.0 add scoping
|
data/Gemfile.lock
CHANGED
@@ -1,14 +1,18 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
sinatra-controllers (0.
|
4
|
+
sinatra-controllers (0.2.0)
|
5
5
|
sinatra (>= 1.0.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: http://rubygems.org/
|
9
9
|
specs:
|
10
|
+
awesome_print (0.3.1)
|
11
|
+
haml (3.0.25)
|
10
12
|
minitest (2.0.2)
|
11
13
|
rack (1.2.2)
|
14
|
+
rack-test (0.5.7)
|
15
|
+
rack (>= 1.0)
|
12
16
|
sinatra (1.2.1)
|
13
17
|
rack (~> 1.1)
|
14
18
|
tilt (>= 1.2.2, < 2.0)
|
@@ -19,8 +23,11 @@ PLATFORMS
|
|
19
23
|
ruby
|
20
24
|
|
21
25
|
DEPENDENCIES
|
26
|
+
awesome_print
|
22
27
|
bundler (>= 1.0.0)
|
28
|
+
haml
|
23
29
|
minitest (~> 2.0.0)
|
30
|
+
rack-test (~> 0.5.7)
|
24
31
|
sinatra (>= 1.0.0)
|
25
32
|
sinatra-controllers!
|
26
33
|
watchr
|
data/README.rdoc
CHANGED
@@ -57,4 +57,28 @@ register is optional here.
|
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
|
+
You can also use scopes for your own defined path, either because you're
|
61
|
+
unhappy with your classname, or because you just want to:
|
60
62
|
|
63
|
+
class Fringe < Sinatra::Controller
|
64
|
+
def get_walternet
|
65
|
+
'scopes'
|
66
|
+
end
|
67
|
+
def peter
|
68
|
+
'more scopes'
|
69
|
+
end
|
70
|
+
def index
|
71
|
+
'index still if you want'
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
Sinatra::Controllers.register(Fringe, :scope => 'fringe') do
|
76
|
+
get 'peter', :peter
|
77
|
+
get '/', :index
|
78
|
+
end
|
79
|
+
|
80
|
+
this enables the following routes
|
81
|
+
|
82
|
+
get '/fringe/walternet'
|
83
|
+
get '/fringe/peter'
|
84
|
+
get '/'
|
data/lib/sinatra-controllers.rb
CHANGED
@@ -25,16 +25,22 @@ module Sinatra
|
|
25
25
|
@opts = opts
|
26
26
|
end
|
27
27
|
|
28
|
-
def route(verb, path, action
|
28
|
+
def route(verb, path, action)
|
29
29
|
aklass = klass # need this so it will stay in scope for closure
|
30
30
|
block = proc { aklass.new(params, request).send action }
|
31
|
+
if path[0] != '/'
|
32
|
+
path = '/' + File.join(opts[:scope], path)
|
33
|
+
end
|
31
34
|
Sinatra::Application.send verb, path, &block
|
32
35
|
end
|
33
36
|
|
34
37
|
def parse
|
35
38
|
klass.instance_methods.each do |meth|
|
36
|
-
|
37
|
-
|
39
|
+
verb,name = meth.to_s.scan(/^(get|post|delete|put)_(.*)$/).flatten
|
40
|
+
next unless verb
|
41
|
+
scope = '/' + (opts[:scope] || klass.to_s.downcase)
|
42
|
+
scope.gsub!(%r{^/+},'/')
|
43
|
+
route verb,"#{scope}/#{name.downcase}", meth
|
38
44
|
end
|
39
45
|
end
|
40
46
|
|
data/sinatra-controllers.gemspec
CHANGED
@@ -18,6 +18,10 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.add_development_dependency "bundler", ">= 1.0.0"
|
19
19
|
s.add_development_dependency "minitest", "~> 2.0.0"
|
20
20
|
s.add_development_dependency "watchr"
|
21
|
+
s.add_development_dependency "rack-test", "~> 0.5.7"
|
22
|
+
s.add_development_dependency "awesome_print"
|
23
|
+
s.add_development_dependency "haml"
|
24
|
+
|
21
25
|
|
22
26
|
s.files = `git ls-files`.split("\n")
|
23
27
|
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
|
data/test/controller_test.rb
CHANGED
@@ -1,28 +1,68 @@
|
|
1
|
-
require '
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
def
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
1
|
+
require './test/helper'
|
2
|
+
|
3
|
+
class Blah < Sinatra::Controller
|
4
|
+
def help
|
5
|
+
@blah = 'message' + params.inspect
|
6
|
+
haml :foo
|
7
|
+
end
|
8
|
+
def test
|
9
|
+
params.inspect
|
10
|
+
end
|
11
|
+
|
12
|
+
def flames
|
13
|
+
'heat'
|
14
|
+
end
|
15
|
+
|
16
|
+
def requested
|
17
|
+
request.inspect
|
18
|
+
end
|
19
|
+
|
20
|
+
def racket
|
21
|
+
'posted'
|
22
|
+
end
|
23
|
+
|
24
|
+
def everything
|
25
|
+
'wiped'
|
26
|
+
end
|
27
|
+
def fringe
|
28
|
+
@fringe = "Anna Torv"
|
29
|
+
haml :fringe
|
30
|
+
end
|
31
|
+
def take
|
32
|
+
params[:arg]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class Help < Sinatra::Controller
|
37
|
+
def help
|
38
|
+
'another help'
|
39
|
+
end
|
40
|
+
def get_index
|
41
|
+
'help found'
|
23
42
|
end
|
24
43
|
end
|
25
|
-
|
44
|
+
|
45
|
+
Sinatra::Controllers.register(Help) do
|
46
|
+
get '/help', :help
|
47
|
+
end
|
48
|
+
|
49
|
+
Sinatra::Controllers.register(Blah) do
|
50
|
+
get '/request', :requested
|
51
|
+
get '/', :help
|
52
|
+
get '/test/:id', :test
|
53
|
+
post '/racket', :racket
|
54
|
+
put '/flame', :flames
|
55
|
+
delete '/all', :everything
|
56
|
+
get '/fringe', :fringe
|
57
|
+
get '/take/:arg', :take
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
# regular path should work too
|
63
|
+
get '/regular' do
|
64
|
+
'flames'
|
65
|
+
end
|
26
66
|
|
27
67
|
class ClassicMappingTest < MiniTest::Unit::TestCase
|
28
68
|
include Rack::Test::Methods
|
@@ -97,33 +137,3 @@ class ClassicMappingTest < MiniTest::Unit::TestCase
|
|
97
137
|
assert_equal true, (last_response.body =~ /123/) != nil
|
98
138
|
end
|
99
139
|
end
|
100
|
-
|
101
|
-
require 'minitest/spec'
|
102
|
-
describe "without defined route block" do
|
103
|
-
include Rack::Test::Methods
|
104
|
-
def app
|
105
|
-
@app = Sinatra::Application
|
106
|
-
@app.set :environment, :test
|
107
|
-
@app
|
108
|
-
end
|
109
|
-
it "should respond to get" do
|
110
|
-
get '/welcome/index'
|
111
|
-
last_response.status.must_equal 200
|
112
|
-
last_response.body.must_match /route test/
|
113
|
-
end
|
114
|
-
it "should respond to put" do
|
115
|
-
put '/welcome/update'
|
116
|
-
last_response.status.must_equal 200
|
117
|
-
last_response.body.must_match /walternet/
|
118
|
-
end
|
119
|
-
it "should respond to post" do
|
120
|
-
post '/welcome/peter'
|
121
|
-
last_response.status.must_equal 200
|
122
|
-
last_response.body.must_match /amber/
|
123
|
-
end
|
124
|
-
it "should respond to delete" do
|
125
|
-
delete '/welcome/universe'
|
126
|
-
last_response.status.must_equal 200
|
127
|
-
last_response.body.must_match /destroy/
|
128
|
-
end
|
129
|
-
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'rack/test'
|
4
|
+
require 'test/unit'
|
5
|
+
require 'minitest/autorun'
|
6
|
+
require 'awesome_print'
|
7
|
+
require 'sinatra'
|
8
|
+
require 'sinatra-controllers'
|
9
|
+
|
10
|
+
class Pride
|
11
|
+
def print o
|
12
|
+
case o
|
13
|
+
when /0 errors/i
|
14
|
+
Kernel.print o.green
|
15
|
+
when /(e|f)/i
|
16
|
+
Kernel.print o.red
|
17
|
+
else
|
18
|
+
Kernel.print o.green
|
19
|
+
end
|
20
|
+
end
|
21
|
+
def puts o = ''
|
22
|
+
if o=~ /([1-9]+ failures)/
|
23
|
+
o.sub!(/([1-9]+ failures)/, '\1'.red)
|
24
|
+
end
|
25
|
+
Kernel.puts o
|
26
|
+
end
|
27
|
+
end
|
28
|
+
MiniTest::Unit.output = Pride.new
|
29
|
+
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require './test/helper'
|
2
|
+
require 'minitest/spec'
|
3
|
+
|
4
|
+
class Welcome < Sinatra::Controller
|
5
|
+
def get_index
|
6
|
+
'route test'
|
7
|
+
end
|
8
|
+
def put_update
|
9
|
+
'walternet'
|
10
|
+
end
|
11
|
+
def post_peter
|
12
|
+
'amber'
|
13
|
+
end
|
14
|
+
def delete_universe
|
15
|
+
'destroy'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
Sinatra::Controllers.register(Welcome)
|
19
|
+
|
20
|
+
describe "without defined route block" do
|
21
|
+
include Rack::Test::Methods
|
22
|
+
def app
|
23
|
+
@app = Sinatra::Application
|
24
|
+
@app.set :environment, :test
|
25
|
+
@app
|
26
|
+
end
|
27
|
+
it "should respond to get" do
|
28
|
+
get '/welcome/index'
|
29
|
+
last_response.status.must_equal 200
|
30
|
+
last_response.body.must_match /route test/
|
31
|
+
end
|
32
|
+
it "should respond to put" do
|
33
|
+
put '/welcome/update'
|
34
|
+
last_response.status.must_equal 200
|
35
|
+
last_response.body.must_match /walternet/
|
36
|
+
end
|
37
|
+
it "should respond to post" do
|
38
|
+
post '/welcome/peter'
|
39
|
+
last_response.status.must_equal 200
|
40
|
+
last_response.body.must_match /amber/
|
41
|
+
end
|
42
|
+
it "should respond to delete" do
|
43
|
+
delete '/welcome/universe'
|
44
|
+
last_response.status.must_equal 200
|
45
|
+
last_response.body.must_match /destroy/
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require './test/helper'
|
2
|
+
require 'minitest/spec'
|
3
|
+
|
4
|
+
class Show70 < Sinatra::Controller
|
5
|
+
def get_kelso
|
6
|
+
'michael'
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class Seinfeld < Sinatra::Controller
|
11
|
+
def get_cosmo
|
12
|
+
'kramer'
|
13
|
+
end
|
14
|
+
def george
|
15
|
+
'bald'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
Sinatra::Controllers.register(Show70, :scope => '/70s_show')
|
20
|
+
# make sure we don't need leading slash
|
21
|
+
Sinatra::Controllers.register(Seinfeld, :scope => 'seinfeld') do
|
22
|
+
get 'costanza', :george
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "with defined scope" do
|
26
|
+
include Rack::Test::Methods
|
27
|
+
def app
|
28
|
+
@app = Sinatra::Application
|
29
|
+
@app.set :environment, :test
|
30
|
+
@app
|
31
|
+
end
|
32
|
+
it "should use correct scope" do
|
33
|
+
get '/70s_show/kelso'
|
34
|
+
last_response.status.must_equal 200
|
35
|
+
last_response.body.must_equal 'michael'
|
36
|
+
end
|
37
|
+
it "should use correct scope without leading slash using no block" do
|
38
|
+
get '/seinfeld/cosmo'
|
39
|
+
last_response.status.must_equal 200
|
40
|
+
last_response.body.must_equal 'kramer'
|
41
|
+
end
|
42
|
+
it "should use correct scope without leading slash for block" do
|
43
|
+
get '/seinfeld/costanza'
|
44
|
+
last_response.status.must_equal 200
|
45
|
+
last_response.body.must_equal 'bald'
|
46
|
+
end
|
47
|
+
end
|
File without changes
|
File without changes
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
- 1
|
8
7
|
- 2
|
9
|
-
|
8
|
+
- 0
|
9
|
+
version: 0.2.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Daniel Bretoi
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-04-
|
17
|
+
date: 2011-04-07 00:00:00 -07:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -75,6 +75,47 @@ dependencies:
|
|
75
75
|
version: "0"
|
76
76
|
type: :development
|
77
77
|
version_requirements: *id004
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rack-test
|
80
|
+
prerelease: false
|
81
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ~>
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
- 5
|
89
|
+
- 7
|
90
|
+
version: 0.5.7
|
91
|
+
type: :development
|
92
|
+
version_requirements: *id005
|
93
|
+
- !ruby/object:Gem::Dependency
|
94
|
+
name: awesome_print
|
95
|
+
prerelease: false
|
96
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
segments:
|
102
|
+
- 0
|
103
|
+
version: "0"
|
104
|
+
type: :development
|
105
|
+
version_requirements: *id006
|
106
|
+
- !ruby/object:Gem::Dependency
|
107
|
+
name: haml
|
108
|
+
prerelease: false
|
109
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
segments:
|
115
|
+
- 0
|
116
|
+
version: "0"
|
117
|
+
type: :development
|
118
|
+
version_requirements: *id007
|
78
119
|
description: Adds controllers to Sinatra
|
79
120
|
email: []
|
80
121
|
|
@@ -86,6 +127,7 @@ extra_rdoc_files: []
|
|
86
127
|
|
87
128
|
files:
|
88
129
|
- .gitignore
|
130
|
+
- CHANGELOG
|
89
131
|
- Gemfile
|
90
132
|
- Gemfile.lock
|
91
133
|
- README.rdoc
|
@@ -94,9 +136,11 @@ files:
|
|
94
136
|
- lib/sinatra-controllers/version.rb
|
95
137
|
- sinatra-controllers.gemspec
|
96
138
|
- test/controller_test.rb
|
97
|
-
- test/
|
98
|
-
- test/
|
99
|
-
- test/
|
139
|
+
- test/helper.rb
|
140
|
+
- test/no_block_test.rb
|
141
|
+
- test/path_defined_test.rb
|
142
|
+
- test/views/foo.haml
|
143
|
+
- test/views/fringe.haml
|
100
144
|
- test/watchr.rb
|
101
145
|
has_rdoc: true
|
102
146
|
homepage: https://github.com/danielb2/sinatra-controllers
|
@@ -134,7 +178,9 @@ specification_version: 3
|
|
134
178
|
summary: Adds controllers to Sinatra
|
135
179
|
test_files:
|
136
180
|
- test/controller_test.rb
|
137
|
-
- test/
|
138
|
-
- test/
|
139
|
-
- test/
|
181
|
+
- test/helper.rb
|
182
|
+
- test/no_block_test.rb
|
183
|
+
- test/path_defined_test.rb
|
184
|
+
- test/views/foo.haml
|
185
|
+
- test/views/fringe.haml
|
140
186
|
- test/watchr.rb
|
@@ -1,86 +0,0 @@
|
|
1
|
-
require 'sinatra'
|
2
|
-
class Sinatra::Application
|
3
|
-
def env
|
4
|
-
@env.update('SCRIPT_NAME' => '/test')
|
5
|
-
end
|
6
|
-
end
|
7
|
-
require 'sinatra-controllers'
|
8
|
-
|
9
|
-
class Blah < Sinatra::Controller
|
10
|
-
def help
|
11
|
-
@blah = 'message' + params.inspect
|
12
|
-
haml :foo
|
13
|
-
end
|
14
|
-
def test
|
15
|
-
params.inspect
|
16
|
-
end
|
17
|
-
|
18
|
-
def flames
|
19
|
-
'heat'
|
20
|
-
end
|
21
|
-
|
22
|
-
def requested
|
23
|
-
request.inspect
|
24
|
-
end
|
25
|
-
|
26
|
-
def racket
|
27
|
-
'posted'
|
28
|
-
end
|
29
|
-
|
30
|
-
def everything
|
31
|
-
'wiped'
|
32
|
-
end
|
33
|
-
def fringe
|
34
|
-
@fringe = "Anna Torv"
|
35
|
-
haml :fringe
|
36
|
-
end
|
37
|
-
def take
|
38
|
-
params[:arg]
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
class Help < Sinatra::Controller
|
43
|
-
def help
|
44
|
-
'another help'
|
45
|
-
end
|
46
|
-
def get_index
|
47
|
-
'help found'
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
class Welcome < Sinatra::Controller
|
52
|
-
def get_index
|
53
|
-
'route test'
|
54
|
-
end
|
55
|
-
def put_update
|
56
|
-
'walternet'
|
57
|
-
end
|
58
|
-
def post_peter
|
59
|
-
'amber'
|
60
|
-
end
|
61
|
-
def delete_universe
|
62
|
-
'destroy'
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
Sinatra::Controllers.register(Welcome)
|
67
|
-
|
68
|
-
Sinatra::Controllers.register(Help) do
|
69
|
-
get '/help', :help
|
70
|
-
end
|
71
|
-
|
72
|
-
Sinatra::Controllers.register(Blah) do
|
73
|
-
get '/request', :requested
|
74
|
-
get '/', :help
|
75
|
-
get '/test/:id', :test
|
76
|
-
post '/racket', :racket
|
77
|
-
put '/flame', :flames
|
78
|
-
delete '/all', :everything
|
79
|
-
get '/fringe', :fringe
|
80
|
-
get '/take/:arg', :take
|
81
|
-
end
|
82
|
-
|
83
|
-
# regular paths should work too
|
84
|
-
get '/regular' do
|
85
|
-
'flames'
|
86
|
-
end
|