pakada-render 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +1 -0
- data/Gemfile +13 -0
- data/LICENSE +19 -0
- data/README.md +4 -0
- data/Rakefile +4 -0
- data/config.ru +21 -0
- data/controllers/foo.rb +3 -0
- data/lib/pakada-render.rb +1 -0
- data/lib/pakada/render.rb +77 -0
- data/lib/pakada/render/controller.rb +45 -0
- data/lib/pakada/render/renderer.rb +29 -0
- data/lib/pakada/render/rendering.rb +14 -0
- data/lib/pakada/render/rendering_context.rb +4 -0
- data/lib/pakada/render/version.rb +5 -0
- data/pakada-render.gemspec +26 -0
- data/spec/controller_spec.rb +146 -0
- data/spec/render_spec.rb +203 -0
- data/spec/renderer_spec.rb +85 -0
- data/spec/rendering_spec.rb +54 -0
- data/spec/spec_helper.rb +18 -0
- data/templates/layout.erb +3 -0
- data/templates/my_module/foo/bar.erb +1 -0
- metadata +122 -0
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source :rubygems
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
gem "rake"
|
6
|
+
gem "awesome_print"
|
7
|
+
|
8
|
+
gem "shotgun"
|
9
|
+
gem "thin"
|
10
|
+
|
11
|
+
gem "fakefs", :git => "https://github.com/lgierth/fakefs",
|
12
|
+
:branch => "fix_infinite_loop"
|
13
|
+
gem "pakada-dispatch", :git => "https://github.com/lgierth/pakada-dispatch"
|
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2011 Lars Gierth
|
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
data/Rakefile
ADDED
data/config.ru
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require "pakada/dispatch"
|
2
|
+
require "pakada/render"
|
3
|
+
require "awesome_print"
|
4
|
+
|
5
|
+
class ::MyModule
|
6
|
+
include Pakada::Module
|
7
|
+
@path = Pathname.new(".")
|
8
|
+
@dependencies << Pakada::Dispatch
|
9
|
+
|
10
|
+
def routes
|
11
|
+
route :test, "/", &controller(:foo).action(:bar)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
Pakada.instance(:default) {|ins|
|
16
|
+
ins.urls = ["/"]
|
17
|
+
ins.modules = MyModule, Pakada::Render
|
18
|
+
}
|
19
|
+
|
20
|
+
Pakada.boot
|
21
|
+
run Pakada
|
data/controllers/foo.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "pakada/theming"
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require "hike"
|
2
|
+
require "tilt"
|
3
|
+
|
4
|
+
require "pakada"
|
5
|
+
|
6
|
+
require "pakada/render/controller"
|
7
|
+
require "pakada/render/renderer"
|
8
|
+
require "pakada/render/rendering"
|
9
|
+
require "pakada/render/rendering_context"
|
10
|
+
require "pakada/render/version"
|
11
|
+
|
12
|
+
class Pakada::Render
|
13
|
+
include Pakada::Module
|
14
|
+
|
15
|
+
attr_reader :load_path, :template_map
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
Pakada.safety(RenderingContext).send :include, Hooked
|
19
|
+
end
|
20
|
+
|
21
|
+
def hooks
|
22
|
+
if Pakada[:dispatch]
|
23
|
+
Pakada[:dispatch].after :request, method(:render_layout)
|
24
|
+
Pakada.safety(Pakada::Dispatch::Controller).tap {|c|
|
25
|
+
c.around(:included) {|inner, klass|
|
26
|
+
inner.call klass
|
27
|
+
klass.send :include, Pakada.safety(Pakada::Render::Controller)
|
28
|
+
klass.after :new, method(:render_controller)
|
29
|
+
}
|
30
|
+
}
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def boot
|
35
|
+
Pakada.modules.each_value {|mod|
|
36
|
+
mod.extend Pakada.safety(Pakada::Render::Rendering)
|
37
|
+
}
|
38
|
+
|
39
|
+
@load_path = build_load_path
|
40
|
+
@template_map = build_template_map(load_path)
|
41
|
+
end
|
42
|
+
|
43
|
+
def build_load_path
|
44
|
+
Hike::Trail.new("/").tap {|load_path|
|
45
|
+
load_path.append_extensions *Tilt.mappings.keys
|
46
|
+
load_path.append_path Pakada.path.join("templates")
|
47
|
+
Pakada.modules.each_value {|mod|
|
48
|
+
load_path.append_path mod.path.join("templates")
|
49
|
+
}
|
50
|
+
}
|
51
|
+
end
|
52
|
+
|
53
|
+
def build_template_map(load_path)
|
54
|
+
load_path.paths.map {|path|
|
55
|
+
prefix = %r|#{Regexp.quote path.to_s}/|
|
56
|
+
Dir.glob("#{path}/**/*").map {|filepath|
|
57
|
+
next if File.directory? filepath
|
58
|
+
filepath.gsub(prefix, "").rpartition(".").first
|
59
|
+
}
|
60
|
+
}.flatten.compact.uniq.inject({}) {|map, name|
|
61
|
+
map[name.to_sym] = Tilt.new(load_path.find(name))
|
62
|
+
map
|
63
|
+
}
|
64
|
+
end
|
65
|
+
|
66
|
+
def render_controller(controller)
|
67
|
+
controller.response.write controller.render if controller.render?
|
68
|
+
end
|
69
|
+
|
70
|
+
def render_layout(response)
|
71
|
+
content = []
|
72
|
+
response[2].each {|chunk| content << chunk }
|
73
|
+
response[2] = [render!(:layout, :content => content)]
|
74
|
+
|
75
|
+
response[1].delete "Content-Length"
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
class Pakada::Render
|
2
|
+
module Controller
|
3
|
+
def self.included(klass)
|
4
|
+
klass.extend Pakada.safety(self::ClassMethods)
|
5
|
+
klass.instance_variable_set :@rendering_rules, :render => [], :dont_render => []
|
6
|
+
end
|
7
|
+
|
8
|
+
def render(template = nil, additional_locals = {})
|
9
|
+
if Hash === template
|
10
|
+
template, additional_locals = nil, template
|
11
|
+
end
|
12
|
+
template ||= options.values_at(:module, :controller, :action).join("/")
|
13
|
+
|
14
|
+
locals = instance_variables.inject({}) {|locals, key|
|
15
|
+
locals[key] = instance_variable_get(key)
|
16
|
+
locals
|
17
|
+
}.merge(additional_locals)
|
18
|
+
Pakada[options[:module]].render template.to_sym, locals
|
19
|
+
end
|
20
|
+
|
21
|
+
def render?
|
22
|
+
action = options[:action]
|
23
|
+
render, dont_render = *self.class.rendering_rules.values_at(:render, :dont_render)
|
24
|
+
|
25
|
+
!dont_render.include?(action) && (
|
26
|
+
!dont_render.include?(:*) || render.include?(action))
|
27
|
+
end
|
28
|
+
|
29
|
+
module ClassMethods
|
30
|
+
attr_reader :rendering_rules
|
31
|
+
|
32
|
+
def dont_render(action = nil, *more_actions)
|
33
|
+
if action
|
34
|
+
rendering_rules[:dont_render].push action, *more_actions
|
35
|
+
else
|
36
|
+
rendering_rules[:dont_render] = [:*]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def render(action, *more_actions)
|
41
|
+
rendering_rules[:render].push action, *more_actions
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class Pakada::Render
|
2
|
+
class Renderer
|
3
|
+
attr_reader :engine, :locals
|
4
|
+
|
5
|
+
def initialize(engine, locals = {})
|
6
|
+
@engine, @locals = engine, locals
|
7
|
+
@instance = Pakada.instance
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_s
|
11
|
+
@string ||= Pakada.instance(@instance.name) {
|
12
|
+
context = Pakada.safety(Pakada::Render::RenderingContext).new
|
13
|
+
engine.render context, locals
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
def each
|
18
|
+
yield to_s
|
19
|
+
end
|
20
|
+
|
21
|
+
def [](key)
|
22
|
+
locals[key]
|
23
|
+
end
|
24
|
+
|
25
|
+
def []=(key, value)
|
26
|
+
locals[key] = value
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class Pakada::Render
|
2
|
+
module Rendering
|
3
|
+
def render(template, locals = {})
|
4
|
+
engine = Pakada[:render].template_map[template.to_sym]
|
5
|
+
raise ArgumentError, "Unknown template #{template.inspect}" unless engine
|
6
|
+
|
7
|
+
Pakada.safety(Pakada::Render::Renderer).new engine, locals
|
8
|
+
end
|
9
|
+
|
10
|
+
def render!(template, locals = {})
|
11
|
+
render(template, locals).to_s
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "pakada/render/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "pakada-render"
|
7
|
+
s.version = Pakada::Render::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Lars Gierth"]
|
10
|
+
s.email = ["lars.gierth@gmail.com"]
|
11
|
+
s.homepage = "https://rubygems.org/gems/pakada-render"
|
12
|
+
s.summary = %q{Template rendering for Pakada}
|
13
|
+
#s.description = %q{TODO: Write a gem description}
|
14
|
+
|
15
|
+
s.add_dependency "pakada"
|
16
|
+
s.add_dependency "tilt"
|
17
|
+
s.add_dependency "hike"
|
18
|
+
|
19
|
+
s.add_development_dependency "rspec"
|
20
|
+
s.add_development_dependency "fakefs"
|
21
|
+
|
22
|
+
s.files = `git ls-files`.split("\n") - [".gitignore", ".travis.yml"]
|
23
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
24
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
25
|
+
s.require_paths = ["lib"]
|
26
|
+
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Pakada::Render::Controller do
|
4
|
+
let(:controller) {
|
5
|
+
Pakada.safety(Pakada::Dispatch::Controller).create {
|
6
|
+
include Pakada.safety(Pakada::Render::Controller)
|
7
|
+
action(:bar) {}
|
8
|
+
}
|
9
|
+
}
|
10
|
+
let(:obj) {
|
11
|
+
options = {
|
12
|
+
:module => :foo_module,
|
13
|
+
:controller => :foo,
|
14
|
+
:action => :bar
|
15
|
+
}
|
16
|
+
controller.new({}, options, &proc {})
|
17
|
+
}
|
18
|
+
|
19
|
+
let(:foo_module) { stub "foo module" }
|
20
|
+
|
21
|
+
before {
|
22
|
+
Pakada.modules[:foo_module] = foo_module
|
23
|
+
}
|
24
|
+
|
25
|
+
context ".included(klass)" do
|
26
|
+
let(:klass) { stub "includer class" }
|
27
|
+
|
28
|
+
it "extends klass with ClassMethods" do
|
29
|
+
class_methods = Pakada.safety(Pakada::Render::Controller::ClassMethods)
|
30
|
+
klass.should_receive(:extend).with class_methods
|
31
|
+
|
32
|
+
Pakada.safety(Pakada::Render::Controller).included klass
|
33
|
+
end
|
34
|
+
|
35
|
+
it "defaults #rendering_rules to empty Arrays" do
|
36
|
+
Pakada.safety(Pakada::Render::Controller).included klass
|
37
|
+
|
38
|
+
klass.rendering_rules[:render].should be_an(Array)
|
39
|
+
klass.rendering_rules[:render].should be_empty
|
40
|
+
|
41
|
+
klass.rendering_rules[:dont_render].should be_an(Array)
|
42
|
+
klass.rendering_rules[:dont_render].should be_empty
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context "#render(template, additional_locals)" do
|
47
|
+
let(:renderer) { stub "renderer" }
|
48
|
+
|
49
|
+
it "returns a Renderer for the passed template" do
|
50
|
+
foo_module.should_receive(:render) {|template|
|
51
|
+
template.should equal(:foo)
|
52
|
+
renderer
|
53
|
+
}
|
54
|
+
|
55
|
+
obj.render(:foo).should equal(renderer)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "passes all instance variables as locals to the Renderer" do
|
59
|
+
foo_module.should_receive(:render) {|template, locals|
|
60
|
+
locals[:@options].should equal(obj.options)
|
61
|
+
locals[:@params].should equal(obj.params)
|
62
|
+
locals[:@request].should equal(obj.request)
|
63
|
+
locals[:@response].should equal(obj.response)
|
64
|
+
}
|
65
|
+
obj.render additional_locals
|
66
|
+
end
|
67
|
+
|
68
|
+
let(:additional_locals) {
|
69
|
+
{
|
70
|
+
:foo => stub("foo local"),
|
71
|
+
:bar => stub("bar local")
|
72
|
+
}
|
73
|
+
}
|
74
|
+
|
75
|
+
it "merges additional_locals into the locals for Renderer" do
|
76
|
+
foo_module.should_receive(:render) {|template, locals|
|
77
|
+
locals[:foo].should equal(additional_locals[:foo])
|
78
|
+
locals[:bar].should equal(additional_locals[:bar])
|
79
|
+
}
|
80
|
+
obj.render additional_locals
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context "#render" do
|
85
|
+
it "defaults template to :<module>/<controller>/<action>" do
|
86
|
+
foo_module.should_receive(:render) {|template|
|
87
|
+
template.should equal(:"foo_module/foo/bar")
|
88
|
+
}
|
89
|
+
obj.render
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
context "#render?" do
|
94
|
+
it "returns true by default" do
|
95
|
+
obj.render?.should be_true
|
96
|
+
end
|
97
|
+
|
98
|
+
it "returns false if dont_render rules include action" do
|
99
|
+
controller.dont_render :bar
|
100
|
+
|
101
|
+
obj.render?.should be_false
|
102
|
+
end
|
103
|
+
|
104
|
+
it "returns false if dont_render rules include wildcard" do
|
105
|
+
controller.dont_render
|
106
|
+
|
107
|
+
obj.render?.should be_false
|
108
|
+
end
|
109
|
+
|
110
|
+
it "prioritizes render rules" do
|
111
|
+
controller.dont_render
|
112
|
+
controller.render :bar
|
113
|
+
|
114
|
+
obj.render?.should be_true
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
context ".render(action, *more_actions)" do
|
119
|
+
it "adds each action to the render ruleset" do
|
120
|
+
controller.tap {|c|
|
121
|
+
c.render :foo, :bar, :baz
|
122
|
+
c.rendering_rules[:render].should include(:foo)
|
123
|
+
c.rendering_rules[:render].should include(:bar)
|
124
|
+
c.rendering_rules[:render].should include(:baz)
|
125
|
+
}
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
context ".dont_render(action, *more_actions)" do
|
130
|
+
it "adds each action to the dont_render ruleset" do
|
131
|
+
controller.tap {|c|
|
132
|
+
c.dont_render :foo, :bar, :baz
|
133
|
+
c.rendering_rules[:dont_render].should include(:foo)
|
134
|
+
c.rendering_rules[:dont_render].should include(:bar)
|
135
|
+
c.rendering_rules[:dont_render].should include(:baz)
|
136
|
+
}
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
context ".dont_render" do
|
141
|
+
it "adds a wildcard to the dont_render ruleset" do
|
142
|
+
controller.dont_render
|
143
|
+
controller.rendering_rules[:dont_render].should include(:*)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
data/spec/render_spec.rb
ADDED
@@ -0,0 +1,203 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "fileutils"
|
3
|
+
|
4
|
+
describe Pakada::Render do
|
5
|
+
let(:render) { Pakada::Render.new }
|
6
|
+
|
7
|
+
context "#initialize" do
|
8
|
+
it "makes RenderingContext hookable" do
|
9
|
+
render
|
10
|
+
Pakada.safety(Pakada::Render::RenderingContext).should respond_to(:instance_hooked)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "#hooks" do
|
15
|
+
let(:dispatch) { stub "dispatch module" }
|
16
|
+
|
17
|
+
before {
|
18
|
+
Pakada.modules[:dispatch] = dispatch
|
19
|
+
dispatch.stub :after
|
20
|
+
}
|
21
|
+
|
22
|
+
it "hooks Pakada::Dispatch#request with #render_layout" do
|
23
|
+
dispatch.should_receive(:after) {|method, block|
|
24
|
+
method.should equal(:request)
|
25
|
+
block.should == render.method(:render_layout)
|
26
|
+
}
|
27
|
+
render.hooks
|
28
|
+
end
|
29
|
+
|
30
|
+
let(:klass) { stub "controller class", :include => nil }
|
31
|
+
|
32
|
+
it "hooks Pakada::Dispatch::Controller.included to include Controller" do
|
33
|
+
Pakada.safety(Pakada::Dispatch::Controller).should_receive(:around) {|method, &block|
|
34
|
+
method.should equal(:included)
|
35
|
+
klass.should_receive(:send).with :include, Pakada.safety(Pakada::Render::Controller)
|
36
|
+
klass.should_receive(:after) {|method, block|
|
37
|
+
method.should equal(:new)
|
38
|
+
block.should == render.method(:render_controller)
|
39
|
+
}
|
40
|
+
block.call stub(:call => nil), klass
|
41
|
+
}
|
42
|
+
render.hooks
|
43
|
+
end
|
44
|
+
|
45
|
+
it "hooks Pakada::Dispatch::Controller.included to hook controller#new with #render_controller" do
|
46
|
+
Pakada.safety(Pakada::Dispatch::Controller).should_receive(:around) {|method, &block|
|
47
|
+
method.should equal(:included)
|
48
|
+
klass.should_receive(:after) {|method, block|
|
49
|
+
method.should equal(:new)
|
50
|
+
block.should == render.method(:render_controller)
|
51
|
+
}
|
52
|
+
block.call stub(:call => nil), klass
|
53
|
+
}
|
54
|
+
render.hooks
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context "#boot" do
|
59
|
+
let(:load_path) { stub "load path" }
|
60
|
+
let(:template_map) { stub "template map" }
|
61
|
+
|
62
|
+
before {
|
63
|
+
render.stub :build_load_path => load_path
|
64
|
+
render.stub :build_template_map => template_map
|
65
|
+
render.boot
|
66
|
+
}
|
67
|
+
|
68
|
+
it "includes Rendering into each module" do
|
69
|
+
Pakada.modules.each_value {|mod|
|
70
|
+
rendering = Pakada.safety(Pakada::Render::Rendering)
|
71
|
+
(class << mod; self; end).included_modules.should include(rendering)
|
72
|
+
}
|
73
|
+
end
|
74
|
+
|
75
|
+
it "builds the load path" do
|
76
|
+
render.load_path.should equal(load_path)
|
77
|
+
end
|
78
|
+
|
79
|
+
it "builds the template map" do
|
80
|
+
render.template_map.should equal(template_map)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context "#build_load_path" do
|
85
|
+
let(:load_path) { render.build_load_path }
|
86
|
+
|
87
|
+
it "adds each of Tilt's mappings as an extension" do
|
88
|
+
Tilt.mappings.keys.each {|ext|
|
89
|
+
load_path.extensions.should include(".#{ext}")
|
90
|
+
}
|
91
|
+
end
|
92
|
+
|
93
|
+
it "adds the Pakada instance's path first" do
|
94
|
+
load_path.paths[0].should == Pakada.path.join("templates").to_s
|
95
|
+
end
|
96
|
+
|
97
|
+
it "adds each module's path" do
|
98
|
+
Pakada.modules.each_value {|mod|
|
99
|
+
load_path.paths.should include(mod.path.join("templates").to_s)
|
100
|
+
}
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
context "#build_template_map(load_path)" do
|
105
|
+
let(:baz) { stub "baz engine" }
|
106
|
+
let(:layout) { stub "layout engine" }
|
107
|
+
let(:derp) { stub "derp engine" }
|
108
|
+
|
109
|
+
before {
|
110
|
+
# undefined method `file?' for #<FakeFS::File::Stat:0xa72a808>
|
111
|
+
#FakeFS.activate!
|
112
|
+
|
113
|
+
Pakada.stub :path => Pathname.new("spec/tmp/instance").expand_path
|
114
|
+
Pakada[:render].stub :path => Pathname.new("spec/tmp/pakada-render").expand_path
|
115
|
+
Pakada[:dispatch].stub :path => Pathname.new("spec/tmp/pakada-dispatch").expand_path
|
116
|
+
|
117
|
+
path = Pakada[:render].path.join("templates")
|
118
|
+
FileUtils.mkdir_p path.join("foo/bar")
|
119
|
+
FileUtils.touch path.join("foo/bar/baz.erb").to_s
|
120
|
+
FileUtils.touch path.join("layout.md").to_s
|
121
|
+
FileUtils.touch path.join("herp_derp.haml").to_s
|
122
|
+
|
123
|
+
path = Pakada[:dispatch].path.join("templates")
|
124
|
+
FileUtils.mkdir_p path.join("foo/bar")
|
125
|
+
FileUtils.touch path.join("foo/bar/baz.sass").to_s
|
126
|
+
FileUtils.touch path.join("layout.coffee").to_s
|
127
|
+
FileUtils.touch path.join("herp_derp.textile").to_s
|
128
|
+
|
129
|
+
path = Pakada.path.join("templates")
|
130
|
+
FileUtils.mkdir_p path.join("foo/bar")
|
131
|
+
FileUtils.touch path.join("foo/bar/baz.str").to_s
|
132
|
+
FileUtils.touch path.join("layout.less").to_s
|
133
|
+
FileUtils.touch path.join("herp_derp.creole").to_s
|
134
|
+
|
135
|
+
@engines = {
|
136
|
+
path.join("foo/bar/baz.str").to_s => baz,
|
137
|
+
path.join("layout.less").to_s => layout,
|
138
|
+
path.join("herp_derp.creole").to_s => derp
|
139
|
+
}
|
140
|
+
}
|
141
|
+
|
142
|
+
after {
|
143
|
+
#FakeFS.deactivate!
|
144
|
+
#FakeFS::FileSystem.clear
|
145
|
+
|
146
|
+
FileUtils.rm_rf "spec/tmp"
|
147
|
+
}
|
148
|
+
|
149
|
+
let(:map) { render.build_template_map render.build_load_path }
|
150
|
+
|
151
|
+
it "evaluates the complete load path" do
|
152
|
+
Tilt.should_receive(:new).exactly(3).times {|path| @engines[path] }
|
153
|
+
map.should == {
|
154
|
+
:"foo/bar/baz" => baz,
|
155
|
+
:layout => layout,
|
156
|
+
:herp_derp => derp
|
157
|
+
}
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
context "#render_controller" do
|
162
|
+
let(:controller) { stub "controller", :response => stub("response") }
|
163
|
+
let(:result) { stub "result" }
|
164
|
+
|
165
|
+
before { controller.stub :render => result }
|
166
|
+
|
167
|
+
it "renders the controller's template" do
|
168
|
+
controller.stub :render? => true
|
169
|
+
controller.response.should_receive(:write).with result
|
170
|
+
|
171
|
+
render.render_controller controller
|
172
|
+
end
|
173
|
+
|
174
|
+
it "doesn't render if it's not supposed to" do
|
175
|
+
controller.stub :render? => false
|
176
|
+
controller.response.should_not_receive :write
|
177
|
+
|
178
|
+
render.render_controller controller
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
context "#render_layout" do
|
183
|
+
let(:response) {
|
184
|
+
[200, {"Content-Length" => "123"}, [content]]
|
185
|
+
}
|
186
|
+
let(:content) { stub "content" }
|
187
|
+
|
188
|
+
before { render.stub :render! }
|
189
|
+
|
190
|
+
it "render's a layout around the content" do
|
191
|
+
render.should_receive(:render!) {|template, locals|
|
192
|
+
template.should equal(:layout)
|
193
|
+
locals[:content].should == [content]
|
194
|
+
}
|
195
|
+
render.render_layout response
|
196
|
+
end
|
197
|
+
|
198
|
+
it "removes the Content-Length header" do
|
199
|
+
render.render_layout response
|
200
|
+
response[1]["Content-Length"].should be_nil
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Pakada::Render::Renderer do
|
4
|
+
let(:engine) { stub "engine" }
|
5
|
+
let(:locals) { stub "locals" }
|
6
|
+
let(:renderer) {
|
7
|
+
Pakada.safety(Pakada::Render::Renderer).new engine, locals
|
8
|
+
}
|
9
|
+
let(:rendered_template) { stub "renderer template "}
|
10
|
+
|
11
|
+
context "#initialize(engine, locals)" do
|
12
|
+
it "sets engine and locals" do
|
13
|
+
renderer.engine.should equal(engine)
|
14
|
+
renderer.locals.should equal(locals)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context "#to_s" do
|
19
|
+
let(:context) { stub "context" }
|
20
|
+
|
21
|
+
before {
|
22
|
+
Pakada.safety(Pakada::Render::RenderingContext).stub :new => context
|
23
|
+
Pakada.instance(:foo) {}
|
24
|
+
renderer
|
25
|
+
}
|
26
|
+
|
27
|
+
it "renders the template with the locals and a clean RenderingContext" do
|
28
|
+
engine.should_receive(:render) {|ctx, lkls|
|
29
|
+
context.should equal(ctx)
|
30
|
+
locals.should equal(lkls)
|
31
|
+
rendered_template
|
32
|
+
}
|
33
|
+
renderer.to_s.should equal(rendered_template)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "memoizes the renderer template" do
|
37
|
+
engine.should_receive(:render).once { rendered_template }
|
38
|
+
renderer.to_s
|
39
|
+
renderer.to_s.should equal(rendered_template)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "renders within the context of the Pakada instance it was created in" do
|
43
|
+
engine.should_receive(:render) {
|
44
|
+
Pakada.instance.should equal(Pakada.instance(:default))
|
45
|
+
}
|
46
|
+
Pakada.instance(:foo) { renderer.to_s }
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context "#each" do
|
51
|
+
it "yields the result of #to_s" do
|
52
|
+
renderer.should_receive(:to_s) { rendered_template }
|
53
|
+
yielded = []
|
54
|
+
renderer.each {|y| yielded << y }
|
55
|
+
|
56
|
+
yielded.should == [rendered_template]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context "#[](key)" do
|
61
|
+
let(:locals) {
|
62
|
+
{
|
63
|
+
:foo => stub("foo local")
|
64
|
+
}
|
65
|
+
}
|
66
|
+
|
67
|
+
it "returns the value for the specified key in locals" do
|
68
|
+
renderer[:foo].should equal(locals[:foo])
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context "#[](key, value)" do
|
73
|
+
let(:locals) {
|
74
|
+
{
|
75
|
+
:foo => stub("foo local")
|
76
|
+
}
|
77
|
+
}
|
78
|
+
let(:bar) { stub "bar local" }
|
79
|
+
|
80
|
+
it "sets the value for the specified key in locals" do
|
81
|
+
renderer[:bar] = bar
|
82
|
+
renderer[:bar].should equal(bar)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Pakada::Render::Rendering do
|
4
|
+
let(:render) { stub "render module" }
|
5
|
+
let(:renderer) { stub "renderer" }
|
6
|
+
let(:obj) {
|
7
|
+
Object.new.tap {|obj| obj.extend Pakada.safety(Pakada::Render::Rendering) }
|
8
|
+
}
|
9
|
+
|
10
|
+
let(:engine) { stub "engine" }
|
11
|
+
let(:locals) { stub "locals" }
|
12
|
+
|
13
|
+
before {
|
14
|
+
Pakada.safety(Pakada::Render::Renderer).stub :new => renderer
|
15
|
+
|
16
|
+
Pakada.modules[:render] = render
|
17
|
+
render.stub :template_map => {
|
18
|
+
:foo => engine
|
19
|
+
}
|
20
|
+
}
|
21
|
+
|
22
|
+
context "#render(template, locals)" do
|
23
|
+
it "raises an ArgumentError if there's no engine for the template" do
|
24
|
+
expect { obj.render :i_dont_exist }.to raise_error(ArgumentError)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "returns a Renderer for the specified template and locals" do
|
28
|
+
Pakada.safety(Pakada::Render::Renderer).should_receive(:new) {|ngin, lcls|
|
29
|
+
ngin.should equal(engine)
|
30
|
+
lcls.should equal(locals)
|
31
|
+
renderer
|
32
|
+
}
|
33
|
+
obj.render(:foo, locals).should equal(renderer)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "defaults locals to an empty Hash" do
|
37
|
+
Pakada.safety(Pakada::Render::Renderer).should_receive(:new) {|ngin, lcls|
|
38
|
+
lcls.should be_a(Hash)
|
39
|
+
lcls.should be_empty
|
40
|
+
renderer
|
41
|
+
}
|
42
|
+
obj.render(:foo).should equal(renderer)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context "#render!(template, locals)" do
|
47
|
+
let(:rendered_template) { stub "rendered template" }
|
48
|
+
|
49
|
+
it "returns #render#to_s" do
|
50
|
+
renderer.should_receive(:to_s) { rendered_template }
|
51
|
+
obj.render!(:foo, locals).should equal(rendered_template)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require "pakada/render"
|
2
|
+
require "pakada/dispatch"
|
3
|
+
|
4
|
+
require "fakefs/safe"
|
5
|
+
|
6
|
+
begin
|
7
|
+
require "awesome_print"
|
8
|
+
rescue LoadError; end
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.before(:each) {
|
12
|
+
Pakada.instances.clear
|
13
|
+
Pakada.instance(:default) {}
|
14
|
+
|
15
|
+
Pakada.stack.clear
|
16
|
+
Pakada.stack.push Pakada.instances[:default]
|
17
|
+
}
|
18
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
hello <%= @name %>!
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pakada-render
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Lars Gierth
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-09-03 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: pakada
|
16
|
+
requirement: &72808660 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *72808660
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: tilt
|
27
|
+
requirement: &72808220 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *72808220
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: hike
|
38
|
+
requirement: &72807800 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *72807800
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
requirement: &72807480 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *72807480
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: fakefs
|
60
|
+
requirement: &72806970 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *72806970
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- lars.gierth@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .rspec
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- config.ru
|
82
|
+
- controllers/foo.rb
|
83
|
+
- lib/pakada-render.rb
|
84
|
+
- lib/pakada/render.rb
|
85
|
+
- lib/pakada/render/controller.rb
|
86
|
+
- lib/pakada/render/renderer.rb
|
87
|
+
- lib/pakada/render/rendering.rb
|
88
|
+
- lib/pakada/render/rendering_context.rb
|
89
|
+
- lib/pakada/render/version.rb
|
90
|
+
- pakada-render.gemspec
|
91
|
+
- spec/controller_spec.rb
|
92
|
+
- spec/render_spec.rb
|
93
|
+
- spec/renderer_spec.rb
|
94
|
+
- spec/rendering_spec.rb
|
95
|
+
- spec/spec_helper.rb
|
96
|
+
- templates/layout.erb
|
97
|
+
- templates/my_module/foo/bar.erb
|
98
|
+
homepage: https://rubygems.org/gems/pakada-render
|
99
|
+
licenses: []
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ! '>='
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
requirements: []
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 1.8.10
|
119
|
+
signing_key:
|
120
|
+
specification_version: 3
|
121
|
+
summary: Template rendering for Pakada
|
122
|
+
test_files: []
|