deas-erubis 0.1.0 → 0.2.0
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/lib/deas-erubis/source.rb +27 -66
- data/lib/deas-erubis/version.rb +1 -1
- data/test/unit/source_tests.rb +53 -70
- data/test/unit/template_engine_tests.rb +3 -3
- metadata +4 -4
data/lib/deas-erubis/source.rb
CHANGED
@@ -8,73 +8,33 @@ module Deas::Erubis
|
|
8
8
|
class Source
|
9
9
|
|
10
10
|
EXT = '.erb'.freeze
|
11
|
-
CACHE_EXT = '.cache'.freeze
|
12
11
|
BUFVAR_NAME = '@_erb_buf'.freeze
|
13
12
|
DEFAULT_ERUBY = ::Erubis::Eruby
|
14
13
|
|
15
|
-
attr_reader :root, :
|
14
|
+
attr_reader :root, :eruby_class, :cache, :context_class
|
16
15
|
|
17
16
|
def initialize(root, opts)
|
18
|
-
@root
|
19
|
-
@eruby_class
|
20
|
-
|
21
|
-
should_cache = !!opts[:cache]
|
22
|
-
@cache_root = opts[:cache] == true ? @root : Pathname.new(opts[:cache].to_s)
|
23
|
-
@cache_root.mkpath if should_cache && !@cache_root.exist?
|
24
|
-
|
25
|
-
if should_cache
|
26
|
-
# use `load_file` to lookup and cache templates (faster renders)
|
27
|
-
if @cache_root == @root
|
28
|
-
# use the `load_file` default and don't bother with looking up, setting,
|
29
|
-
# and making sure the cache file path exists - by default `load_file`
|
30
|
-
# caches alongside the source with the `CACHE_EXT` appended.
|
31
|
-
add_meta_eruby_method do |file_name|
|
32
|
-
@eruby_class.load_file(source_file_path(file_name), {
|
33
|
-
:bufvar => BUFVAR_NAME
|
34
|
-
})
|
35
|
-
end
|
36
|
-
else
|
37
|
-
# lookup and ensure the custom cache location exists (more expensive)
|
38
|
-
add_meta_eruby_method do |file_name|
|
39
|
-
@eruby_class.load_file(source_file_path(file_name), {
|
40
|
-
:bufvar => BUFVAR_NAME,
|
41
|
-
:cachename => cache_file_path(file_name)
|
42
|
-
})
|
43
|
-
end
|
44
|
-
end
|
45
|
-
else
|
46
|
-
# don't cache template files (slower renders, but no cache files created)
|
47
|
-
add_meta_eruby_method do |file_name|
|
48
|
-
filename = source_file_path(file_name).to_s
|
49
|
-
template = File.send(File.respond_to?(:binread) ? :binread : :read, filename)
|
50
|
-
@eruby_class.new(template, {
|
51
|
-
:bufvar => BUFVAR_NAME,
|
52
|
-
:filename => filename
|
53
|
-
})
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
@deas_source = opts[:deas_source]
|
17
|
+
@root = Pathname.new(root.to_s)
|
18
|
+
@eruby_class = opts[:eruby] || DEFAULT_ERUBY
|
19
|
+
@cache = opts[:cache] ? Hash.new : NullCache.new
|
58
20
|
@context_class = build_context_class(opts)
|
59
|
-
end
|
60
21
|
|
61
|
-
|
62
|
-
# should be overridden by a metaclass equivalent on init
|
63
|
-
# the implementation changes whether you are caching templates or not
|
64
|
-
# and the goal here is to not add a bunch of conditional overhead as this
|
65
|
-
# will be called on every render
|
66
|
-
raise NotImplementedError
|
22
|
+
@deas_source = opts[:deas_source]
|
67
23
|
end
|
68
24
|
|
69
25
|
def render(file_name, locals, &content)
|
70
|
-
|
26
|
+
load(file_name).evaluate(@context_class.new(@deas_source, locals), &content)
|
71
27
|
end
|
72
28
|
|
73
|
-
def compile(
|
29
|
+
def compile(filename, content)
|
30
|
+
eruby(filename, content).evaluate(@context_class.new(@deas_source, {}))
|
31
|
+
end
|
32
|
+
|
33
|
+
def eruby(filename, content)
|
74
34
|
@eruby_class.new(content, {
|
75
35
|
:bufvar => BUFVAR_NAME,
|
76
|
-
:filename =>
|
77
|
-
})
|
36
|
+
:filename => filename
|
37
|
+
})
|
78
38
|
end
|
79
39
|
|
80
40
|
def inspect
|
@@ -85,21 +45,16 @@ module Deas::Erubis
|
|
85
45
|
|
86
46
|
private
|
87
47
|
|
88
|
-
def
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
path.dirname.mkpath if !path.dirname.exist?
|
95
|
-
end.to_s
|
48
|
+
def load(file_name)
|
49
|
+
@cache[file_name] ||= begin
|
50
|
+
filename = source_file_path(file_name).to_s
|
51
|
+
content = File.send(File.respond_to?(:binread) ? :binread : :read, filename)
|
52
|
+
eruby(filename, content)
|
53
|
+
end
|
96
54
|
end
|
97
55
|
|
98
|
-
def
|
99
|
-
|
100
|
-
metaclass.class_eval do
|
101
|
-
define_method(:eruby, &method)
|
102
|
-
end
|
56
|
+
def source_file_path(file_name)
|
57
|
+
self.root.join("#{file_name}#{EXT}").to_s
|
103
58
|
end
|
104
59
|
|
105
60
|
def build_context_class(opts)
|
@@ -121,6 +76,12 @@ module Deas::Erubis
|
|
121
76
|
end
|
122
77
|
end
|
123
78
|
|
79
|
+
class NullCache
|
80
|
+
def [](file_name); end
|
81
|
+
def []=(file_name, value); end
|
82
|
+
def keys; []; end
|
83
|
+
end
|
84
|
+
|
124
85
|
end
|
125
86
|
|
126
87
|
class DefaultSource < Source
|
data/lib/deas-erubis/version.rb
CHANGED
data/test/unit/source_tests.rb
CHANGED
@@ -12,9 +12,8 @@ class Deas::Erubis::Source
|
|
12
12
|
end
|
13
13
|
subject{ @source_class }
|
14
14
|
|
15
|
-
should "know its
|
15
|
+
should "know its extension" do
|
16
16
|
assert_equal '.erb', subject::EXT
|
17
|
-
assert_equal '.cache', subject::CACHE_EXT
|
18
17
|
end
|
19
18
|
|
20
19
|
should "know the bufvar name to use" do
|
@@ -35,8 +34,8 @@ class Deas::Erubis::Source
|
|
35
34
|
end
|
36
35
|
subject{ @source }
|
37
36
|
|
38
|
-
should have_readers :root, :
|
39
|
-
should have_imeths :
|
37
|
+
should have_readers :root, :eruby_class, :cache, :context_class
|
38
|
+
should have_imeths :render, :compile, :eruby
|
40
39
|
|
41
40
|
should "know its root" do
|
42
41
|
assert_equal @root, subject.root.to_s
|
@@ -53,32 +52,23 @@ class Deas::Erubis::Source
|
|
53
52
|
end
|
54
53
|
|
55
54
|
should "build eruby instances for a given template file" do
|
56
|
-
assert_kind_of subject.eruby_class, subject.eruby('basic')
|
55
|
+
assert_kind_of subject.eruby_class, subject.eruby('basic', Factory.string)
|
57
56
|
end
|
58
57
|
|
59
58
|
should "build its eruby instances with the correct bufvar name" do
|
59
|
+
eruby = subject.eruby('basic', Factory.string)
|
60
|
+
|
60
61
|
exp = Deas::Erubis::Source::BUFVAR_NAME
|
61
|
-
assert_equal exp,
|
62
|
+
assert_equal exp, eruby.instance_variable_get('@bufvar')
|
62
63
|
end
|
63
64
|
|
64
|
-
should "
|
65
|
-
|
65
|
+
should "not cache templates by default" do
|
66
|
+
assert_kind_of NullCache, subject.cache
|
66
67
|
end
|
67
68
|
|
68
|
-
should "
|
69
|
+
should "cache templates if the :cache opt is `true`" do
|
69
70
|
source = @source_class.new(@root, :cache => true)
|
70
|
-
|
71
|
-
end
|
72
|
-
|
73
|
-
should "optionally use a custom cache root" do
|
74
|
-
source = @source_class.new(@root, :cache => TEMPLATE_CACHE_ROOT)
|
75
|
-
assert_equal TEMPLATE_CACHE_ROOT.to_s, source.cache_root.to_s
|
76
|
-
end
|
77
|
-
|
78
|
-
should "create the cache root if it doesn't exist already" do
|
79
|
-
FileUtils.rm_rf(TEMPLATE_CACHE_ROOT) if TEMPLATE_CACHE_ROOT.exist?
|
80
|
-
source = @source_class.new(@root, :cache => TEMPLATE_CACHE_ROOT)
|
81
|
-
assert_file_exists TEMPLATE_CACHE_ROOT.to_s
|
71
|
+
assert_kind_of Hash, source.cache
|
82
72
|
end
|
83
73
|
|
84
74
|
should "know its context class" do
|
@@ -121,27 +111,15 @@ class Deas::Erubis::Source
|
|
121
111
|
|
122
112
|
end
|
123
113
|
|
124
|
-
class
|
114
|
+
class RenderTests < InitTests
|
115
|
+
desc "`render` method"
|
125
116
|
setup do
|
117
|
+
@file_name = "basic"
|
126
118
|
@file_locals = {
|
127
119
|
'name' => Factory.string,
|
128
120
|
'local1' => Factory.integer
|
129
121
|
}
|
130
122
|
end
|
131
|
-
teardown do
|
132
|
-
root = TEMPLATE_ROOT.join("*#{@source_class::CACHE_EXT}").to_s
|
133
|
-
Dir.glob(root).each{ |f| FileUtils.rm_f(f) }
|
134
|
-
root = TEMPLATE_CACHE_ROOT.join("*#{@source_class::CACHE_EXT}").to_s
|
135
|
-
Dir.glob(root).each{ |f| FileUtils.rm_f(f) }
|
136
|
-
end
|
137
|
-
|
138
|
-
end
|
139
|
-
|
140
|
-
class RenderTests < RenderSetupTests
|
141
|
-
desc "`render` method"
|
142
|
-
setup do
|
143
|
-
@file_name = "basic"
|
144
|
-
end
|
145
123
|
|
146
124
|
should "render a template for the given file name and return its data" do
|
147
125
|
exp = Factory.basic_erb_rendered(@file_locals)
|
@@ -162,36 +140,18 @@ class Deas::Erubis::Source
|
|
162
140
|
|
163
141
|
end
|
164
142
|
|
165
|
-
class
|
143
|
+
class RenderCacheTests < RenderTests
|
166
144
|
desc "when caching is enabled"
|
167
145
|
setup do
|
168
146
|
@source = @source_class.new(@root, :cache => true)
|
169
147
|
end
|
170
148
|
|
171
|
-
should "cache
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
assert_not_file_exists cache_file
|
176
|
-
subject.render(@file_name, @file_locals)
|
177
|
-
assert_file_exists cache_file
|
178
|
-
end
|
179
|
-
|
180
|
-
end
|
181
|
-
|
182
|
-
class RenderCustomCacheTests < RenderTests
|
183
|
-
desc "when caching is enabled on a custom cache root"
|
184
|
-
setup do
|
185
|
-
@source = @source_class.new(@root, :cache => TEMPLATE_CACHE_ROOT)
|
186
|
-
end
|
187
|
-
|
188
|
-
should "cache templates in the cache root" do
|
189
|
-
f = "#{@file_name}#{@source_class::EXT}#{@source_class::CACHE_EXT}"
|
190
|
-
cache_file = TEMPLATE_CACHE_ROOT.join(f)
|
149
|
+
should "cache template eruby instances by their file name" do
|
150
|
+
exp = Factory.basic_erb_rendered(@file_locals)
|
151
|
+
assert_equal exp, @source.render(@file_name, @file_locals)
|
191
152
|
|
192
|
-
|
193
|
-
|
194
|
-
assert_file_exists cache_file
|
153
|
+
assert_equal [@file_name], @source.cache.keys
|
154
|
+
assert_kind_of @source.eruby_class, @source.cache[@file_name]
|
195
155
|
end
|
196
156
|
|
197
157
|
end
|
@@ -202,13 +162,11 @@ class Deas::Erubis::Source
|
|
202
162
|
@source = @source_class.new(@root, :cache => false)
|
203
163
|
end
|
204
164
|
|
205
|
-
should "not cache
|
206
|
-
|
207
|
-
|
165
|
+
should "not cache template eruby instances" do
|
166
|
+
exp = Factory.basic_erb_rendered(@file_locals)
|
167
|
+
assert_equal exp, @source.render(@file_name, @file_locals)
|
208
168
|
|
209
|
-
|
210
|
-
subject.render(@file_name, @file_locals)
|
211
|
-
assert_not_file_exists cache_file
|
169
|
+
assert_equal [], @source.cache.keys
|
212
170
|
end
|
213
171
|
|
214
172
|
end
|
@@ -227,7 +185,7 @@ class Deas::Erubis::Source
|
|
227
185
|
|
228
186
|
end
|
229
187
|
|
230
|
-
class CompileTests <
|
188
|
+
class CompileTests < InitTests
|
231
189
|
desc "`compile` method"
|
232
190
|
|
233
191
|
should "compile raw content file name and return its data" do
|
@@ -238,15 +196,40 @@ class Deas::Erubis::Source
|
|
238
196
|
|
239
197
|
end
|
240
198
|
|
241
|
-
class
|
242
|
-
desc "
|
199
|
+
class NullCacheTests < UnitTests
|
200
|
+
desc "NullCache"
|
201
|
+
setup do
|
202
|
+
@cache = NullCache.new
|
203
|
+
end
|
204
|
+
subject{ @cache }
|
205
|
+
|
206
|
+
should have_imeths :[], :[]=, :keys
|
207
|
+
|
208
|
+
should "take a file name and return nothing on index" do
|
209
|
+
assert_nil subject[Factory.path]
|
210
|
+
end
|
211
|
+
|
212
|
+
should "take a file name and value and do nothing on index write" do
|
213
|
+
assert_nothing_raised do
|
214
|
+
subject[Factory.path] = Factory.string
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
should "always have empty keys" do
|
219
|
+
assert_equal [], subject.keys
|
220
|
+
end
|
221
|
+
|
222
|
+
end
|
223
|
+
|
224
|
+
class DefaultSourceTests < Assert::Context
|
225
|
+
desc "Deas::Erubis::DefaultSource"
|
243
226
|
setup do
|
244
227
|
@source = Deas::Erubis::DefaultSource.new
|
245
228
|
end
|
246
229
|
subject{ @source }
|
247
230
|
|
248
231
|
should "be a Source" do
|
249
|
-
assert_kind_of
|
232
|
+
assert_kind_of Deas::Erubis::Source, subject
|
250
233
|
end
|
251
234
|
|
252
235
|
should "use `/` as its root" do
|
@@ -34,9 +34,9 @@ class Deas::Erubis::TemplateEngine
|
|
34
34
|
assert_equal custom_eruby, engine.erb_source.eruby_class
|
35
35
|
end
|
36
36
|
|
37
|
-
should "
|
38
|
-
engine = Deas::Erubis::TemplateEngine.new('cache' =>
|
39
|
-
|
37
|
+
should "pass any given cache option to its source" do
|
38
|
+
engine = Deas::Erubis::TemplateEngine.new('cache' => true)
|
39
|
+
assert_kind_of Hash, engine.erb_source.cache
|
40
40
|
end
|
41
41
|
|
42
42
|
should "pass any given deas template source to its source" do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deas-erubis
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kelly Redding
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2015-01-
|
19
|
+
date: 2015-01-14 00:00:00 Z
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
22
|
requirement: &id001 !ruby/object:Gem::Requirement
|