sanford 0.16.1 → 0.17.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA512:
3
- data.tar.gz: 81515e6dbc607826d511c14f4423afc408d58787cc5ba1b791d29b4d44f464ec05560c5165cdf16aa45e02233eef2e80d6d9a11458ffd63adb9daa77440e3a20
4
- metadata.gz: 20da29417dddee1768e3b64d29b945bc92826e548cdd7b38c8a85214522393d65477d02398f905e5f8a9640b3f0fc7f15f2619fa7233885ba6948c6f88752202
3
+ metadata.gz: 179edb42ed70349903ee1c61dac5d041ce8ccf2224121e952871965635ea4f26541f58468c3f7bfc6842dfae80551993dae461845024e26a89bfbbfb315f144a
4
+ data.tar.gz: dc50fc52dcabe5aaea8969654bdd1af13fc40cb1bd62ffaafc38a845b3fe1127a17b1f1adfcd2880a828a18241b869c5f4ad3782c71bcd40c5635d285e633a3a
5
5
  SHA1:
6
- data.tar.gz: d2a9c2b2c446cf36c7a09b80b1f182ac444058cf
7
- metadata.gz: a68ed5854a158d2c9d0c19cda28cb61fc51149ea
6
+ metadata.gz: da5833891f1c7570eb1cb642bbd506487eb2f3c8
7
+ data.tar.gz: 3b093248ea8ac8cd931f1cb05db52d676a34494c
@@ -13,7 +13,7 @@ module Sanford
13
13
  @logger = @opts['logger'] || Sanford::NullLogger.new
14
14
  end
15
15
 
16
- def render(path, service_handler, locals)
16
+ def render(name, service_handler, locals)
17
17
  raise NotImplementedError
18
18
  end
19
19
 
@@ -30,12 +30,17 @@ module Sanford
30
30
 
31
31
  class NullTemplateEngine < TemplateEngine
32
32
 
33
- def render(path, service_handler, locals)
34
- template_file = self.source_path.join(path).to_s
35
- unless File.exists?(template_file)
36
- raise ArgumentError, "template file `#{template_file}` does not exist"
33
+ def render(template_name, service_handler, locals)
34
+ paths = Dir.glob(self.source_path.join("#{template_name}*"))
35
+ if paths.size > 1
36
+ raise ArgumentError, "#{template_name.inspect} matches more than one " \
37
+ "file, consider using a more specific template name"
37
38
  end
38
- File.read(template_file)
39
+ if paths.size < 1
40
+ raise ArgumentError, "a template file named #{template_name.inspect} " \
41
+ "does not exist"
42
+ end
43
+ File.read(paths.first.to_s)
39
44
  end
40
45
 
41
46
  end
@@ -5,10 +5,6 @@ module Sanford
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)
@@ -17,29 +13,32 @@ module Sanford
17
13
  'source_path' => @path,
18
14
  'logger' => logger || Sanford::NullLogger.new
19
15
  }
20
- @engines = Hash.new{ |h,k| Sanford::NullTemplateEngine.new(@default_opts) }
16
+ @engines = Hash.new do |hash, ext|
17
+ # cache null template exts so we don't repeatedly call this block for
18
+ # known null template exts
19
+ hash[ext.to_s] = Sanford::NullTemplateEngine.new(@default_opts)
20
+ end
21
+ @engine_exts = []
21
22
  end
22
23
 
23
24
  def engine(input_ext, engine_class, registered_opts = nil)
24
- if DISALLOWED_ENGINE_EXTS.include?(input_ext)
25
- raise DisallowedEngineExtError, "`#{input_ext}` is disallowed as an"\
26
- " engine extension."
27
- end
25
+ @engine_exts << input_ext.to_s
28
26
  engine_opts = @default_opts.merge(registered_opts || {})
27
+ engine_opts['ext'] = input_ext.to_s
29
28
  @engines[input_ext.to_s] = engine_class.new(engine_opts)
30
29
  end
31
30
 
32
31
  def engine_for?(ext)
33
- @engines.keys.include?(ext)
32
+ @engine_exts.include?(ext.to_s)
34
33
  end
35
34
 
36
35
  def engine_for_template?(template_name)
37
36
  self.engine_for?(get_template_ext(template_name))
