nm 0.5.1 → 0.5.2

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
  SHA1:
3
- data.tar.gz: a775f8cf829f10a9890879b5a8f227da85eca3a7
4
- metadata.gz: 43d84fe3504eccd6241abaf2a1842d511410323d
3
+ metadata.gz: e31cf50f545fcddce9f04f39d6772210358f9873
4
+ data.tar.gz: 0f87cd5c7ebd8752c2a3f1d5dbb2ed8858ba9dfc
5
5
  SHA512:
6
- data.tar.gz: 049217416ebbbc9e98bc3a464ceff8e35111929eb6c9a1b06655a1e1abfa90cddbb5c9d63648d80f3996539828fe93ade692f66377267f5adc899c7cc40b287d
7
- metadata.gz: de20fcd9575520aec65cc71137448b32ba1fcb129399222f1086e14af899493ca6e6f454622b23c52f86be0b7e95dfe6a61fa77fffa9bd20e6b472a96f06e778
6
+ metadata.gz: 925cdd2c049da6f0747999f372016e35b519572d6a0b12647f181cd6bbd8b2d5a3555e36b053d7b8f3a610c3459a65fd5bd7a950fef2e7a50934ec837627d743
7
+ data.tar.gz: 403c5c37f9271836fea0d3f2b0f9b2edc403336197d4e9e015812e5543214b9671468046e2f8bd846271cfce12f23ddbfba470793ee21cc293688e2fee86d7a2
data/lib/nm/source.rb CHANGED
@@ -5,11 +5,12 @@ module Nm
5
5
 
6
6
  class Source
7
7
 
8
- attr_reader :root, :cache, :template_class
8
+ attr_reader :root, :ext, :cache, :template_class
9
9
 
10
10
  def initialize(root, opts = nil)
11
11
  opts ||= {}
12
12
  @root = Pathname.new(root.to_s)
13
+ @ext = opts[:ext] ? ".#{opts[:ext]}" : nil
13
14
  @cache = opts[:cache] ? Hash.new : NullCache.new
14
15
 
15
16
  @template_class = Class.new(Template) do
@@ -28,15 +29,22 @@ module Nm
28
29
  end
29
30
 
30
31
  def render(template_name, locals = nil)
31
- @template_class.new(self, source_file_path(template_name), locals || {}).__data__
32
+ if (filename = source_file_path(template_name)).nil?
33
+ template_desc = "a template file named #{template_name.inspect}"
34
+ if !@ext.nil?
35
+ template_desc += " that ends in #{@ext.inspect}"
36
+ end
37
+ raise ArgumentError, "#{template_desc} does not exist"
38
+ end
39
+ @template_class.new(self, filename, locals || {}).__data__
32
40
  end
33
41
 
34
42
  alias_method :partial, :render
35
43
 
36
44
  private
37
45
 
38
- def source_file_path(template_name)
39
- Dir.glob(self.root.join("#{template_name}*")).first
46
+ def source_file_path(name)
47
+ Dir.glob(self.root.join(name.end_with?(@ext) ? name : "#{name}*#{@ext}")).first
40
48
  end
41
49
 
42
50
  class NullCache
data/lib/nm/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Nm
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.2"
3
3
  end
@@ -22,13 +22,21 @@ class Nm::Source
22
22
  end
23
23
  subject{ @source }
24
24
 
25
- should have_readers :root, :cache, :template_class
25
+ should have_readers :root, :ext, :cache, :template_class
26
26
  should have_imeths :data, :render, :partial
27
27
 
28
28
  should "know its root" do
29
29
  assert_equal @root, subject.root.to_s
30
30
  end
31
31
 
32
+ should "know its extension for looking up source files" do
33
+ assert_nil subject.ext
34
+
35
+ ext = Factory.string
36
+ source = @source_class.new(@root, :ext => ext)
37
+ assert_equal ".#{ext}", source.ext
38
+ end
39
+
32
40
  should "not cache templates by default" do
33
41
  assert_kind_of NullCache, subject.cache
34
42
  end
@@ -99,6 +107,32 @@ class Nm::Source
99
107
  assert_equal exp, subject.partial(@template_name, @file_locals)
100
108
  end
101
109
 
110
+ should "only render templates with the matching ext if one is specified" do
111
+ source = @source_class.new(@root, :ext => 'nm')
112
+ file_path = Factory.template_file('locals.nm')
113
+ exp = Nm::Template.new(source, file_path, @file_locals).__data__
114
+ ['locals', 'locals.nm'].each do |name|
115
+ assert_equal exp, source.render(name, @file_locals)
116
+ end
117
+
118
+ source = @source_class.new(@root, :ext => 'inem')
119
+ file_path = Factory.template_file('locals_alt.data.inem')
120
+ exp = Nm::Template.new(source, file_path, @file_locals).__data__
121
+ ['locals', 'locals_alt', 'locals_alt.data', 'locals_alt.data.inem'].each do |name|
122
+ assert_equal exp, source.render(name, @file_locals)
123
+ end
124
+
125
+ source = @source_class.new(@root, :ext => 'nm')
126
+ ['locals_alt', 'locals_alt.data', 'locals_alt.data.inem'].each do |name|
127
+ assert_raises(ArgumentError){ source.render(name, @file_locals) }
128
+ end
129
+
130
+ source = @source_class.new(@root, :ext => 'data')
131
+ ['locals_alt', 'locals_alt.data', 'locals_alt.data.inem'].each do |name|
132
+ assert_raises(ArgumentError){ source.render(name, @file_locals) }
133
+ end
134
+ end
135
+
102
136
  end
103
137
 
104
138
  class DefaultSource < UnitTests
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
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-05 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
@@ -52,7 +52,7 @@ files:
52
52
  - test/support/templates/aliases.nm
53
53
  - test/support/templates/list.nm
54
54
  - test/support/templates/locals.nm
55
- - test/support/templates/locals_alt.inem
55
+ - test/support/templates/locals_alt.data.inem
56
56
  - test/support/templates/obj.nm
57
57
  - test/unit/ext_tests.rb
58
58
  - test/unit/source_tests.rb
@@ -93,7 +93,7 @@ test_files:
93
93
  - test/support/templates/aliases.nm
94
94
  - test/support/templates/list.nm
95
95
  - test/support/templates/locals.nm
96
- - test/support/templates/locals_alt.inem
96
+ - test/support/templates/locals_alt.data.inem
97
97
  - test/support/templates/obj.nm
98
98
  - test/unit/ext_tests.rb
99
99
  - test/unit/source_tests.rb