nm 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -65,6 +65,24 @@ Output:
65
65
 
66
66
  ## Notes
67
67
 
68
+ ### Cache Templates
69
+
70
+ By default the source doesn't cache template files. You can configure it to cache templates using the `:cache` option:
71
+
72
+ ```ruby
73
+ source = Nm::Source.new('/path/to/views', :cache => true)
74
+ ```
75
+
76
+ ### Default locals
77
+
78
+ You can specify a set of default locals to use on all renders for a source using the `:locals` option:
79
+
80
+ ```ruby
81
+ source = Nm::Source.new('/path/to/views', :locals => {
82
+ 'something' => 'value'
83
+ })
84
+ ```
85
+
68
86
  ### Render Format
69
87
 
70
88
  Rendering templates returns a data object (`::Hash` or `::Array`). To serialize, bring in your favorite JSON/BSON/whatever serializer and pass the rendered object to it.
data/lib/nm/source.rb CHANGED
@@ -7,12 +7,15 @@ module Nm
7
7
 
8
8
  EXT = ".nm"
9
9
 
10
- attr_reader :root, :template_class
10
+ attr_reader :root, :cache, :template_class
11
+
12
+ def initialize(root, opts = nil)
13
+ opts ||= {}
14
+ @root = Pathname.new(root.to_s)
15
+ @cache = opts[:cache] ? Hash.new : NullCache.new
11
16
 
12
- def initialize(root, locals = nil)
13
- @root = Pathname.new(root.to_s)
14
17
  @template_class = Class.new(Template) do
15
- (locals || {}).each{ |key, value| define_method(key){ value } }
18
+ (opts[:locals] || {}).each{ |key, value| define_method(key){ value } }
16
19
  end
17
20
  end
18
21
 
@@ -21,7 +24,9 @@ module Nm
21
24
  end
22
25
 
23
26
  def data(file_path)
24
- File.send(File.respond_to?(:binread) ? :binread : :read, file_path)
27
+ @cache[file_path] ||= begin
28
+ File.send(File.respond_to?(:binread) ? :binread : :read, file_path)
29
+ end
25
30
  end
26
31
 
27
32
  def render(file_name, locals = nil)
@@ -36,6 +41,12 @@ module Nm
36
41
  self.root.join("#{file_name}#{EXT}").to_s
37
42
  end
38
43
 
44
+ class NullCache
45
+ def [](file_name); end
46
+ def []=(file_name, value); end
47
+ def keys; []; end
48
+ end
49
+
39
50
  end
40
51
 
41
52
  class DefaultSource < Source
data/lib/nm/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Nm
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -7,7 +7,10 @@ class Nm::Source
7
7
 
8
8
  class UnitTests < Assert::Context
9
9
  desc "Nm::Source"
10
- subject{ Nm::Source }
10
+ setup do
11
+ @source_class = Nm::Source
12
+ end
13
+ subject{ @source_class }
11
14
 
12
15
  should "know its extension" do
13
16
  assert_equal ".nm", subject::EXT
@@ -19,24 +22,35 @@ class Nm::Source
19
22
  desc "when init"
20
23
  setup do
21
24
  @root = Factory.template_root
22
- @source = Nm::Source.new(@root)
25
+ @source = @source_class.new(@root)
23
26
  end
24
27
  subject{ @source }
25
28
 
26
- should have_readers :root, :template_class
29
+ should have_readers :root, :cache, :template_class
27
30
  should have_imeths :data, :render, :partial
28
31
 
29
32
  should "know its root" do
30
33
  assert_equal @root, subject.root.to_s
31
34
  end
32
35
 
36
+ should "not cache templates by default" do
37
+ assert_kind_of NullCache, subject.cache
38
+ end
39
+
40
+ should "cache templates if the :cache opt is `true`" do
41
+ source = @source_class.new(@root, :cache => true)
42
+ assert_kind_of Hash, source.cache
43
+ end
44
+
33
45
  should "know its template class" do
34
46
  assert_true subject.template_class < Nm::Template
35
47
  end
36
48
 
37
49
  should "optionally take and apply default locals to its template class" do
38
50
  local_name, local_val = [Factory.string, Factory.string]
39
- source = Nm::Source.new(@root, local_name => local_val)
51
+ source = @source_class.new(@root, :locals => {
52
+ local_name => local_val
53
+ })
40
54
  template = source.template_class.new
41
55
 
42
56
  assert_responds_to local_name, template
@@ -56,6 +70,19 @@ class Nm::Source
56
70
  assert_equal exp, subject.data(@file_path)
57
71
  end
58
72
 
73
+ should "not cache template source by default" do
74
+ assert_equal [], subject.cache.keys
75
+ end
76
+
77
+ should "cache template source by file path if enabled" do
78
+ source = @source_class.new(@root, :cache => true)
79
+
80
+ exp = File.read(@file_path)
81
+ assert_equal exp, source.data(@file_path)
82
+ assert_equal [@file_path], source.cache.keys
83
+ assert_equal exp, source.cache[@file_path]
84
+ end
85
+
59
86
  end
60
87
 
61
88
  class RenderTests < InitTests
@@ -63,7 +90,7 @@ class Nm::Source
63
90
  setup do
64
91
  @file_name = "locals"
65
92
  @file_locals = { 'key' => 'a-value' }
66
- @file_path = Factory.template_file("#{@file_name}#{Nm::Source::EXT}")
93
+ @file_path = Factory.template_file("#{@file_name}#{@source_class::EXT}")
67
94
  end
68
95
 
69
96
  should "render a template for the given file name and return its data" do
@@ -86,7 +113,7 @@ class Nm::Source
86
113
  subject{ @source }
87
114
 
88
115
  should "be a Source" do
89
- assert_kind_of Nm::Source, subject
116
+ assert_kind_of @source_class, subject
90
117
  end
91
118
 
92
119
  should "use `/` as its root" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nm
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 4
8
+ - 5
9
9
  - 0
10
- version: 0.4.0
10
+ version: 0.5.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-25 00:00:00 Z
19
+ date: 2015-01-19 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  requirement: &id001 !ruby/object:Gem::Requirement