padrino-views 0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +19 -0
- data/README.md +66 -0
- data/Rakefile +11 -0
- data/lib/padrino/views.rb +34 -0
- data/lib/sinatra/views.rb +38 -0
- data/padrino-views.gemspec +19 -0
- data/test/padrino/views_test.rb +69 -0
- data/test/sinatra/views_test.rb +88 -0
- data/test/test_helper.rb +30 -0
- metadata +82 -0
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2010 Florian Gilcher <florian.gilcher@asquera.de>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
Views for Sinatra and Padrino
|
2
|
+
=============================
|
3
|
+
|
4
|
+
sinatra-views provides a view implementation inspired by Agavi to Sinatra and Padrino. This saves the hassle of implementing special functionality based on content types on your own.
|
5
|
+
|
6
|
+
Example
|
7
|
+
-------
|
8
|
+
|
9
|
+
class MyApp < Sinatra::Base
|
10
|
+
register Sinatra::Views
|
11
|
+
|
12
|
+
get '/' do
|
13
|
+
#do something and retrieve @data
|
14
|
+
view :success
|
15
|
+
end
|
16
|
+
|
17
|
+
error NotFoundError do
|
18
|
+
view :error
|
19
|
+
end
|
20
|
+
|
21
|
+
view :success do
|
22
|
+
def html
|
23
|
+
render 'template'
|
24
|
+
end
|
25
|
+
|
26
|
+
def xml
|
27
|
+
@data.to_xml
|
28
|
+
end
|
29
|
+
|
30
|
+
def json
|
31
|
+
@data.to_json
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
view :error do
|
36
|
+
def html
|
37
|
+
status 404
|
38
|
+
render 'error_template'
|
39
|
+
end
|
40
|
+
|
41
|
+
def xml
|
42
|
+
status 404
|
43
|
+
end
|
44
|
+
|
45
|
+
def json
|
46
|
+
status 404
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
View names can be Arrays, so namespacing views is easy:
|
52
|
+
|
53
|
+
class MyApp
|
54
|
+
view :viewA, :success do
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
view :viewB, :success do
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
Padrino
|
64
|
+
-------
|
65
|
+
|
66
|
+
Sinatra::Views is also available as a special extension for Padrino. Padrino users are advised to install and require `padrino-views` and then register `Padrino::Views` instead. This provides automatic namespacing by controller. Views defined within a controller will be used when calling `view` inside a controller context. Views defined outside of a controller are global and can be accessed from everywhere, as long as there is no controller view of the same name.
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
desc 'Default task: run all tests'
|
2
|
+
task :default => [:test]
|
3
|
+
|
4
|
+
require 'rake/testtask'
|
5
|
+
Rake::TestTask.new(:test) do |test|
|
6
|
+
test.libs << 'test'
|
7
|
+
test.pattern = 'test/**/*_test.rb'
|
8
|
+
test.verbose = false
|
9
|
+
test.warning = false
|
10
|
+
test.ruby_opts = ['-rubygems', '-rtest_helper']
|
11
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'sinatra/views'
|
2
|
+
require 'padrino-core'
|
3
|
+
require 'pp'
|
4
|
+
|
5
|
+
module Padrino
|
6
|
+
module Views
|
7
|
+
|
8
|
+
def self.registered(app)
|
9
|
+
app.send(:include, Sinatra::Views::InstanceMethods)
|
10
|
+
app.extend(Sinatra::Views::ClassMethods)
|
11
|
+
app.send(:include, InstanceMethods)
|
12
|
+
app.extend(ClassMethods)
|
13
|
+
end
|
14
|
+
|
15
|
+
module InstanceMethods
|
16
|
+
|
17
|
+
def lookup_module(names)
|
18
|
+
controller_names = [request.controller.to_sym] + names
|
19
|
+
settings.view_modules[controller_names] || settings.view_modules[names]
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
module ClassMethods
|
25
|
+
def view(*names, &block)
|
26
|
+
if @_controller
|
27
|
+
names.unshift @_controller.first
|
28
|
+
end
|
29
|
+
super(*names, &block)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'sinatra'
|
2
|
+
|
3
|
+
module Sinatra
|
4
|
+
module Views
|
5
|
+
|
6
|
+
def self.registered(app)
|
7
|
+
app.send(:include, InstanceMethods)
|
8
|
+
app.extend(ClassMethods)
|
9
|
+
end
|
10
|
+
|
11
|
+
module InstanceMethods
|
12
|
+
def lookup_module(names)
|
13
|
+
settings.view_modules[names]
|
14
|
+
end
|
15
|
+
|
16
|
+
def view(*names)
|
17
|
+
opts = names.last.kind_of?(Hash) ? names.pop : {}
|
18
|
+
format = opts[:format] || params[:format]
|
19
|
+
mod = lookup_module(names)
|
20
|
+
extend mod
|
21
|
+
send format
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
module ClassMethods
|
26
|
+
|
27
|
+
def self.extended(app)
|
28
|
+
app.set :view_modules, {}
|
29
|
+
end
|
30
|
+
|
31
|
+
def view(*names, &block)
|
32
|
+
view_modules[names] ||= Module.new
|
33
|
+
view_modules[names].class_eval(&block)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'lib/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "padrino-views"
|
7
|
+
s.version = 0.4
|
8
|
+
s.authors = ["Florian Gilcher"]
|
9
|
+
s.email = "florian.gilcher@asquera.de"
|
10
|
+
s.summary = "Padrino Plugin to provide a View layer to Padrino."
|
11
|
+
s.description = <<-DESC
|
12
|
+
Padriono Plugin to provide a View layer to Padrino.
|
13
|
+
DESC
|
14
|
+
|
15
|
+
s.files = Dir['**/{sinatra/*,sinatra,test_helper.rb}'] + Dir['**/{padrino/*,padrino}'] + %w(README.md padrino-views.gemspec Rakefile LICENSE)
|
16
|
+
s.platform = Gem::Platform::RUBY
|
17
|
+
s.require_path = 'lib'
|
18
|
+
s.add_dependency('padrino', '>= 0.9.14')
|
19
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'padrino/views'
|
2
|
+
|
3
|
+
class PadrinoTest < Padrino::Application
|
4
|
+
register Padrino::Views
|
5
|
+
set :environment, :test
|
6
|
+
|
7
|
+
controller :test do
|
8
|
+
get '/' do
|
9
|
+
view :list, :format => :html
|
10
|
+
end
|
11
|
+
|
12
|
+
get '/foo', :provides => [:html] do
|
13
|
+
view :list
|
14
|
+
end
|
15
|
+
|
16
|
+
get '/test' do
|
17
|
+
view :test, :format => :html
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
controller :test do
|
22
|
+
view :list do
|
23
|
+
def html
|
24
|
+
'test/hoge'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
view :test do
|
30
|
+
def html
|
31
|
+
"test/fuge"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
context "Padrino app class with views extension" do
|
38
|
+
app PadrinoTest
|
39
|
+
|
40
|
+
asserts "the list view" do
|
41
|
+
topic.view_modules[[:test, :list]]
|
42
|
+
end.kind_of(Module)
|
43
|
+
|
44
|
+
asserts "the list views methods" do
|
45
|
+
topic.view_modules[[:test, :list]].instance_methods.map(&:to_s)
|
46
|
+
end.includes("html")
|
47
|
+
end
|
48
|
+
|
49
|
+
context "Full padrino app" do
|
50
|
+
app PadrinoTest
|
51
|
+
|
52
|
+
context "with explicit view format" do
|
53
|
+
setup { get 'test/' }
|
54
|
+
|
55
|
+
asserts(:body).equals('test/hoge')
|
56
|
+
end
|
57
|
+
|
58
|
+
context "with implicit view format" do
|
59
|
+
setup { get 'test/foo.html' }
|
60
|
+
|
61
|
+
asserts(:body).equals('test/hoge')
|
62
|
+
end
|
63
|
+
|
64
|
+
context "with views outside controller" do
|
65
|
+
setup { get 'test/test' }
|
66
|
+
|
67
|
+
asserts(:body).equals('test/fuge')
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'sinatra/views'
|
2
|
+
|
3
|
+
class SinatraTest < Sinatra::Base
|
4
|
+
register Sinatra::Views
|
5
|
+
set :environment, :test
|
6
|
+
|
7
|
+
get '/' do
|
8
|
+
view :list, :format => :html
|
9
|
+
end
|
10
|
+
|
11
|
+
get '/foo.:format' do
|
12
|
+
view :list
|
13
|
+
end
|
14
|
+
|
15
|
+
view :list do
|
16
|
+
def html
|
17
|
+
'test/hoge'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class AmbivalenceTest < Sinatra::Base
|
23
|
+
register Sinatra::Views
|
24
|
+
set :environment, :test
|
25
|
+
|
26
|
+
get '/' do
|
27
|
+
view :list, :format => :html
|
28
|
+
end
|
29
|
+
|
30
|
+
get '/foo.:format' do
|
31
|
+
view :list
|
32
|
+
end
|
33
|
+
|
34
|
+
def html
|
35
|
+
'test/fuge'
|
36
|
+
end
|
37
|
+
|
38
|
+
view :list do
|
39
|
+
def html
|
40
|
+
'test/hoge'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "Sinatra app class with views extension" do
|
46
|
+
app SinatraTest
|
47
|
+
|
48
|
+
asserts "the list view" do
|
49
|
+
topic.view_modules[[:list]]
|
50
|
+
end.kind_of(Module)
|
51
|
+
|
52
|
+
asserts "the list views methods" do
|
53
|
+
topic.view_modules[[:list]].instance_methods.map(&:to_s)
|
54
|
+
end.includes("html")
|
55
|
+
end
|
56
|
+
|
57
|
+
context "Full sinatra app" do
|
58
|
+
app SinatraTest
|
59
|
+
|
60
|
+
context "with explicit view format" do
|
61
|
+
setup { get '/' }
|
62
|
+
|
63
|
+
asserts(:body).equals('test/hoge')
|
64
|
+
end
|
65
|
+
|
66
|
+
context "with implicit view format" do
|
67
|
+
setup { get '/foo.html' }
|
68
|
+
|
69
|
+
asserts(:body).equals('test/hoge')
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
context "Ambivalent app" do
|
75
|
+
app AmbivalenceTest
|
76
|
+
|
77
|
+
context "with explicit view format" do
|
78
|
+
setup { get '/' }
|
79
|
+
|
80
|
+
asserts(:body).equals('test/hoge')
|
81
|
+
end
|
82
|
+
|
83
|
+
context "with implicit view format" do
|
84
|
+
setup { get '/foo.html' }
|
85
|
+
|
86
|
+
asserts(:body).equals('test/hoge')
|
87
|
+
end
|
88
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'rack/test'
|
2
|
+
require 'riot'
|
3
|
+
|
4
|
+
# Specify your app using the #app helper inside a context.
|
5
|
+
# If you don't specify one, Riot::Rack will recursively look for a config.ru file.
|
6
|
+
# Takes either an app class or a block argument.
|
7
|
+
# app { Padrino.application }
|
8
|
+
# app { Testtest.tap { |app| } }
|
9
|
+
|
10
|
+
class Riot::Situation
|
11
|
+
include Rack::Test::Methods
|
12
|
+
|
13
|
+
# The Rack app under test.
|
14
|
+
def app
|
15
|
+
@app
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class Riot::Context
|
20
|
+
# Set the Rack app which is to be tested.
|
21
|
+
#
|
22
|
+
# context "MyApp" do
|
23
|
+
# app { [200, {}, "Hello!"] }
|
24
|
+
# setup { get '/' }
|
25
|
+
# asserts(:status).equals(200)
|
26
|
+
# end
|
27
|
+
def app(app=nil, &block)
|
28
|
+
setup { @app = (app || block) }
|
29
|
+
end
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: padrino-views
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 4
|
8
|
+
version: "0.4"
|
9
|
+
platform: ruby
|
10
|
+
authors:
|
11
|
+
- Florian Gilcher
|
12
|
+
autorequire:
|
13
|
+
bindir: bin
|
14
|
+
cert_chain: []
|
15
|
+
|
16
|
+
date: 2010-07-19 00:00:00 +02:00
|
17
|
+
default_executable:
|
18
|
+
dependencies:
|
19
|
+
- !ruby/object:Gem::Dependency
|
20
|
+
name: padrino
|
21
|
+
prerelease: false
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
segments:
|
27
|
+
- 0
|
28
|
+
- 9
|
29
|
+
- 14
|
30
|
+
version: 0.9.14
|
31
|
+
type: :runtime
|
32
|
+
version_requirements: *id001
|
33
|
+
description: " Padriono Plugin to provide a View layer to Padrino.\n"
|
34
|
+
email: florian.gilcher@asquera.de
|
35
|
+
executables: []
|
36
|
+
|
37
|
+
extensions: []
|
38
|
+
|
39
|
+
extra_rdoc_files: []
|
40
|
+
|
41
|
+
files:
|
42
|
+
- lib/sinatra/views.rb
|
43
|
+
- test/sinatra/views_test.rb
|
44
|
+
- test/test_helper.rb
|
45
|
+
- lib/padrino/views.rb
|
46
|
+
- test/padrino/views_test.rb
|
47
|
+
- README.md
|
48
|
+
- padrino-views.gemspec
|
49
|
+
- Rakefile
|
50
|
+
- LICENSE
|
51
|
+
has_rdoc: true
|
52
|
+
homepage:
|
53
|
+
licenses: []
|
54
|
+
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
requirements: []
|
75
|
+
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 1.3.6
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: Padrino Plugin to provide a View layer to Padrino.
|
81
|
+
test_files: []
|
82
|
+
|