sinatra-views 0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +19 -0
- data/README.md +66 -0
- data/Rakefile +11 -0
- data/lib/sinatra/views.rb +38 -0
- data/sinatra-views.gemspec +19 -0
- data/test/sinatra/views_test.rb +88 -0
- data/test/test_helper.rb +30 -0
- metadata +86 -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,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 = "sinatra-views"
|
|
7
|
+
s.version = 0.4
|
|
8
|
+
s.authors = ["Florian Gilcher"]
|
|
9
|
+
s.email = "florian.gilcher@asquera.de"
|
|
10
|
+
s.summary = "Sinatra Plugin to provide a View layer to Sinatra."
|
|
11
|
+
s.description = <<-DESC
|
|
12
|
+
Sinatra Plugin to provide a View layer to Sinatra.
|
|
13
|
+
DESC
|
|
14
|
+
|
|
15
|
+
s.files = Dir['**/{sinatra/*,sinatra,test_helper.rb}'] + %w(README.md sinatra-views.gemspec Rakefile LICENSE)
|
|
16
|
+
s.platform = Gem::Platform::RUBY
|
|
17
|
+
s.require_path = 'lib'
|
|
18
|
+
s.add_dependency('sinatra', '>= 1.0')
|
|
19
|
+
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,86 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: sinatra-views
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
hash: 3
|
|
5
|
+
prerelease: false
|
|
6
|
+
segments:
|
|
7
|
+
- 0
|
|
8
|
+
- 4
|
|
9
|
+
version: "0.4"
|
|
10
|
+
platform: ruby
|
|
11
|
+
authors:
|
|
12
|
+
- Florian Gilcher
|
|
13
|
+
autorequire:
|
|
14
|
+
bindir: bin
|
|
15
|
+
cert_chain: []
|
|
16
|
+
|
|
17
|
+
date: 2010-07-19 00:00:00 +02: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
|
+
hash: 15
|
|
29
|
+
segments:
|
|
30
|
+
- 1
|
|
31
|
+
- 0
|
|
32
|
+
version: "1.0"
|
|
33
|
+
type: :runtime
|
|
34
|
+
version_requirements: *id001
|
|
35
|
+
description: " Sinatra Plugin to provide a View layer to Sinatra.\n"
|
|
36
|
+
email: florian.gilcher@asquera.de
|
|
37
|
+
executables: []
|
|
38
|
+
|
|
39
|
+
extensions: []
|
|
40
|
+
|
|
41
|
+
extra_rdoc_files: []
|
|
42
|
+
|
|
43
|
+
files:
|
|
44
|
+
- lib/sinatra/views.rb
|
|
45
|
+
- test/sinatra/views_test.rb
|
|
46
|
+
- test/test_helper.rb
|
|
47
|
+
- README.md
|
|
48
|
+
- sinatra-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
|
+
none: false
|
|
62
|
+
requirements:
|
|
63
|
+
- - ">="
|
|
64
|
+
- !ruby/object:Gem::Version
|
|
65
|
+
hash: 3
|
|
66
|
+
segments:
|
|
67
|
+
- 0
|
|
68
|
+
version: "0"
|
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
|
+
none: false
|
|
71
|
+
requirements:
|
|
72
|
+
- - ">="
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
hash: 3
|
|
75
|
+
segments:
|
|
76
|
+
- 0
|
|
77
|
+
version: "0"
|
|
78
|
+
requirements: []
|
|
79
|
+
|
|
80
|
+
rubyforge_project:
|
|
81
|
+
rubygems_version: 1.3.7
|
|
82
|
+
signing_key:
|
|
83
|
+
specification_version: 3
|
|
84
|
+
summary: Sinatra Plugin to provide a View layer to Sinatra.
|
|
85
|
+
test_files: []
|
|
86
|
+
|