tiltout 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/lib/tiltout.rb +3 -2
- data/lib/tiltout/version.rb +28 -0
- data/test/tiltout_test.rb +45 -22
- data/tiltout.gemspec +1 -1
- metadata +5 -4
data/Gemfile.lock
CHANGED
data/lib/tiltout.rb
CHANGED
@@ -23,16 +23,16 @@
|
|
23
23
|
# SOFTWARE.
|
24
24
|
#++
|
25
25
|
require "tilt"
|
26
|
+
require "tiltout/version"
|
26
27
|
|
27
28
|
class Tiltout
|
28
|
-
VERSION = "1.0.0"
|
29
|
-
|
30
29
|
def initialize(template_root, opt = {})
|
31
30
|
@template_root = template_root
|
32
31
|
@cache = {} if !opt.key?(:cache) || opt[:cache]
|
33
32
|
@layout = opt[:layout]
|
34
33
|
@default_type = opt[:default_type] || "erb"
|
35
34
|
@context_class = Class.new
|
35
|
+
(opt[:helpers] || []).each { |h| helper(h) }
|
36
36
|
end
|
37
37
|
|
38
38
|
def helper(helper)
|
@@ -62,6 +62,7 @@ class Tiltout
|
|
62
62
|
end
|
63
63
|
|
64
64
|
def find_file(name)
|
65
|
+
return name[:file] if name.is_a?(Hash)
|
65
66
|
full_name = File.join(@template_root, name.to_s)
|
66
67
|
return full_name if cached?(full_name) || File.exists?(full_name)
|
67
68
|
File.join(@template_root, "#{name}.#{@default_type}")
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# --
|
3
|
+
# The MIT License (MIT)
|
4
|
+
#
|
5
|
+
# Copyright (C) 2012 Gitorious AS
|
6
|
+
#
|
7
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
8
|
+
# of this software and associated documentation files (the "Software"), to deal
|
9
|
+
# in the Software without restriction, including without limitation the rights
|
10
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11
|
+
# copies of the Software, and to permit persons to whom the Software is
|
12
|
+
# furnished to do so, subject to the following conditions:
|
13
|
+
#
|
14
|
+
# The above copyright notice and this permission notice shall be included in all
|
15
|
+
# copies or substantial portions of the Software.
|
16
|
+
#
|
17
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
19
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
20
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
21
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
22
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
23
|
+
# SOFTWARE.
|
24
|
+
#++
|
25
|
+
|
26
|
+
class Tiltout
|
27
|
+
VERSION = "1.1.0"
|
28
|
+
end
|
data/test/tiltout_test.rb
CHANGED
@@ -34,14 +34,23 @@ end
|
|
34
34
|
describe Tiltout do
|
35
35
|
before { @root = "/dolt/views" }
|
36
36
|
|
37
|
+
def file_read
|
38
|
+
File.respond_to?(:binread) ? :binread : :read
|
39
|
+
end
|
40
|
+
|
41
|
+
def fake_file(file, content)
|
42
|
+
File.stubs(:file_exists?).with(file).returns(true)
|
43
|
+
File.stubs(file_read).with(file).returns(content)
|
44
|
+
end
|
45
|
+
|
37
46
|
it "reads template from file" do
|
38
|
-
File.expects(
|
47
|
+
File.expects(file_read).with("/dolt/views/file.erb").returns("")
|
39
48
|
renderer = Tiltout.new("/dolt/views")
|
40
49
|
renderer.render(:file)
|
41
50
|
end
|
42
51
|
|
43
52
|
it "renders template with locals" do
|
44
|
-
|
53
|
+
fake_file("#{@root}/file.erb", "<%= name %>!")
|
45
54
|
renderer = Tiltout.new(@root)
|
46
55
|
|
47
56
|
assert_equal "Chris!", renderer.render(:file, { :name => "Chris"})
|
@@ -49,43 +58,51 @@ describe Tiltout do
|
|
49
58
|
|
50
59
|
it "caches template in memory" do
|
51
60
|
renderer = Tiltout.new(@root)
|
52
|
-
|
61
|
+
fake_file("#{@root}/file.erb", "Original")
|
53
62
|
renderer.render(:file)
|
54
|
-
|
63
|
+
fake_file("#{@root}/file.erb", "Updated")
|
55
64
|
|
56
65
|
assert_equal "Original", renderer.render(:file)
|
57
66
|
end
|
58
67
|
|
59
68
|
it "does not cache template in memory when configured not to" do
|
60
69
|
renderer = Tiltout.new(@root, :cache => false)
|
61
|
-
|
70
|
+
fake_file("#{@root}/file.erb", "Original")
|
62
71
|
renderer.render(:file)
|
63
|
-
|
72
|
+
fake_file("#{@root}/file.erb", "Updated")
|
64
73
|
|
65
74
|
assert_equal "Updated", renderer.render(:file)
|
66
75
|
end
|
67
76
|
|
68
77
|
it "renders template with layout" do
|
69
78
|
renderer = Tiltout.new("/", :layout => "layout")
|
70
|
-
|
71
|
-
|
79
|
+
fake_file("/file.erb", "Template")
|
80
|
+
fake_file("/layout.erb", "I give you: <%= yield %>")
|
81
|
+
|
82
|
+
assert_equal "I give you: Template", renderer.render(:file)
|
83
|
+
end
|
84
|
+
|
85
|
+
it "renders template with layout not in template root" do
|
86
|
+
renderer = Tiltout.new("/somewhere", :layout => { :file => "/my/layout.erb" })
|
87
|
+
fake_file("/somewhere/file.erb", "Template")
|
88
|
+
fake_file("/my/layout.erb", "I give you: <%= yield %>")
|
72
89
|
|
73
90
|
assert_equal "I give you: Template", renderer.render(:file)
|
74
91
|
end
|
75
92
|
|
76
93
|
it "renders template once without layout" do
|
77
94
|
renderer = Tiltout.new("/", :layout => "layout")
|
78
|
-
|
79
|
-
|
95
|
+
fake_file("/file.erb", "Template")
|
96
|
+
fake_file("/layout.erb", "I give you: <%= yield %>")
|
80
97
|
|
81
98
|
assert_equal "Template", renderer.render(:file, {}, :layout => nil)
|
82
99
|
end
|
83
100
|
|
84
101
|
it "renders template once with different layout" do
|
85
102
|
renderer = Tiltout.new("/", :layout => "layout")
|
86
|
-
|
87
|
-
|
88
|
-
|
103
|
+
fake_file("/file.erb", "Template")
|
104
|
+
fake_file("/layout.erb", "I give you: <%= yield %>")
|
105
|
+
fake_file("/layout2.erb", "I present you: <%= yield %>")
|
89
106
|
|
90
107
|
html = renderer.render(:file, {}, :layout => "layout2")
|
91
108
|
|
@@ -94,15 +111,15 @@ describe Tiltout do
|
|
94
111
|
|
95
112
|
it "renders templates of default type" do
|
96
113
|
renderer = Tiltout.new("/", :default_type => :str)
|
97
|
-
|
114
|
+
fake_file("/file.str", "Hey!")
|
98
115
|
|
99
116
|
assert_equal "Hey!", renderer.render(:file)
|
100
117
|
end
|
101
118
|
|
102
119
|
it "renders templates of specific type" do
|
103
120
|
renderer = Tiltout.new("/", :default_type => :lol)
|
104
|
-
|
105
|
-
|
121
|
+
fake_file("/file.lol", "No!")
|
122
|
+
fake_file("/file.erb", "Yes!")
|
106
123
|
File.stubs(:exists?).with("/file.lol").returns(false)
|
107
124
|
File.stubs(:exists?).with("/file.erb").returns(true)
|
108
125
|
|
@@ -112,14 +129,21 @@ describe Tiltout do
|
|
112
129
|
it "renders with helper object" do
|
113
130
|
renderer = Tiltout.new("/")
|
114
131
|
renderer.helper(ViewHelper)
|
115
|
-
|
132
|
+
fake_file("/file.erb", "Say it: <%= say_it %>")
|
133
|
+
|
134
|
+
assert_equal "Say it: YES", renderer.render(:file)
|
135
|
+
end
|
136
|
+
|
137
|
+
it "creates with helper object" do
|
138
|
+
renderer = Tiltout.new("/", :helpers => [ViewHelper])
|
139
|
+
fake_file("/file.erb", "Say it: <%= say_it %>")
|
116
140
|
|
117
141
|
assert_equal "Say it: YES", renderer.render(:file)
|
118
142
|
end
|
119
143
|
|
120
144
|
it "does not leak state across render calls" do
|
121
145
|
renderer = Tiltout.new("/")
|
122
|
-
|
146
|
+
fake_file("/file.erb", <<-TEMPLATE)
|
123
147
|
<%= @response %><% @response = "NO" %><%= @response %>
|
124
148
|
TEMPLATE
|
125
149
|
|
@@ -129,11 +153,10 @@ describe Tiltout do
|
|
129
153
|
|
130
154
|
it "shares state between template and layout" do
|
131
155
|
renderer = Tiltout.new("/", :layout => "layout")
|
132
|
-
|
133
|
-
|
156
|
+
fake_file("/file.erb", <<-TEMPLATE)
|
157
|
+
<h1><%= @response %><% @response = "NO" %><%= @response %></h1>
|
134
158
|
TEMPLATE
|
135
|
-
|
136
|
-
File.stubs(:read).with("/layout.erb").returns(tpl)
|
159
|
+
fake_file("/layout.erb", "<title><%= @response %></title><%= yield %>")
|
137
160
|
|
138
161
|
assert_equal "<title>NO</title><h1>NO</h1>\n", renderer.render(:file)
|
139
162
|
end
|
data/tiltout.gemspec
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tiltout
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 1.0.0
|
10
|
+
version: 1.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Christian Johansen
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-10-
|
18
|
+
date: 2012-10-10 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -96,6 +96,7 @@ files:
|
|
96
96
|
- Rakefile
|
97
97
|
- Readme.md
|
98
98
|
- lib/tiltout.rb
|
99
|
+
- lib/tiltout/version.rb
|
99
100
|
- test/tiltout_test.rb
|
100
101
|
- tiltout.gemspec
|
101
102
|
has_rdoc: true
|