deas 0.39.2 → 0.40.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/deas/template_engine.rb +10 -4
- data/lib/deas/template_source.rb +29 -17
- data/lib/deas/version.rb +1 -1
- data/test/support/{template.json → template-compiled3.foo.json} +0 -0
- data/test/support/template-compiled4.json.foo +1 -0
- data/test/support/template.a.json +1 -0
- data/test/unit/template_engine_tests.rb +8 -2
- data/test/unit/template_source_tests.rb +45 -50
- metadata +8 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc1b0541f8352e417fcaea600dfc199984f4ee5f
|
4
|
+
data.tar.gz: 55e89c13f41a75441fb281025d62deb3fa114080
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 066f4eac74ffd2e34af998f5622291a9d3db400381e3e3b1bbb68fd819ca00880d208460462f2b340980659b1518da9a575d4b58d67a8750b099688c6b5d7187
|
7
|
+
data.tar.gz: c4bf700bfe1196493333e38cf2bf437841d4f86258bd6e713159c5c9838db19c5d3526ab04f18619d5fd30c6e513b8369589c91e0a368ca081ba747c41d1de57
|
data/lib/deas/template_engine.rb
CHANGED
@@ -30,10 +30,16 @@ module Deas
|
|
30
30
|
class NullTemplateEngine < TemplateEngine
|
31
31
|
|
32
32
|
def render(template_name, view_handler, locals, &content)
|
33
|
-
|
34
|
-
|
33
|
+
paths = Dir.glob(self.source_path.join("#{template_name}*"))
|
34
|
+
if paths.size > 1
|
35
|
+
raise ArgumentError, "#{template_name.inspect} matches more than one " \
|
36
|
+
"file, consider using a more specific template name"
|
35
37
|
end
|
36
|
-
|
38
|
+
if paths.size < 1
|
39
|
+
raise ArgumentError, "a template file named #{template_name.inspect} " \
|
40
|
+
"does not exist"
|
41
|
+
end
|
42
|
+
File.read(paths.first)
|
37
43
|
end
|
38
44
|
|
39
45
|
def partial(template_name, locals, &content)
|
@@ -41,7 +47,7 @@ module Deas
|
|
41
47
|
end
|
42
48
|
|
43
49
|
def compile(template_name, compiled_content)
|
44
|
-
compiled_content
|
50
|
+
compiled_content # no-op, pass-thru - just return the given content
|
45
51
|
end
|
46
52
|
|
47
53
|
end
|
data/lib/deas/template_source.rb
CHANGED
@@ -5,10 +5,6 @@ module Deas
|
|
5
5
|
|
6
6
|
class TemplateSource
|
7
7
|
|
8
|
-
DISALLOWED_ENGINE_EXTS = [ 'rb' ]
|
9
|
-
|
10
|
-
DisallowedEngineExtError = Class.new(ArgumentError)
|
11
|
-
|
12
8
|
attr_reader :path, :engines
|
13
9
|
|
14
10
|
def initialize(path, logger = nil)
|
@@ -18,7 +14,12 @@ module Deas
|
|
18
14
|
'logger' => logger || Deas::NullLogger.new,
|
19
15
|
'default_template_source' => self
|
20
16
|
}
|
21
|
-
@engines = Hash.new
|
17
|
+
@engines = Hash.new do |hash, ext|
|
18
|
+
# cache null template exts so we don't repeatedly call this block for
|
19
|
+
# known null template exts
|
20
|
+
hash[ext.to_s] = Deas::NullTemplateEngine.new(@default_engine_opts)
|
21
|
+
end
|
22
|
+
@engine_exts = []
|
22
23
|
@ext_lists = Hash.new do |hash, template_name|
|
23
24
|
# An ext list is an array of non-template-name extensions that have engines
|
24
25
|
# configured. The first ext in the list is the most precedent. Its engine
|
@@ -29,16 +30,15 @@ module Deas
|
|
29
30
|
end
|
30
31
|
|
31
32
|
def engine(input_ext, engine_class, registered_opts = nil)
|
32
|
-
|
33
|
-
|
34
|
-
" engine extension."
|
35
|
-
end
|
33
|
+
@engine_exts << input_ext.to_s
|
34
|
+
|
36
35
|
engine_opts = @default_engine_opts.merge(registered_opts || {})
|
36
|
+
engine_opts['ext'] = input_ext.to_s
|
37
37
|
@engines[input_ext.to_s] = engine_class.new(engine_opts)
|
38
38
|
end
|
39
39
|
|
40
40
|
def engine_for?(ext)
|
41
|
-
@
|
41
|
+
@engine_exts.include?(ext.to_s)
|
42
42
|
end
|
43
43
|
|
44
44
|
def render(template_name, view_handler, locals, &content)
|
@@ -62,17 +62,29 @@ module Deas
|
|
62
62
|
private
|
63
63
|
|
64
64
|
def compile(name)
|
65
|
-
|
66
|
-
|
67
|
-
@engines[ext].compile(name, content)
|
65
|
+
@ext_lists[name].drop(1).inject(yield @engines[@ext_lists[name].first]) do |c, e|
|
66
|
+
@engines[e].compile(name, c)
|
68
67
|
end
|
69
68
|
end
|
70
69
|
|
71
70
|
def parse_ext_list(template_name)
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
71
|
+
paths = Dir.glob(File.join(@path, "#{template_name}*"))
|
72
|
+
if paths.size > 1
|
73
|
+
raise ArgumentError, "#{template_name.inspect} matches more than one " \
|
74
|
+
"file, consider using a more specific template name"
|
75
|
+
end
|
76
|
+
get_ext_list(paths.first.to_s)
|
77
|
+
end
|
78
|
+
|
79
|
+
def get_ext_list(path)
|
80
|
+
# get the base name of the path (file name plus extensions). Split on the
|
81
|
+
# periods and drop the first value (the file name). reverse the list b/c
|
82
|
+
# we process exts right-to-left. reject any unnecessary exts.
|
83
|
+
File.basename(path).split('.').drop(1).reverse.reject.each_with_index do |e, i|
|
84
|
+
# keep the first ext (for initial render from source) and any registered
|
85
|
+
# exts. remove any non-first non-registered exts so you don't have the
|
86
|
+
# overhead of running through the null engine for each.
|
87
|
+
i != 0 && !self.engine_for?(e)
|
76
88
|
end
|
77
89
|
end
|
78
90
|
|
data/lib/deas/version.rb
CHANGED
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
This is a json template for use in template source/engine tests.
|
@@ -0,0 +1 @@
|
|
1
|
+
This is a json template for use in template source/engine tests.
|
@@ -86,17 +86,23 @@ class Deas::TemplateEngine
|
|
86
86
|
end
|
87
87
|
|
88
88
|
should "read and return the given path in its source path on `render`" do
|
89
|
-
template = 'test/support/template'
|
89
|
+
template = ['test/support/template.a', 'test/support/template.a.json'].choice
|
90
90
|
exp = File.read(Dir.glob(subject.source_path.join("#{template}*")).first)
|
91
91
|
assert_equal exp, subject.render(template, @v, @l)
|
92
92
|
end
|
93
93
|
|
94
94
|
should "call `render` to implement its `partial` method" do
|
95
|
-
template = 'test/support/template'
|
95
|
+
template = ['test/support/template.a', 'test/support/template.a.json'].choice
|
96
96
|
exp = subject.render(template, nil, @l)
|
97
97
|
assert_equal exp, subject.partial(template, @l)
|
98
98
|
end
|
99
99
|
|
100
|
+
should "complain if given a path that matches multiple files" do
|
101
|
+
template = 'test/support/template'
|
102
|
+
assert_raises(ArgumentError){ subject.render(template, @v, @l) }
|
103
|
+
assert_raises(ArgumentError){ subject.partial(template, @l) }
|
104
|
+
end
|
105
|
+
|
100
106
|
should "complain if given a path that does not exist in its source path" do
|
101
107
|
template = '/does/not/exists'
|
102
108
|
assert_raises(ArgumentError){ subject.render(template, @v, @l) }
|
@@ -10,11 +10,6 @@ class Deas::TemplateSource
|
|
10
10
|
desc "Deas::TemplateSource"
|
11
11
|
subject{ Deas::TemplateSource }
|
12
12
|
|
13
|
-
should "disallow certain engine extensions" do
|
14
|
-
exp = [ 'rb' ]
|
15
|
-
assert_equal exp, subject::DISALLOWED_ENGINE_EXTS
|
16
|
-
end
|
17
|
-
|
18
13
|
end
|
19
14
|
|
20
15
|
class InitTests < Assert::Context
|
@@ -45,49 +40,48 @@ class Deas::TemplateSource
|
|
45
40
|
assert_kind_of Deas::NullTemplateEngine, subject.engines['test']
|
46
41
|
subject.engine 'test', @test_engine
|
47
42
|
assert_kind_of @test_engine, subject.engines['test']
|
43
|
+
|
44
|
+
assert_true subject.engine_for?('test')
|
45
|
+
assert_false subject.engine_for?(Factory.string)
|
48
46
|
end
|
49
47
|
|
50
48
|
should "register with default options" do
|
51
|
-
|
49
|
+
engine_ext = Factory.string
|
50
|
+
subject.engine engine_ext, @test_engine
|
52
51
|
exp_opts = {
|
53
52
|
'source_path' => subject.path,
|
54
53
|
'logger' => @logger,
|
55
|
-
'default_template_source' => subject
|
54
|
+
'default_template_source' => subject,
|
55
|
+
'ext' => engine_ext
|
56
56
|
}
|
57
|
-
assert_equal exp_opts, subject.engines[
|
57
|
+
assert_equal exp_opts, subject.engines[engine_ext].opts
|
58
|
+
assert_true subject.engine_for?(engine_ext)
|
58
59
|
|
59
|
-
|
60
|
+
custom_opts = { Factory.string => Factory.string }
|
61
|
+
subject.engine engine_ext, @test_engine, custom_opts
|
60
62
|
exp_opts = {
|
61
63
|
'source_path' => subject.path,
|
62
64
|
'logger' => @logger,
|
63
65
|
'default_template_source' => subject,
|
64
|
-
'
|
65
|
-
}
|
66
|
-
assert_equal exp_opts, subject.engines[
|
66
|
+
'ext' => engine_ext
|
67
|
+
}.merge(custom_opts)
|
68
|
+
assert_equal exp_opts, subject.engines[engine_ext].opts
|
69
|
+
assert_true subject.engine_for?(engine_ext)
|
67
70
|
|
68
|
-
|
71
|
+
custom_opts = {
|
69
72
|
'source_path' => 'something',
|
70
73
|
'logger' => 'another',
|
71
|
-
'default_template_source' => 'tempsource'
|
72
|
-
|
73
|
-
exp_opts = {
|
74
|
-
'source_path' => 'something',
|
75
|
-
'logger' => 'another',
|
76
|
-
'default_template_source' => 'tempsource'
|
74
|
+
'default_template_source' => 'tempsource',
|
75
|
+
'ext' => Factory.string
|
77
76
|
}
|
78
|
-
|
77
|
+
subject.engine(engine_ext, @test_engine, custom_opts)
|
78
|
+
exp_opts = custom_opts.merge('ext' => engine_ext)
|
79
|
+
assert_equal exp_opts, subject.engines[engine_ext].opts
|
80
|
+
assert_true subject.engine_for?(engine_ext)
|
79
81
|
|
80
82
|
source = Deas::TemplateSource.new(@source_path)
|
81
|
-
source.engine(
|
82
|
-
assert_instance_of Deas::NullLogger, source.engines[
|
83
|
-
end
|
84
|
-
|
85
|
-
should "complain if registering a disallowed temp" do
|
86
|
-
assert_kind_of Deas::NullTemplateEngine, subject.engines['rb']
|
87
|
-
assert_raises DisallowedEngineExtError do
|
88
|
-
subject.engine 'rb', @test_engine
|
89
|
-
end
|
90
|
-
assert_kind_of Deas::NullTemplateEngine, subject.engines['rb']
|
83
|
+
source.engine(engine_ext, @test_engine)
|
84
|
+
assert_instance_of Deas::NullLogger, source.engines[engine_ext].opts['logger']
|
91
85
|
end
|
92
86
|
|
93
87
|
end
|
@@ -112,25 +106,29 @@ class Deas::TemplateSource
|
|
112
106
|
assert_equal exp, subject.render('test_template', @v, @l)
|
113
107
|
end
|
114
108
|
|
115
|
-
should "only render the first template file matching the template name" do
|
116
|
-
# there should be 2 files called "template" in `test/support` with diff
|
117
|
-
# extensions
|
118
|
-
exp = "render-json-engine on template\n"
|
119
|
-
assert_equal exp, subject.render('template', @v, @l)
|
120
|
-
end
|
121
|
-
|
122
109
|
should "compile multiple engine outputs if template has multi-engine exts" do
|
123
110
|
exp = "render-json-engine on template-compiled1\ncompile-test-engine"
|
124
111
|
assert_equal exp, subject.render('template-compiled1', @v, @l)
|
125
112
|
|
126
113
|
exp = "render-test-engine on template-compiled2\ncompile-json-engine"
|
127
114
|
assert_equal exp, subject.render('template-compiled2', @v, @l)
|
115
|
+
|
116
|
+
exp = "render-json-engine on template-compiled3\n"
|
117
|
+
assert_equal exp, subject.render('template-compiled3', @v, @l)
|
118
|
+
|
119
|
+
exp = "This is a json template for use in template source/engine tests.\n"\
|
120
|
+
"compile-json-engine"
|
121
|
+
assert_equal exp, subject.render('template-compiled4', @v, @l)
|
122
|
+
end
|
123
|
+
|
124
|
+
should "complain if the given template name matches multiple templates" do
|
125
|
+
# there should be more than 1 file called "template" in `test/support`
|
126
|
+
# with various extensions
|
127
|
+
assert_raises(ArgumentError){ subject.render('template', @v, @l) }
|
128
128
|
end
|
129
129
|
|
130
130
|
should "use the null template engine when an engine can't be found" do
|
131
|
-
assert_raises(ArgumentError)
|
132
|
-
subject.render(Factory.string, @v, @l)
|
133
|
-
end
|
131
|
+
assert_raises(ArgumentError){ subject.render(Factory.string, @v, @l) }
|
134
132
|
end
|
135
133
|
|
136
134
|
end
|
@@ -158,13 +156,6 @@ class Deas::TemplateSource
|
|
158
156
|
assert_equal exp, subject.partial('test_template', @l)
|
159
157
|
end
|
160
158
|
|
161
|
-
should "only render the first template file matching the template name" do
|
162
|
-
# there should be 2 files called "template" in `test/support` with diff
|
163
|
-
# extensions
|
164
|
-
exp = "partial-json-engine on template\n"
|
165
|
-
assert_equal exp, subject.partial('template', @l)
|
166
|
-
end
|
167
|
-
|
168
159
|
should "compile multiple engine outputs if template has multi-engine exts" do
|
169
160
|
exp = "partial-json-engine on template-compiled1\ncompile-test-engine"
|
170
161
|
assert_equal exp, subject.partial('template-compiled1', @l)
|
@@ -173,10 +164,14 @@ class Deas::TemplateSource
|
|
173
164
|
assert_equal exp, subject.partial('template-compiled2', @l)
|
174
165
|
end
|
175
166
|
|
167
|
+
should "complain if the given template name matches multiple templates" do
|
168
|
+
# there should be more than 1 file called "template" in `test/support`
|
169
|
+
# with various extensions
|
170
|
+
assert_raises(ArgumentError){ subject.partial('template', @l) }
|
171
|
+
end
|
172
|
+
|
176
173
|
should "use the null template engine when an engine can't be found" do
|
177
|
-
assert_raises(ArgumentError)
|
178
|
-
subject.partial(Factory.string, @l)
|
179
|
-
end
|
174
|
+
assert_raises(ArgumentError){ subject.partial(Factory.string, @l) }
|
180
175
|
end
|
181
176
|
|
182
177
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deas
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.40.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kelly Redding
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2016-04-
|
13
|
+
date: 2016-04-11 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: assert
|
@@ -126,7 +126,9 @@ files:
|
|
126
126
|
- test/support/show.json
|
127
127
|
- test/support/template-compiled1.test.json
|
128
128
|
- test/support/template-compiled2.json.test
|
129
|
-
- test/support/template.json
|
129
|
+
- test/support/template-compiled3.foo.json
|
130
|
+
- test/support/template-compiled4.json.foo
|
131
|
+
- test/support/template.a.json
|
130
132
|
- test/support/template.z.erb
|
131
133
|
- test/support/test_layout1.test
|
132
134
|
- test/support/test_layout2.test
|
@@ -193,7 +195,9 @@ test_files:
|
|
193
195
|
- test/support/show.json
|
194
196
|
- test/support/template-compiled1.test.json
|
195
197
|
- test/support/template-compiled2.json.test
|
196
|
-
- test/support/template.json
|
198
|
+
- test/support/template-compiled3.foo.json
|
199
|
+
- test/support/template-compiled4.json.foo
|
200
|
+
- test/support/template.a.json
|
197
201
|
- test/support/template.z.erb
|
198
202
|
- test/support/test_layout1.test
|
199
203
|
- test/support/test_layout2.test
|