sanford 0.19.0 → 0.19.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- data.tar.gz: be152ddfab1bc45693e6d3f7770fb2dcac53e7ac
4
- metadata.gz: 550b49e83a11d06488fbdddcec205ef4adb7df58
5
2
  SHA512:
6
- data.tar.gz: 5ffb0877eaf187acc36e3a681478d90f867ab5e5156074ab56427c03df8f35eea17be19071d5bffbef79f1ed1784bfa6f6fbcfc5c12c8c6b91f5e02580cbcdd5
7
- metadata.gz: 688a1b30785ba2d731e93f7b4ca513a6b8797654177b3414999f28330d9b78efd7a555b4f3e2314bb6ae7f0112d6146f346170aea3cb2d7ad79a7340337d74b2
3
+ data.tar.gz: 37d9c67d1935a3a4de78f3fae6fd4f01127d5cea1e516c3e312f0dd4a61a33e4d4bf7296b3e73d67823113938d6aecb2177bb1044180667f92f9d44b6e7a73f6
4
+ metadata.gz: b5b32bfaf14577c18ce4da0506753aae3ebff7f4fee0e858a1c43e5b090d59117a6f713854cd7f7ed51a543b59a18b6d1456a510da191bdc24d85d3a085399f6
5
+ SHA1:
6
+ data.tar.gz: f630d9098164756eec0de60f38a03567c357be3b
7
+ metadata.gz: ed54304eab935901a6f8882e713dd58a2878659b
data/Gemfile CHANGED
@@ -2,5 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
- gem 'pry', "~> 0.9.0"
6
- gem 'bson_ext', '~>1.7'
5
+ gem 'pry', "~> 0.9.0"
6
+ gem 'bson_ext', "~> 1.7"
@@ -68,6 +68,10 @@ module Sanford
68
68
  self.data(self.template_source.render(path, self.handler, locals || {}))
69
69
  end
70
70
 
71
+ def partial(path, locals = nil)
72
+ self.data(self.template_source.partial(path, locals || {}))
73
+ end
74
+
71
75
  end
72
76
 
73
77
  end
@@ -62,10 +62,11 @@ module Sanford
62
62
  def params; @sanford_runner.params; end
63
63
 
64
64
  # response
65
- def status(*args); @sanford_runner.status(*args); end
66
- def data(*args); @sanford_runner.data(*args); end
67
- def halt(*args); @sanford_runner.halt(*args); end
68
- def render(*args); @sanford_runner.render(*args); end
65
+ def status(*args); @sanford_runner.status(*args); end
66
+ def data(*args); @sanford_runner.data(*args); end
67
+ def halt(*args); @sanford_runner.halt(*args); end
68
+ def render(*args); @sanford_runner.render(*args); end
69
+ def partial(*args); @sanford_runner.partial(*args); end
69
70
 
70
71
  end
71
72
 
@@ -17,6 +17,10 @@ module Sanford
17
17
  raise NotImplementedError
18
18
  end
19
19
 
20
+ def partial(name, locals)
21
+ raise NotImplementedError
22
+ end
23
+
20
24
  def ==(other_engine)
21
25
  if other_engine.kind_of?(TemplateEngine)
22
26
  self.source_path == other_engine.source_path &&
@@ -31,6 +35,10 @@ module Sanford
31
35
  class NullTemplateEngine < TemplateEngine
32
36
 
33
37
  def render(template_name, service_handler, locals)
38
+ self.partial(template_name, locals)
39
+ end
40
+
41
+ def partial(template_name, locals)
34
42
  paths = Dir.glob(self.source_path.join("#{template_name}*"))
35
43
  if paths.size > 1
36
44
  raise ArgumentError, "#{template_name.inspect} matches more than one " \
@@ -41,6 +41,11 @@ module Sanford
41
41
  engine.render(template_name, service_handler, locals)
42
42
  end
43
43
 
44
+ def partial(template_name, locals)
45
+ engine = @engines[get_template_ext(template_name)]
46
+ engine.partial(template_name, locals)
47
+ end
48
+
44
49
  def ==(other_template_source)