38
37
  end
39
38
 
40
- def render(template_path, service_handler, locals)
41
- engine = @engines[get_template_ext(template_path)]
42
- engine.render(template_path, service_handler, locals)
39
+ def render(template_name, service_handler, locals)
40
+ engine = @engines[get_template_ext(template_name)]
41
+ engine.render(template_name, service_handler, locals)
43
42
  end
44
43
 
45
44
  def ==(other_template_source)
@@ -53,14 +52,17 @@ module Sanford
53
52
 
54
53
  private
55
54
 
56
- def get_template_ext(template_path)
57
- files = Dir.glob("#{File.join(@path, template_path.to_s)}.*")
58
- files = files.reject{ |p| !@engines.keys.include?(parse_ext(p)) }
59
- parse_ext(files.first.to_s || '')
55
+ def get_template_ext(template_name)
56
+ files = Dir.glob("#{File.join(@path, template_name.to_s)}*")
57
+ if files.size > 1
58
+ raise ArgumentError, "#{template_name.inspect} matches more than one " \
59
+ "file, consider using a more specific template name"
60
+ end
61
+ parse_ext(files.first.to_s)
60
62
  end
61
63
 
62
- def parse_ext(template_path)
63
- File.extname(template_path)[1..-1]
64
+ def parse_ext(template_name)
65
+ File.extname(template_name)[1..-1]
64
66
  end
65
67
 
66
68
  end
@@ -1,3 +1,3 @@
1
1
  module Sanford
2
- VERSION = "0.16.1"
2
+ VERSION = "0.17.0"
3
3
  end
@@ -0,0 +1 @@
1
+ This is an erb template for use in template engine tests.
@@ -70,11 +70,18 @@ class Sanford::TemplateEngine
70
70
  end
71
71
 
72
72
  should "read and return the given path in its source path on `render" do
73
- exists_file = 'test/support/template.json'
74
- exp = File.read(subject.source_path.join(exists_file).to_s)
73
+ exists_file = ['test/support/template', 'test/support/template.erb'].choice
74
+ exp = File.read(Dir.glob("#{subject.source_path.join(exists_file)}*").first)
75
75
  assert_equal exp, subject.render(exists_file, @service_handler, @locals)
76
76
  end
77
77
 
78
+ should "complain if given a path that matches multiple files" do
79
+ conflict_file = 'test/support/conflict_template'
80
+ assert_raises ArgumentError do
81
+ subject.render(conflict_file, @service_handler, @locals)
82
+ end
83
+ end
84
+
78
85
  should "complain if given a path that does not exist in its source path" do
79
86
  no_exists_file = '/does/not/exists'
80
87
  assert_raises ArgumentError do
@@ -10,11 +10,6 @@ class Sanford::TemplateSource
10
10
  desc "Sanford::TemplateSource"
