sinatra-controllers 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +26 -0
- data/README.rdoc +60 -0
- data/Rakefile +7 -0
- data/lib/sinatra-controllers.rb +61 -0
- data/lib/sinatra-controllers/version.rb +5 -0
- data/sinatra-controllers.gemspec +26 -0
- data/test/controller_test.rb +129 -0
- data/test/fixtures/sinatra_application.rb +86 -0
- data/test/fixtures/views/foo.haml +9 -0
- data/test/fixtures/views/fringe.haml +2 -0
- data/test/watchr.rb +4 -0
- metadata +140 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
sinatra-controllers (0.0.3)
|
5
|
+
sinatra (>= 1.1.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
minitest (2.0.2)
|
11
|
+
rack (1.2.2)
|
12
|
+
sinatra (1.2.1)
|
13
|
+
rack (~> 1.1)
|
14
|
+
tilt (>= 1.2.2, < 2.0)
|
15
|
+
tilt (1.2.2)
|
16
|
+
watchr (0.7)
|
17
|
+
|
18
|
+
PLATFORMS
|
19
|
+
ruby
|
20
|
+
|
21
|
+
DEPENDENCIES
|
22
|
+
bundler (>= 1.0.0)
|
23
|
+
minitest (~> 2.0.0)
|
24
|
+
sinatra (>= 1.1.0)
|
25
|
+
sinatra-controllers!
|
26
|
+
watchr
|
data/README.rdoc
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
= Sinatra-controller
|
2
|
+
|
3
|
+
Sinatra routing done differently. Allows for clean organization across
|
4
|
+
multiple sinatra routes files.
|
5
|
+
|
6
|
+
NOTE: this has only been tested with ruby 1.9.2
|
7
|
+
|
8
|
+
|
9
|
+
== Examples
|
10
|
+
Also see the fixtures folders for what's used in the tests.
|
11
|
+
|
12
|
+
views/index.haml
|
13
|
+
<!DOCTYPE HTML>
|
14
|
+
%html
|
15
|
+
%head
|
16
|
+
%meta(http-equiv="content-type" content="text/html; charset=utf-8")
|
17
|
+
|
18
|
+
%title test page
|
19
|
+
%body
|
20
|
+
fly away with sinatra controllers
|
21
|
+
|
22
|
+
You can divide your routes cleanly between as many classes as you please. At
|
23
|
+
any time, you can also simply revert back to the plain vanilla sinatra syntax
|
24
|
+
on use get at the top level.
|
25
|
+
|
26
|
+
sinatra\_controller.rb
|
27
|
+
#!/usr/bin/env ruby
|
28
|
+
|
29
|
+
require 'sinatra'
|
30
|
+
require 'sinatra-controllers'
|
31
|
+
|
32
|
+
class Welcome < Sinatra::Controller
|
33
|
+
def index
|
34
|
+
haml :index
|
35
|
+
end
|
36
|
+
|
37
|
+
# this create a route corresponding to `get '/welcome/about'`
|
38
|
+
# making it a shortcut for common routes. It doesn't handle arguments
|
39
|
+
# unlike the block syntax.
|
40
|
+
def get_about
|
41
|
+
'making controllers in sinatra easy'
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
Sinatra::Controllers.register Welcome do
|
46
|
+
get '/', :index
|
47
|
+
end
|
48
|
+
|
49
|
+
This is a trick for common requests that don't require arguments. It creates a
|
50
|
+
route corresponding to `get '/welcome/about'` in the flavor of Test::Unit where
|
51
|
+
it uses the prefix before the underscore for the route type. The block to
|
52
|
+
register is optional here.
|
53
|
+
|
54
|
+
class Welcome < Sinatra::Controller
|
55
|
+
def get_about
|
56
|
+
'making controllers in sinatra easy'
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
module Sinatra
|
2
|
+
class Controller
|
3
|
+
attr_accessor :params, :request, :template_cache
|
4
|
+
include Sinatra::Templates
|
5
|
+
def initialize(params,request)
|
6
|
+
@params = params
|
7
|
+
@request = request
|
8
|
+
@template_cache = Tilt::Cache.new rescue 'sinatra < 1.0'
|
9
|
+
@templates = {}
|
10
|
+
end
|
11
|
+
class << self
|
12
|
+
def views
|
13
|
+
end
|
14
|
+
def templates
|
15
|
+
{}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
module Controllers
|
20
|
+
class Mapping
|
21
|
+
attr_accessor :klass, :opts
|
22
|
+
|
23
|
+
def initialize(klass,opts={})
|
24
|
+
@klass = klass
|
25
|
+
@opts = opts
|
26
|
+
end
|
27
|
+
|
28
|
+
def route(verb, path, action, opts={})
|
29
|
+
aklass = klass # need this so it will stay in scope for closure
|
30
|
+
block = proc { aklass.new(params, request).send action }
|
31
|
+
Sinatra::Application.send verb, path, &block
|
32
|
+
end
|
33
|
+
|
34
|
+
def parse
|
35
|
+
klass.instance_methods.each do |meth|
|
36
|
+
next unless meth =~ /^(get|post|delete|put)_(.*)$/
|
37
|
+
route $1,"/#{klass.to_s.downcase}/#{$2.downcase}", meth
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# why are these even defined?
|
42
|
+
undef_method :get, :post, :put, :delete
|
43
|
+
def method_missing(method,*a)
|
44
|
+
case method.to_s
|
45
|
+
when /(get|post|delete|put)/
|
46
|
+
route(method, *a)
|
47
|
+
else
|
48
|
+
super
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class << self
|
54
|
+
def register(klass, opts={}, &block)
|
55
|
+
map = Mapping.new(klass,opts)
|
56
|
+
map.instance_eval(&block) if block
|
57
|
+
map.parse
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path("../lib/sinatra-controllers/version", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "sinatra-controllers"
|
6
|
+
s.version = Sinatra::Controllers::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ['Daniel Bretoi']
|
9
|
+
s.email = []
|
10
|
+
s.homepage = "http://rubygems.org/gems/sinatra-controllers"
|
11
|
+
s.summary = "Adds controllers to Sinatra"
|
12
|
+
s.description = "Adds controllers to Sinatra"
|
13
|
+
|
14
|
+
s.required_rubygems_version = ">= 1.3.6"
|
15
|
+
s.rubyforge_project = "sinatra-controllers"
|
16
|
+
|
17
|
+
s.add_dependency "sinatra", ">= 1.1.0"
|
18
|
+
s.add_development_dependency "bundler", ">= 1.0.0"
|
19
|
+
s.add_development_dependency "minitest", "~> 2.0.0"
|
20
|
+
s.add_development_dependency "watchr"
|
21
|
+
|
22
|
+
s.files = `git ls-files`.split("\n")
|
23
|
+
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
|
24
|
+
s.require_path = 'lib'
|
25
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
26
|
+
end
|
@@ -0,0 +1,129 @@
|
|
1
|
+
require 'rack/test'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require 'ap'
|
5
|
+
require './test/fixtures/sinatra_application'
|
6
|
+
|
7
|
+
class Pride
|
8
|
+
def print o
|
9
|
+
case o
|
10
|
+
when /0 errors/i
|
11
|
+
Kernel.print o.green
|
12
|
+
when /(e|f)/i
|
13
|
+
Kernel.print o.red
|
14
|
+
else
|
15
|
+
Kernel.print o.green
|
16
|
+
end
|
17
|
+
end
|
18
|
+
def puts o = ''
|
19
|
+
if o=~ /([1-9]+ failures)/
|
20
|
+
o.sub!(/([1-9]+ failures)/, '\1'.red)
|
21
|
+
end
|
22
|
+
Kernel.puts o
|
23
|
+
end
|
24
|
+
end
|
25
|
+
MiniTest::Unit.output = Pride.new
|
26
|
+
|
27
|
+
class ClassicMappingTest < MiniTest::Unit::TestCase
|
28
|
+
include Rack::Test::Methods
|
29
|
+
|
30
|
+
def app
|
31
|
+
@app = Sinatra::Application
|
32
|
+
@app.set :environment, :test
|
33
|
+
@app
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_render
|
37
|
+
get '/'
|
38
|
+
assert_equal 200, last_response.status
|
39
|
+
assert_equal true, (last_response.body =~ /message/) != nil
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_fringe
|
43
|
+
get '/fringe'
|
44
|
+
assert_equal 200, last_response.status
|
45
|
+
assert_equal true, (last_response.body =~ /bishop/) != nil
|
46
|
+
assert_equal true, (last_response.body =~ /Anna Torv/) != nil
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_param_pass
|
50
|
+
get '/test/232'
|
51
|
+
assert_equal 200, last_response.status
|
52
|
+
assert_equal true, (last_response.body =~ /232/) != nil
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_help_route
|
56
|
+
get '/help'
|
57
|
+
assert_equal 200, last_response.status
|
58
|
+
end
|
59
|
+
def test_help_index
|
60
|
+
get '/help/index'
|
61
|
+
assert_equal 200, last_response.status
|
62
|
+
assert_equal true, (last_response.body =~ /help found/) != nil
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_regular
|
66
|
+
get '/regular'
|
67
|
+
assert_equal 200, last_response.status
|
68
|
+
assert_equal true, (last_response.body =~ /flames/) != nil
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_all
|
72
|
+
delete '/all'
|
73
|
+
assert_equal 200, last_response.status
|
74
|
+
assert_equal true, (last_response.body =~ /wiped/) != nil
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_flame
|
78
|
+
put '/flame'
|
79
|
+
assert_equal 200, last_response.status
|
80
|
+
assert_equal true, (last_response.body =~ /heat/) != nil
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_request
|
84
|
+
get '/request'
|
85
|
+
assert_equal 200, last_response.status
|
86
|
+
assert_equal true, (last_response.body =~ /SCRIPT_NAME/) != nil
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_post
|
90
|
+
post '/racket'
|
91
|
+
assert_equal 200, last_response.status
|
92
|
+
assert_equal true, (last_response.body =~ /posted/) != nil
|
93
|
+
end
|
94
|
+
def test_arguments
|
95
|
+
get '/take/123'
|
96
|
+
assert_equal 200, last_response.status
|
97
|
+
assert_equal true, (last_response.body =~ /123/) != nil
|
98
|
+
end
|
99
|
+
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
|
@@ -0,0 +1,86 @@
|
|
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
|
data/test/watchr.rb
ADDED
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-controllers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Daniel Bretoi
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-04-02 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: sinatra
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 1
|
30
|
+
- 1
|
31
|
+
- 0
|
32
|
+
version: 1.1.0
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: bundler
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
segments:
|
44
|
+
- 1
|
45
|
+
- 0
|
46
|
+
- 0
|
47
|
+
version: 1.0.0
|
48
|
+
type: :development
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: minitest
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ~>
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
segments:
|
59
|
+
- 2
|
60
|
+
- 0
|
61
|
+
- 0
|
62
|
+
version: 2.0.0
|
63
|
+
type: :development
|
64
|
+
version_requirements: *id003
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: watchr
|
67
|
+
prerelease: false
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
version: "0"
|
76
|
+
type: :development
|
77
|
+
version_requirements: *id004
|
78
|
+
description: Adds controllers to Sinatra
|
79
|
+
email: []
|
80
|
+
|
81
|
+
executables: []
|
82
|
+
|
83
|
+
extensions: []
|
84
|
+
|
85
|
+
extra_rdoc_files: []
|
86
|
+
|
87
|
+
files:
|
88
|
+
- .gitignore
|
89
|
+
- Gemfile
|
90
|
+
- Gemfile.lock
|
91
|
+
- README.rdoc
|
92
|
+
- Rakefile
|
93
|
+
- lib/sinatra-controllers.rb
|
94
|
+
- lib/sinatra-controllers/version.rb
|
95
|
+
- sinatra-controllers.gemspec
|
96
|
+
- test/controller_test.rb
|
97
|
+
- test/fixtures/sinatra_application.rb
|
98
|
+
- test/fixtures/views/foo.haml
|
99
|
+
- test/fixtures/views/fringe.haml
|
100
|
+
- test/watchr.rb
|
101
|
+
has_rdoc: true
|
102
|
+
homepage: http://rubygems.org/gems/sinatra-controllers
|
103
|
+
licenses: []
|
104
|
+
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
segments:
|
116
|
+
- 0
|
117
|
+
version: "0"
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
none: false
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
segments:
|
124
|
+
- 1
|
125
|
+
- 3
|
126
|
+
- 6
|
127
|
+
version: 1.3.6
|
128
|
+
requirements: []
|
129
|
+
|
130
|
+
rubyforge_project: sinatra-controllers
|
131
|
+
rubygems_version: 1.3.7
|
132
|
+
signing_key:
|
133
|
+
specification_version: 3
|
134
|
+
summary: Adds controllers to Sinatra
|
135
|
+
test_files:
|
136
|
+
- test/controller_test.rb
|
137
|
+
- test/fixtures/sinatra_application.rb
|
138
|
+
- test/fixtures/views/foo.haml
|
139
|
+
- test/fixtures/views/fringe.haml
|
140
|
+
- test/watchr.rb
|