45
50
  if other_template_source.kind_of?(TemplateSource)
46
51
  self.path == other_template_source.path &&
@@ -1,3 +1,3 @@
1
1
  module Sanford
2
- VERSION = "0.19.0"
2
+ VERSION = "0.19.1"
3
3
  end
@@ -18,7 +18,7 @@ Gem::Specification.new do |gem|
18
18
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
19
  gem.require_paths = ["lib"]
20
20
 
21
- gem.add_development_dependency("assert", ["~> 2.16.1"])
21
+ gem.add_development_dependency("assert", ["~> 2.16.3"])
22
22
 
23
23
  gem.add_dependency("dat-tcp", ["~> 0.8.2"])
24
24
  gem.add_dependency("much-plugin", ["~> 0.2.0"])
@@ -21,6 +21,14 @@ class AppERBEngine < Sanford::TemplateEngine
21
21
  b = RenderScope.new(service_handler).get_binding
22
22
  ERB.new(File.read(full_path)).result(b)
23
23
  end
24
+
25
+ def partial(path, locals)
26
+ require 'erb'
27
+ full_path = ROOT_PATH.join("test/support/#{path}.erb")
28
+
29
+ b = RenderScope.new(nil).get_binding
30
+ ERB.new(File.read(full_path)).result(b)
31
+ end
24
32
  end
25
33
 
26
34
  class AppServer
@@ -38,12 +46,13 @@ class AppServer
38
46
  router do
39
47
  service_handler_ns 'AppHandlers'
40
48
 
41
- service 'echo', 'Echo'
42
- service 'raise', 'Raise'
43
- service 'bad_response', 'BadResponse'
44
- service 'template', 'Template'
45
- service 'halt', 'Halt'
46
- service 'custom_error', 'CustomError'
49
+ service 'echo', 'Echo'
50
+ service 'raise', 'Raise'
51
+ service 'bad_response', 'BadResponse'
52
+ service 'render_template', 'RenderTemplate'
53
+ service 'partial_template', 'PartialTemplate'
54
+ service 'halt', 'Halt'
55
+ service 'custom_error', 'CustomError'
47
56
  end
48
57
 
49
58
  Sanford::TemplateSource.new(ROOT_PATH.join('test/support').to_s).tap do |s|
@@ -96,11 +105,17 @@ module AppHandlers
96
105
  def init!
97
106
  @message = params['message']
98
107
  end
99
-
108
+ end
109
+ class RenderTemplate < Template
100
110
  def run!
101
111
  render "template"
102
112
  end
103
113
  end
114
+ class PartialTemplate < Template
115
+ def run!
116
+ partial "template"
117
+ end
118
+ end
104
119
 
105
120
  class Halt
106
121
  include Sanford::ServiceHandler
@@ -1 +1 @@
1
- ERB Template Message: <%= view.message %>
1
+ ERB Template Message: <%= view.nil? ? '' : view.message %>
@@ -166,11 +166,11 @@ module Sanford::Server
166
166
 
167
167
  end
168
168
 
169
- class TemplateTests < RunningAppServerTests
169
+ class RenderTemplateTests < RunningAppServerTests
170
170
  desc "calling a service that renders a template"
171
171
  setup do
172
172
  @message = Factory.text
173
- @client.set_request('template', :message => @message)
173
+ @client.set_request('render_template', :message => @message)
174
174
  @response = @client.call
175
175
  end
176
176
 
@@ -182,6 +182,22 @@ module Sanford::Server
182
182
 
183
183
  end
184
184
 
185
+ class PartialTemplateTests < RunningAppServerTests
186
+ desc "calling a service that renders a partial template"
187
+ setup do
188
+ @message = Factory.text
189
+ @client.set_request('partial_template', :message => @message)
190
+ @response = @client.call
191
+ end
192
+
193
+ should "return a success response with the rendered data" do
194
+ assert_equal 200, subject.code
195
+ assert_nil subject.status.message
196
+ assert_equal "ERB Template Message: \n", subject.data
197
+ end
198
+
199
+ end
200
+
185
201
  class KeepAliveTests < RunningAppServerTests