11
11
  subject{ Sanford::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,45 +40,45 @@ class Sanford::TemplateSource
45
40
  assert_kind_of Sanford::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
- subject.engine 'test', @test_engine
49
+ engine_ext = Factory.string
50
+ subject.engine engine_ext, @test_engine
52
51
  exp_opts = {
53
52
  'source_path' => subject.path,
54
- 'logger' => @logger
53
+ 'logger' => @logger,
54
+ 'ext' => engine_ext
55
55
  }
56
- assert_equal exp_opts, subject.engines['test'].opts
56
+ assert_equal exp_opts, subject.engines[engine_ext].opts
57
+ assert_true subject.engine_for?(engine_ext)
57
58
 
58
59
  source = Sanford::TemplateSource.new(@source_path)
59
- source.engine 'test', @test_engine
60
- assert_kind_of Sanford::NullLogger, source.engines['test'].opts['logger']
60
+ source.engine engine_ext, @test_engine
61
+ assert_kind_of Sanford::NullLogger, source.engines[engine_ext].opts['logger']
61
62
 
62
- subject.engine 'test', @test_engine, 'an' => 'opt'
63
+ custom_opts = { Factory.string => Factory.string }
64
+ subject.engine engine_ext, @test_engine, custom_opts
63
65
  exp_opts = {
64
66
  'source_path' => subject.path,
65
67
  'logger' => @logger,
66
- 'an' => 'opt'
67
- }
68
- assert_equal exp_opts, subject.engines['test'].opts
69
-
70
- subject.engine('test', @test_engine, {
71
- 'source_path' => 'something',
72
- 'logger' => 'another'
73
- })
74
- exp_opts = {
75
- 'source_path' => 'something',
76
- 'logger' => 'another'
68
+ 'ext' => engine_ext
69
+ }.merge(custom_opts)
70
+ assert_equal exp_opts, subject.engines[engine_ext].opts
71
+ assert_true subject.engine_for?(engine_ext)
72
+
73
+ custom_opts = {
74
+ 'source_path' => Factory.string,
75
+ 'logger' => Factory.string,
76
+ 'ext' => Factory.string
77
77
  }
78
- assert_equal exp_opts, subject.engines['test'].opts
79
- end
80
-
81
- should "complain if registering a disallowed temp" do
82
- assert_kind_of Sanford::NullTemplateEngine, subject.engines['rb']
83
- assert_raises DisallowedEngineExtError do
84
- subject.engine 'rb', @test_engine
85
- end
86
- assert_kind_of Sanford::NullTemplateEngine, subject.engines['rb']
78
+ subject.engine(engine_ext, @test_engine, custom_opts)
79
+ exp_opts = custom_opts.merge('ext' => engine_ext)
80
+ assert_equal exp_opts, subject.engines[engine_ext].opts
81
+ assert_true subject.engine_for?(engine_ext)
87
82
  end
88
83
 
89
84
  should "know if it has an engine registered for a given template name" do
@@ -110,13 +105,9 @@ class Sanford::TemplateSource
110
105
  locals = { :something => Factory.string }
111
106
  result = subject.render('test_template', TestServiceHandler, locals)
112
107
  assert_equal 'test-engine', result
113
- end
114
108
 
115
- should "only try rendering template files its has engines for" do
116
- # there should be 2 files called "template" in `test/support` with diff
117
- # extensions
118
- result = subject.render('template', TestServiceHandler, {})
119
- assert_equal 'json-engine', result
109
+ result = subject.render('test_template.test', TestServiceHandler, locals)
110
+ assert_equal 'test-engine', result
120
111
  end
121
112
 
122
113
  should "use the null template engine when an engine can't be found" do
@@ -125,6 +116,14 @@ class Sanford::TemplateSource
125
116
  end
126
117
  end
127
118
 
119
+ should "complain if the given template name matches multiple templates" do
120
+ # there should be 2 files called "conflict_template" in `test/support`
121
+ # with diff extensions
122
+ assert_raises(ArgumentError) do
123
+ subject.render('conflict_template', TestServiceHandler, {})
124
+ end
125
+ end
126
+
128
127
  end
129
128
 
130
129
  class NullTemplateSourceTests < Assert::Context
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sanford
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.1
4
+ version: 0.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Collin Redding
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2016-03-14 00:00:00 Z
13
+ date: 2016-04-11 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: assert
@@ -110,11 +110,12 @@ files:
110
110
  - test/support/config.sanford
111
111
  - test/support/config_invalid_run.sanford
112
112
  - test/support/config_no_run.sanford
113
+ - test/support/conflict_template.erb
114
+ - test/support/conflict_template.json
113
115
  - test/support/factory.rb
114
116
  - test/support/fake_server_connection.rb
115
117
  - test/support/pid_file_spy.rb
116
118
  - test/support/template.erb
117
- - test/support/template.json
118
119
  - test/support/test_disallowed_template.rb
119
120
  - test/support/test_template.test
120
121
  - test/system/server_tests.rb
@@ -171,11 +172,12 @@ test_files:
171
172
  - test/support/config.sanford
172
173
  - test/support/config_invalid_run.sanford
173
174
  - test/support/config_no_run.sanford
175
+ - test/support/conflict_template.erb
176
+ - test/support/conflict_template.json
174
177
  - test/support/factory.rb
175
178
  - test/support/fake_server_connection.rb
176
179
  - test/support/pid_file_spy.rb
177
180
  - test/support/template.erb
178
- - test/support/template.json
179
181
  - test/support/test_disallowed_template.rb
180
182
  - test/support/test_template.test
181
183
  - test/system/server_tests.rb