deas-nm 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,10 +1,66 @@
1
- # DeasNm
1
+ # Deas::Nm
2
2
 
3
3
  [Deas](https://github.com/redding/deas) template engine for rendering [Nm](https://github.com/redding/nm) templates
4
4
 
5
5
  ## Usage
6
6
 
7
- TODO: Write code samples and usage instructions here
7
+ Register the engine:
8
+
9
+ ```ruby
10
+ require 'deas'
11
+ require 'deas-nm'
12
+
13
+ Deas.configure do |c|
14
+
15
+ c.template_source "/path/to/templates" do |s|
16
+ s.engine 'nm', Deas::Nm::TemplateEngine
17
+ end
18
+
19
+ end
20
+ ```
21
+
22
+ Add `.nm` to any template files in your template source path. Deas will render their content using Nm when they are rendered.
23
+
24
+ ### Serialization
25
+
26
+ Nm doesn't serialize the objects it renders - it just returns them. However, Deas expects serialized body content. By default, the rendered objects are not serialized.
27
+
28
+ To serialize the rendered objects, specify a serializer when registering:
29
+
30
+ ```ruby
31
+ # this uses Oj to serialize to JSON (for example)
32
+ c.template_source "/path/to/templates" do |s|
33
+ s.engine('nm', Deas::Nm::TemplateEngine, {
34
+ 'serializer' => proc{ |obj, template_name| Oj.dump(obj, :mode => :strict) }
35
+ })
36
+ end
37
+ ```
38
+
39
+ The template name is passed to any serializer proc. This can be helpful if choosing how to serialize is conditonal upon the template name. For example:
40
+
41
+ ```ruby
42
+ c.template_source "/path/to/templates" do |s|
43
+ s.engine('nm', Deas::Nm::TemplateEngine, {
44
+ 'serializer' => proc do |obj, template_name|
45
+ if File.extname(template_name) == '.json'
46
+ Oj.dump(obj, :mode => :strict)
47
+ else
48
+ # serialize some other way?
49
+ end
50
+ end
51
+ })
52
+ end
53
+ ```
54
+
55
+ ### Notes
56
+
57
+ Nm doesn't allow overriding the template scope but instead allows you to pass in data that binds to the template scope as local methods. By default, the view handler will be bound to Nm's scope via the `view` method in templates. If you want to change this, provide a `'handler_local'` option when registering:
58
+
59
+ ```ruby
60
+ c.template_source "/path/to/templates" do |s|
61
+ s.engine 'nm', Deas::Nm::TemplateEngine, 'handler_local' => 'view_handler'
62
+ end
63
+ ```
8
64
 
9
65
  ## Installation
10
66
 
data/lib/deas-nm.rb CHANGED
@@ -1,5 +1,50 @@
1
+ require 'deas/template_engine'
2
+ require 'nm'
1
3
  require "deas-nm/version"
2
4
 
3
5
  module Deas::Nm
4
- # TODO: your code goes here...
6
+
7
+ class TemplateEngine < Deas::TemplateEngine
8
+
9
+ DEFAULT_HANDLER_LOCAL = 'view'.freeze
10
+ DEFAULT_SERIALIZER = proc{ |obj, template_name| obj }.freeze # no-op
11
+
12
+ def nm_source
13
+ @nm_source ||= Nm::Source.new(self.source_path)
14
+ end
15
+
16
+ def nm_handler_local
17
+ @nm_handler_local ||= (self.opts['handler_local'] || DEFAULT_HANDLER_LOCAL)
18
+ end
19
+
20
+ def nm_serializer
21
+ @nm_serializer ||= (self.opts['serializer'] || DEFAULT_SERIALIZER)
22
+ end
23
+
24
+ def render(template_name, view_handler, locals)
25
+ self.nm_serializer.call(
26
+ self.nm_source.render(template_name, render_locals(view_handler, locals)),
27
+ template_name
28
+ )
29
+ end
30
+
31
+ def partial(template_name, locals)
32
+ self.nm_serializer.call(
33
+ self.nm_source.render(template_name, locals),
34
+ template_name
35
+ )
36
+ end
37
+
38
+ def capture_partial(template_name, locals, &content)
39
+ raise NotImplementedError
40
+ end
41
+
42
+ private
43
+
44
+ def render_locals(view_handler, locals)
45
+ { self.nm_handler_local => view_handler }.merge(locals)
46
+ end
47
+
48
+ end
49
+
5
50
  end
@@ -1,4 +1,4 @@
1
1
  module Deas; end
2
2
  module Deas::Nm
3
- VERSION = "0.0.1"
3
+ VERSION = "0.1.0"
4
4
  end
data/test/helper.rb CHANGED
@@ -9,4 +9,5 @@ require 'pry'
9
9
 
10
10
  require 'test/support/factory'
11
11
 
12
- # TODO: put test helpers here...
12
+ require 'pathname'
13
+ TEST_SUPPORT_PATH = Pathname.new(File.expand_path('../support', __FILE__))
@@ -0,0 +1,3 @@
1
+ node('thing') {
2
+ node('local1', local1)
3
+ }
@@ -3,4 +3,20 @@ require 'assert/factory'
3
3
  module Factory
4
4
  extend Assert::Factory
5
5
 
6
+ def self.template_json_rendered(view_handler, locals)
7
+ { 'thing' => {
8
+ 'id' => view_handler.identifier,
9
+ 'name' => view_handler.name,
10
+ 'local1' => locals['local1']
11
+ }
12
+ }
13
+ end
14
+
15
+ def self.partial_json_rendered(locals)
16
+ { 'thing' => {
17
+ 'local1' => locals['local1']
18
+ }
19
+ }
20
+ end
21
+
6
22
  end
@@ -0,0 +1,5 @@
1
+ node('thing') {
2
+ node('id', view.identifier)
3
+ node('name', view.name)
4
+ node('local1', local1)
5
+ }
@@ -0,0 +1,80 @@
1
+ require 'assert'
2
+ require 'deas-nm'
3
+
4
+ require 'nm/source'
5
+ require 'deas/template_engine'
6
+
7
+ class Deas::Nm::TemplateEngine
8
+
9
+ class UnitTests < Assert::Context
10
+ desc "Deas::Nm::TemplateEngine"
11
+ setup do
12
+ @engine = Deas::Nm::TemplateEngine.new({
13
+ 'source_path' => TEST_SUPPORT_PATH
14
+ })
15
+ end
16
+ subject{ @engine }
17
+
18
+ should have_imeths :nm_source, :nm_handler_local, :nm_serializer
19
+ should have_imeths :render, :partial, :capture_partial
20
+
21
+ should "be a Deas template engine" do
22
+ assert_kind_of Deas::TemplateEngine, subject
23
+ end
24
+
25
+ should "memoize its Nm source" do
26
+ assert_kind_of Nm::Source, subject.nm_source
27
+ assert_equal subject.source_path, subject.nm_source.root
28
+ assert_same subject.nm_source, subject.nm_source
29
+ end
30
+
31
+ should "use 'view' as the handler local name by default" do
32
+ assert_equal 'view', subject.nm_handler_local
33
+ end
34
+
35
+ should "allow custom handler local names" do
36
+ handler_local = Factory.string
37
+ engine = Deas::Nm::TemplateEngine.new('handler_local' => handler_local)
38
+ assert_equal handler_local, engine.nm_handler_local
39
+ end
40
+
41
+ should "use a no-op serializer by default" do
42
+ obj = Factory.integer
43
+ assert_equal obj, subject.nm_serializer.call(obj, Factory.string)
44
+ end
45
+
46
+ should "render nm template files and serialize them" do
47
+ engine = Deas::Nm::TemplateEngine.new({
48
+ 'source_path' => TEST_SUPPORT_PATH,
49
+ 'serializer' => proc{ |obj, template_name| obj.to_s }
50
+ })
51
+ view_handler = OpenStruct.new({
52
+ :identifier => Factory.integer,
53
+ :name => Factory.string
54
+ })
55
+ locals = { 'local1' => Factory.string }
56
+ exp = Factory.template_json_rendered(view_handler, locals).to_s
57
+
58
+ assert_equal exp, engine.render('template.json', view_handler, locals)
59
+ end
60
+
61
+ should "render nm partials and serialize them" do
62
+ engine = Deas::Nm::TemplateEngine.new({
63
+ 'source_path' => TEST_SUPPORT_PATH,
64
+ 'serializer' => proc{ |obj, template_name| obj.to_s }
65
+ })
66
+ locals = { 'local1' => Factory.string }
67
+ exp = Factory.partial_json_rendered(locals).to_s
68
+
69
+ assert_equal exp, engine.partial('_partial.json', locals)
70
+ end
71
+
72
+ should "not implement the engine capture partial method" do
73
+ assert_raises NotImplementedError do
74
+ subject.capture_partial('_partial.json', {})
75
+ end
76
+ end
77
+
78
+ end
79
+
80
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deas-nm
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 0
9
8
  - 1
10
- version: 0.0.1
9
+ - 0
10
+ version: 0.1.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: 2014-11-14 00:00:00 Z
19
+ date: 2014-11-18 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  requirement: &id001 !ruby/object:Gem::Requirement
@@ -83,7 +83,10 @@ files:
83
83
  - lib/deas-nm/version.rb
84
84
  - log/.gitkeep
85
85
  - test/helper.rb
86
+ - test/support/_partial.json.nm
86
87
  - test/support/factory.rb
88
+ - test/support/template.json.nm
89
+ - test/unit/template_engine_tests.rb
87
90
  - tmp/.gitkeep
88
91
  homepage: http://github.com/redding/deas-nm
89
92
  licenses:
@@ -120,4 +123,7 @@ specification_version: 3
120
123
  summary: Deas template engine for Nm templates
121
124
  test_files:
122
125
  - test/helper.rb
126
+ - test/support/_partial.json.nm
123
127
  - test/support/factory.rb
128
+ - test/support/template.json.nm
129
+ - test/unit/template_engine_tests.rb