186
202
  desc "receiving a keep-alive connection"
187
203
  setup do
@@ -57,11 +57,11 @@ module Sanford::ServiceHandler
57
57
 
58
58
  end
59
59
 
60
- class TemplateHandlerTests < SystemTests
61
- desc "AppHandlers::Template"
60
+ class RenderTemplateHandlerTests < SystemTests
61
+ desc "AppHandlers::RenderTemplate"
62
62
  setup do
63
63
  @params = { 'message' => Factory.text }
64
- @runner = test_runner(AppHandlers::Template, {
64
+ @runner = test_runner(AppHandlers::RenderTemplate, {
65
65
  :params => @params,
66
66
  :template_source => AppServer.config.template_source
67
67
  })
@@ -78,12 +78,41 @@ module Sanford::ServiceHandler
78
78
  should "return a 200 response and render the template when run" do
79
79
  response = @runner.run
80
80
  assert_equal 200, response.code
81
+
81
82
  exp = "ERB Template Message: #{@params['message']}\n"
82
83
  assert_equal exp, response.data
83
84
  end
84
85
 
85
86
  end
86
87
 
88
+ class PartialTemplateHandlerTests < SystemTests
89
+ desc "AppHandlers::PartialTemplate"
90
+ setup do
91
+ @params = { 'message' => Factory.text }
92
+ @runner = test_runner(AppHandlers::PartialTemplate, {
93
+ :params => @params,
94
+ :template_source => AppServer.config.template_source
95
+ })
96
+ @handler = @runner.handler
97
+ end
98
+ subject{ @handler }
99
+
100
+ should have_readers :message
101
+
102
+ should "know its message" do
103
+ assert_equal @params['message'], subject.message
104
+ end
105
+
106
+ should "return a 200 response and render the template when run" do
107
+ response = @runner.run
108
+ assert_equal 200, response.code
109
+
110
+ exp = "ERB Template Message: \n"
111
+ assert_equal exp, response.data
112
+ end
113
+
114
+ end
115
+
87
116
  class HaltHandlerTests < SystemTests
88
117
  desc "AppHandlers::Halt"
89
118
  setup do
@@ -41,8 +41,8 @@ class Sanford::Runner
41
41
  should have_readers :handler_class, :handler
42
42
  should have_readers :logger, :router, :template_source
43
43
  should have_readers :request, :params
44
- should have_imeths :run, :to_response
45
- should have_imeths :halt
44
+ should have_imeths :run, :to_response, :status, :data
45
+ should have_imeths :halt, :render, :partial
46
46
 
47
47
  should "know its handler class and handler" do
48
48
  assert_equal @handler_class, subject.handler_class
@@ -180,19 +180,28 @@ class Sanford::Runner
180
180
 
181
181
  end
182
182
 
183
- class RenderTests < InitTests
184
- desc "the `render` method"
183
+ class RenderPartialTests < InitTests
185
184
  setup do
186
185
  @template_name = Factory.path
187
- @locals = { Factory.string => Factory.string }
188
- data = @data = Factory.text
189
- @render_called_with = nil
186
+ @locals = { Factory.string => Factory.string }
187
+ @data = Factory.text
188
+
190
189
  @source = @runner.template_source
190
+ end
191
+
192
+ end
193
+
194
+ class RenderTests < RenderPartialTests
195
+ desc "the `render` method"
196
+ setup do
197
+ data = @data
198
+ @render_called_with = nil
191
199
  Assert.stub(@source, :render){ |*args| @render_called_with = args; data }
192
200
  end
193
201
 
194
202
  should "call to the template source's render method and set the return value as data" do
195
203
  subject.render(@template_name, @locals)
204
+
196
205
  exp = [@template_name, subject.handler, @locals]
197
206
  assert_equal exp, @render_called_with
198
207
  assert_equal @data, subject.data
@@ -200,12 +209,37 @@ class Sanford::Runner
200
209
 
201
210
  should "default the locals if none given" do
202
211
  subject.render(@template_name)
212
+
203
213
  exp = [@template_name, subject.handler, {}]
204
214
  assert_equal exp, @render_called_with
205
215
  end
206
216
 
207
217
  end
208
218
 
219
+ class PartialTests < RenderPartialTests
220
+ desc "the `partial` method"
221
+ setup do
222
+ data = @data
223
+ @partial_called_with = nil
224
+ Assert.stub(@source, :partial){ |*args| @partial_called_with = args; data }
225
+ end
226
+
227
+ should "call to the template source's partial method and set the return value as data" do
228
+ subject.partial(@template_name, @locals)
229
+
230
+ exp = [@template_name, @locals]
231
+ assert_equal exp, @partial_called_with
232
+ assert_equal @data, subject.data
233
+ end
234
+
235
+ should "default the locals if none given" do
236
+ subject.partial(@template_name)
237
+ exp = [@template_name, {}]
238
+ assert_equal exp, @partial_called_with
239
+ end
240
+
241
+ end
242
+
209
243
  class TestServiceHandler
210
244
  include Sanford::ServiceHandler
211
245
 
@@ -255,6 +255,14 @@ module Sanford::ServiceHandler
255
255
  assert_equal exp_args, @meth_args
256
256
  end
257
257
 
258
+ should "call to the runner for its partial helper" do
259
+ capture_runner_meth_args_for(:partial)
260
+ exp_args = @args
261
+ subject.instance_eval{ partial(*exp_args) }
262
+
263
+ assert_equal exp_args, @meth_args
264
+ end
265
+
258
266
  private
259
267
 
260
268
  def stub_runner_with_something_for(meth)
@@ -19,7 +19,7 @@ class Sanford::TemplateEngine
19
19
  subject{ @engine }
20
20
 
21
21
  should have_readers :source_path, :logger, :opts
22
- should have_imeths :render
22
+ should have_imeths :render, :partial
23
23
 
24
24
  should "default its source path" do
25
25
  assert_equal Pathname.new(nil.to_s), subject.source_path
@@ -56,6 +56,12 @@ class Sanford::TemplateEngine
56
56
  end
57
57
  end
58
58
 
59
+ should "raise NotImplementedError on `partial`" do
60
+ assert_raises NotImplementedError do
61
+ subject.partial(@path, @locals)
62
+ end
63
+ end
64
+
59
65
  end
60
66
 
61
67
  class NullTemplateEngineTests < Assert::Context
@@ -69,10 +75,11 @@ class Sanford::TemplateEngine
69
75
  assert_kind_of Sanford::TemplateEngine, subject
70
76
  end
71
77
 
72
- should "read and return the given path in its source path on `render" do
78
+ should "read and return the given path in its source path" do
73
79
  exists_file = ['test/support/template', 'test/support/template.erb'].sample
74
80
  exp = File.read(Dir.glob("#{subject.source_path.join(exists_file)}*").first)
75
81
  assert_equal exp, subject.render(exists_file, @service_handler, @locals)
82
+ assert_equal exp, subject.partial(exists_file, @locals)
76
83
  end
77
84
 
78
85
  should "complain if given a path that matches multiple files" do
@@ -80,6 +87,9 @@ class Sanford::TemplateEngine
80
87
  assert_raises ArgumentError do
81
88
  subject.render(conflict_file, @service_handler, @locals)
82
89
  end
90
+ assert_raises ArgumentError do
91
+ subject.partial(conflict_file, @locals)
92
+ end
83
93
  end
84
94
 
85
95
  should "complain if given a path that does not exist in its source path" do
@@ -87,6 +97,9 @@ class Sanford::TemplateEngine
87
97
  assert_raises ArgumentError do
88
98
  subject.render(no_exists_file, @service_handler, @locals)
89
99
  end
100
+ assert_raises ArgumentError do
101
+ subject.partial(no_exists_file, @locals)
102
+ end
90
103
  end
91
104
 
92
105
  end
@@ -22,7 +22,7 @@ class Sanford::TemplateSource
22
22
 
23
23
  should have_readers :path, :engines
24
24
  should have_imeths :engine, :engine_for?, :engine_for_template?
25
- should have_imeths :render
25
+ should have_imeths :render, :partial
26
26
 
27
27
  should "know its path" do
28
28
  assert_equal @source_path.to_s, subject.path
@@ -94,20 +94,24 @@ class Sanford::TemplateSource
94
94
 
95
95
  end
96
96
 
97
- class RenderTests < InitTests
98
- desc "when rendering a template"
97
+ class TemplateTests < InitTests
99
98
  setup do
100
99
  @source.engine('test', TestEngine)
101
100
  @source.engine('json', JsonEngine)
102
101
  end
103
102
 
103
+ end
104
+
105
+ class RenderTemplateTests < TemplateTests
106
+ desc "when rendering a template"
107
+
104
108
  should "render a matching template using the configured engine" do
105
109
  locals = { :something => Factory.string }
106
110
  result = subject.render('test_template', TestServiceHandler, locals)
107
- assert_equal 'test-engine', result
111
+ assert_equal 'test-engine-render', result
108
112
 
109
113
  result = subject.render('test_template.test', TestServiceHandler, locals)
110
- assert_equal 'test-engine', result
114
+ assert_equal 'test-engine-render', result
111
115
  end
112
116
 
113
117
  should "use the null template engine when an engine can't be found" do
@@ -126,6 +130,34 @@ class Sanford::TemplateSource
126
130
 
127
131
  end
128
132
 
133
+ class PartialTemplateTests < TemplateTests
134
+ desc "when rendering a partial template"
135
+
136
+ should "render a matching partial template using the configured engine" do
137
+ locals = { :something => Factory.string }
138
+ result = subject.partial('test_template', locals)
139
+ assert_equal 'test-engine-partial', result
140
+
141
+ result = subject.partial('test_template.test', locals)
142
+ assert_equal 'test-engine-partial', result
143
+ end
144
+
145
+ should "use the null template engine when an engine can't be found" do
146
+ assert_raises(ArgumentError) do
147
+ subject.partial(Factory.string, {})
148
+ end
149
+ end
150
+
151
+ should "complain if the given template name matches multiple templates" do
152
+ # there should be 2 files called "conflict_template" in `test/support`
153
+ # with diff extensions
154
+ assert_raises(ArgumentError) do
155
+ subject.partial('conflict_template', {})
156
+ end
157
+ end
158
+
159
+ end
160
+
129
161
  class NullTemplateSourceTests < Assert::Context
130
162
  desc "Sanford::NullTemplateSource"
131
163
  setup do
@@ -151,13 +183,19 @@ class Sanford::TemplateSource
151
183
 
152
184
  class TestEngine < Sanford::TemplateEngine
153
185
  def render(path, service_handler, locals)
154
- 'test-engine'
186
+ 'test-engine-render'
187
+ end
188
+ def partial(path, locals)
189
+ 'test-engine-partial'
155
190
  end
156
191
  end
157
192
 
158
193
  class JsonEngine < Sanford::TemplateEngine
159
194
  def render(path, service_handler, locals)
160
- 'json-engine'
195
+ 'json-engine-render'
196
+ end
197
+ def partial(path, locals)
198
+ 'json-engine-partial'
161
199
  end
162
200
  end
163
201
 
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.19.0
4
+ version: 0.19.1
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-09-15 00:00:00 Z
13
+ date: 2017-08-17 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: assert
@@ -19,7 +19,7 @@ dependencies:
19
19
  requirements:
20
20
  - - ~>
21
21
  - !ruby/object:Gem::Version
22
- version: 2.16.1
22
+ version: 2.16.3
23
23
  type: :development
24
24
  version_requirements: *id001
25
25
  - !ruby/object:Gem::Dependency
@@ -154,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
154
154
  requirements: []
155
155
 
156
156
  rubyforge_project:
157
- rubygems_version: 2.6.4
157
+ rubygems_version: 2.6.6
158
158
  signing_key:
159
159
  specification_version: 4
160
160
  summary: Sanford TCP protocol server for hosting services