deas-erubis 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -4,7 +4,113 @@
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-erubis'
12
+
13
+ Deas.configure do |c|
14
+
15
+ c.template_source "/path/to/templates" do |s|
16
+ s.engine 'erb', Deas::Erubis::TemplateEngine
17
+ end
18
+
19
+ end
20
+ ```
21
+
22
+ Add `.erb` to any template files in your template source path. Deas will evaluate their content using Erubis when they are rendered.
23
+
24
+ ### Partials
25
+
26
+ Deas::Erubis provides template helpers for rendering partial templates.
27
+
28
+ ```erb
29
+ # in /path/to/templates/list.html.erb
30
+ <h1><%= list.title %></h1>
31
+ <ul>
32
+ <% list.items.each do |item| %>
33
+ <%= partial '_list_item.html', :item => item %>
34
+ <% end %>
35
+ </ul>
36
+
37
+ # in /path/to/templates/_list_item.html.erb
38
+ <li><%= item.name %></li>
39
+
40
+ # output
41
+ <h1>My List</h1>
42
+ <ul>
43
+ <li>Item 1</li>
44
+ <li>Item 2</li>
45
+ <li>Item 3</li>
46
+ </ul>
47
+ ```
48
+
49
+ There are also helpers for rendering partials that yield to given content blocks (called "capture partials")
50
+
51
+ ```erb
52
+ # in /path/to/templates/list.html.erb
53
+ <h1><%= list.title %></h1>
54
+ <ul>
55
+ <% list.items.each do |item| %>
56
+ <% capture_partial '_list_item.html' do %>
57
+ <span><%= item.name %></span>
58
+ <% end %>
59
+ <% end %>
60
+ </ul>
61
+
62
+ # in /path/to/templates/_list_item.html.erb
63
+ <li><%= yield %></li>
64
+
65
+ # output
66
+ # output
67
+ <h1>My List</h1>
68
+ <ul>
69
+ <li>
70
+ <span>Item 1</span>
71
+ </li>
72
+ <li>
73
+ <span>Item 2</span>
74
+ </li>
75
+ <li>
76
+ <span>Item 3</span>
77
+ </li>
78
+ </ul>
79
+ ```
80
+
81
+ ### Notes
82
+
83
+ The engine 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 the scope via the `view` method in templates. If you want to change this, provide a `'handler_local'` option when registering:
84
+
85
+ ```ruby
86
+ c.template_source "/path/to/templates" do |s|
87
+ s.engine 'erb', Deas::Erubis::TemplateEngine, 'handler_local' => 'view_handler'
88
+ end
89
+ ```
90
+
91
+ By default, `::Erubis::Eruby` is used to evaluate the templates. However, Erubis provides "enhancers" that add certain features. You can pass in any custom eruby class (with any enhancers you like) useing the `'eruby'` option when registering:
92
+
93
+ ```ruby
94
+ c.template_source "/path/to/templates" do |s|
95
+ s.engine 'erb', Deas::Erubis::TemplateEngine, 'eruby' => MyEnhancedEruby
96
+ end
97
+ ```
98
+
99
+ The engine doesn't cache templates by default - it opens the file from disk on each render. To enable caching (which stores the file contents in memory on first use), pass a `'cache'` option when registering:
100
+
101
+ ```ruby
102
+ c.template_source "/path/to/templates" do |s|
103
+ s.engine 'erb', Deas::Erubis::TemplateEngine, 'cache' => true
104
+ end
105
+ ```
106
+
107
+ If you wish to provide custom template helpers, organize them in module(s) and pass them with the `'helpers'` option when registering:
108
+
109
+ ```ruby
110
+ c.template_source "/path/to/templates" do |s|
111
+ s.engine 'erb', Deas::Erubis::TemplateEngine, 'helpers' => [MyAwesomeHelpers, NotSoAwesomeHelpers]
112
+ end
113
+ ```
8
114
 
9
115
  ## Installation
10
116
 
data/deas-erubis.gemspec CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |gem|
20
20
 
21
21
  gem.add_development_dependency("assert", ["~> 2.12"])
22
22
 
23
- gem.add_dependency("deas", ["~> 0.29"])
23
+ gem.add_dependency("deas", ["~> 0.30"])
24
24
  gem.add_dependency("erubis")
25
25
 
26
26
  end
data/lib/deas-erubis.rb CHANGED
@@ -15,8 +15,9 @@ module Deas::Erubis
15
15
  @erb_source ||= Source.new(self.source_path, {
16
16
  :eruby => self.opts['eruby'],
17
17
  :cache => self.opts['cache'],
18
- :deas_source => self.opts['deas_template_source'],
19
- :default_locals => { self.erb_logger_local => self.logger }
18
+ :default_source => self.opts['default_template_source'],
19
+ :helpers => self.opts['helpers'],
20
+ :locals => { self.erb_logger_local => self.logger }
20
21
  })
21
22
  end
22
23
 
@@ -19,22 +19,22 @@ module Deas::Erubis
19
19
  @cache = opts[:cache] ? Hash.new : NullCache.new
20
20
  @context_class = build_context_class(opts)
21
21
 
22
- @deas_source = opts[:deas_source]
22
+ @default_source = opts[:default_source]
23
23
  end
24
24
 
25
25
  def render(file_name, locals, &content)
26
- load(file_name).evaluate(@context_class.new(@deas_source, locals), &content)
26
+ load(file_name).evaluate(@context_class.new(@default_source, locals), &content)
27
27
  end
28
28
 
29
29
  def compile(filename, content)
30
- eruby(filename, content).evaluate(@context_class.new(@deas_source, {}))
30
+ template(filename, content).evaluate(@context_class.new(@default_source, {}))
31
31
  end
32
32
 
33
- def eruby(filename, content)
34
- @eruby_class.new(content, {
33
+ def template(filename, content)
34
+ Template.new(@eruby_class.new(content, {
35
35
  :bufvar => BUFVAR_NAME,
36
36
  :filename => filename
37
- })
37
+ }))
38
38
  end
39
39
 
40
40
  def inspect
@@ -49,7 +49,7 @@ module Deas::Erubis
49
49
  @cache[file_name] ||= begin
50
50
  filename = source_file_path(file_name).to_s
51
51
  content = File.send(File.respond_to?(:binread) ? :binread : :read, filename)
52
- eruby(filename, content)
52
+ template(filename, content)
53
53
  end
54
54
  end
55
55
 
@@ -60,11 +60,11 @@ module Deas::Erubis
60
60
  def build_context_class(opts)
61
61
  Class.new do
62
62
  include ::Deas::Erubis::TemplateHelpers
63
- # TODO: mixin context helpers? `opts[:template_helpers]`
64
- (opts[:default_locals] || {}).each{ |k, v| define_method(k){ v } }
63
+ [*(opts[:helpers] || [])].each{ |helper| include helper }
64
+ (opts[:locals] || {}).each{ |k, v| define_method(k){ v } }
65
65
 
66
- def initialize(deas_source, locals)
67
- @deas_source = deas_source
66
+ def initialize(default_source, locals)
67
+ @default_source = default_source
68
68
 
69
69
  metaclass = class << self; self; end
70
70
  metaclass.class_eval do
@@ -76,6 +76,21 @@ module Deas::Erubis
76
76
  end
77
77
  end
78
78
 
79
+ class Template
80
+ attr_reader :src, :filename, :eruby_class, :eruby_bufvar
81
+
82
+ def initialize(erubis_eruby)
83
+ @src = erubis_eruby.src
84
+ @filename = erubis_eruby.filename
85
+ @eruby_class = erubis_eruby.class
86
+ @eruby_bufvar = erubis_eruby.instance_variable_get('@bufvar')
87
+ end
88
+
89
+ def evaluate(context)
90
+ context.instance_eval(@src, @filename)
91
+ end
92
+ end
93
+
79
94
  class NullCache
80
95
  def [](file_name); end
81
96
  def []=(file_name, value); end
@@ -11,12 +11,20 @@ module Deas::Erubis
11
11
 
12
12
  module Methods
13
13
 
14
- def partial(n, l = nil)
15
- @deas_source.partial(n, l || {})
14
+ def partial(name, locals = nil)
15
+ source_partial(@default_source, name, locals)
16
16
  end
17
17
 
18
- def capture_partial(n, l = nil, &c)
19
- _erb_buffer @deas_source.partial(n, l || {}, &Proc.new{ _erb_capture(&c) })
18
+ def capture_partial(name, locals = nil, &c)
19
+ source_capture_partial(@default_source, name, locals, &c)
20
+ end
21
+
22
+ def source_partial(source, name, locals = nil)
23
+ source.partial(name, locals || {})
24
+ end
25
+
26
+ def source_capture_partial(source, name, locals = nil, &c)
27
+ _erb_buffer source.partial(name, locals || {}, &Proc.new{ _erb_capture(&c) })
20
28
  end
21
29
 
22
30
  private
@@ -1,4 +1,4 @@
1
1
  module Deas; end
2
2
  module Deas::Erubis
3
- VERSION = "0.3.0"
3
+ VERSION = "0.4.0"
4
4
  end
@@ -59,6 +59,7 @@ module Factory
59
59
  " <h1>local1: #{locals['local1']}</h1>\n"\
60
60
  "<p>logger: #{engine.logger.to_s}</p>\n\n"\
61
61
  " <span>No locals!</span>\n\n"\
62
+ " <span>No locals!</span>\n\n"\
62
63
  "</div>\n"
63
64
  end
64
65
 
@@ -74,6 +75,7 @@ module Factory
74
75
  "<h1>local1: #{locals['local1']}</h1>\n"\
75
76
  "<p>logger: #{engine.logger.to_s}</p>\n"\
76
77
  "<span>No locals!</span>\n"\
78
+ "<span>No locals!</span>\n"\
77
79
  "</div>\n"
78
80
  end
79
81
 
@@ -4,4 +4,5 @@
4
4
  <% end %>
5
5
  <% capture_partial '_partial', 'local1' => local1 %>
6
6
  <% capture_partial '_partial_no_locals' %>
7
+ <% source_capture_partial @default_source, '_partial_no_locals' %>
7
8
  </div>
@@ -1,4 +1,5 @@
1
1
  <div>
2
2
  <%= partial '_partial', 'local1' => local1 %>
3
3
  <%= partial '_partial_no_locals' %>
4
+ <%= source_partial @default_source, '_partial_no_locals' %>
4
5
  </div>
@@ -53,10 +53,10 @@ class Deas::Erubis::TemplateEngine
53
53
  class TemplateHelperTests < SystemTests
54
54
  desc "template helpers"
55
55
  setup do
56
- @deas_source = Deas::TemplateSource.new(TEMPLATE_ROOT).tap do |s|
56
+ @source = Deas::TemplateSource.new(TEMPLATE_ROOT).tap do |s|
57
57
  s.engine 'erb', Deas::Erubis::TemplateEngine
58
58
  end
59
- @engine = @deas_source.engines['erb']
59
+ @engine = @source.engines['erb']
60
60
  end
61
61
 
62
62
  should "render partials" do
@@ -35,7 +35,7 @@ class Deas::Erubis::Source
35
35
  subject{ @source }
36
36
 
37
37
  should have_readers :root, :eruby_class, :cache, :context_class
38
- should have_imeths :render, :compile, :eruby
38
+ should have_imeths :render, :compile, :template
39
39
 
40
40
  should "know its root" do
41
41
  assert_equal @root, subject.root.to_s
@@ -51,15 +51,13 @@ class Deas::Erubis::Source
51
51
  assert_equal eruby, source.eruby_class
52
52
  end
53
53
 
54
- should "build eruby instances for a given template file" do
55
- assert_kind_of subject.eruby_class, subject.eruby('basic', Factory.string)
56
- end
57
-
58
- should "build its eruby instances with the correct bufvar name" do
59
- eruby = subject.eruby('basic', Factory.string)
54
+ should "build template objects for template files" do
55
+ filename = 'basic'
56
+ template = subject.template(filename, Factory.string)
60
57
 
61
- exp = Deas::Erubis::Source::BUFVAR_NAME
62
- assert_equal exp, eruby.instance_variable_get('@bufvar')
58
+ assert_equal filename, template.filename
59
+ assert_equal subject.eruby_class, template.eruby_class
60
+ assert_equal Deas::Erubis::Source::BUFVAR_NAME, template.eruby_bufvar
63
61
  end
64
62
 
65
63
  should "not cache templates by default" do
@@ -81,12 +79,25 @@ class Deas::Erubis::Source
81
79
  context = subject.context_class.new('deas-source', {})
82
80
  assert_responds_to :partial, context
83
81
  assert_responds_to :capture_partial, context
82
+ assert_responds_to :source_partial, context
83
+ assert_responds_to :source_capture_partial, context
84
+ end
85
+
86
+ should "mixin custom template helpers to its context class" do
87
+ source = @source_class.new(@root, :helpers => SomeCustomHelpers)
88
+ assert_includes SomeCustomHelpers, source.context_class
89
+
90
+ source = @source_class.new(@root, :helpers => [SomeCustomHelpers])
91
+ assert_includes SomeCustomHelpers, source.context_class
92
+
93
+ context = source.context_class.new('deas-source', {})
94
+ assert_responds_to :a_custom_method, context
84
95
  end
85
96
 
86
97
  should "optionally take and apply default locals to its context class" do
87
98
  local_name, local_val = [Factory.string, Factory.string]
88
99
  source = @source_class.new(@root, {
89
- :default_locals => { local_name => local_val }
100
+ :locals => { local_name => local_val }
90
101
  })
91
102
  context = source.context_class.new('deas-source', {})
92
103
 
@@ -102,11 +113,11 @@ class Deas::Erubis::Source
102
113
  assert_equal local_val, context.send(local_name)
103
114
  end
104
115
 
105
- should "set any deas source given to its context class as an ivar on init" do
106
- deas_source = 'a-deas-source'
107
- context = subject.context_class.new(deas_source, {})
116
+ should "set any default source given to its context class as an ivar on init" do
117
+ default_source = 'a-default-source'
118
+ context = subject.context_class.new(default_source, {})
108
119
 
109
- assert_equal deas_source, context.instance_variable_get('@deas_source')
120
+ assert_equal default_source, context.instance_variable_get('@default_source')
110
121
  end
111
122
 
112
123
  end
@@ -126,16 +137,16 @@ class Deas::Erubis::Source
126
137
  assert_equal exp, subject.render(@file_name, @file_locals)
127
138
  end
128
139
 
129
- should "pass its deas source to its context class" do
130
- deas_source = 'a-deas-source'
131
- source = @source_class.new(@root, :deas_source => deas_source)
140
+ should "pass its default source to its context class" do
141
+ default_source = 'a-deas-source'
142
+ source = @source_class.new(@root, :default_source => default_source)
132
143
  context_class = nil
133
144
  Assert.stub(source.context_class, :new) do |s, l|
134
145
  context_class = ContextClassSpy.new(s, l)
135
146
  end
136
147
  source.render(@file_name, @file_locals)
137
148
 
138
- assert_equal deas_source, context_class.deas_source
149
+ assert_equal default_source, context_class.default_source
139
150
  end
140
151
 
141
152
  end
@@ -146,12 +157,12 @@ class Deas::Erubis::Source
146
157
  @source = @source_class.new(@root, :cache => true)
147
158
  end
148
159
 
149
- should "cache template eruby instances by their file name" do
160
+ should "cache templates by their file name" do
150
161
  exp = Factory.basic_erb_rendered(@file_locals)
151
162
  assert_equal exp, @source.render(@file_name, @file_locals)
152
163
 
153
164
  assert_equal [@file_name], @source.cache.keys
154
- assert_kind_of @source.eruby_class, @source.cache[@file_name]
165
+ assert_kind_of Template, @source.cache[@file_name]
155
166
  end
156
167
 
157
168
  end
@@ -162,7 +173,7 @@ class Deas::Erubis::Source
162
173
  @source = @source_class.new(@root, :cache => false)
163
174
  end
164
175
 
165
- should "not cache template eruby instances" do
176
+ should "not cache templates" do
166
177
  exp = Factory.basic_erb_rendered(@file_locals)
167
178
  assert_equal exp, @source.render(@file_name, @file_locals)
168
179
 
@@ -239,9 +250,9 @@ class Deas::Erubis::Source
239
250
  end
240
251
 
241
252
  class ContextClassSpy
242
- attr_reader :deas_source
243
- def initialize(deas_source, locals)
244
- @deas_source = deas_source
253
+ attr_reader :default_source
254
+ def initialize(default_source, locals)
255
+ @default_source = default_source
245
256
  metaclass = class << self; self; end
246
257
  metaclass.class_eval do
247
258
  locals.each do |key, value|
@@ -251,4 +262,8 @@ class Deas::Erubis::Source
251
262
  end
252
263
  end
253
264
 
265
+ module SomeCustomHelpers
266
+ def a_custom_method; end
267
+ end
268
+
254
269
  end
@@ -40,13 +40,18 @@ class Deas::Erubis::TemplateEngine
40
40
  end
41
41
 
42
42
  should "pass any given deas template source to its source" do
43
- deas_source = 'a-deas-source'
43
+ default_source = 'a-default-source'
44
44
  source_opts = nil
45
45
 
46
46
  Assert.stub(Deas::Erubis::Source, :new){ |root, opts| source_opts = opts }
47
- Deas::Erubis::TemplateEngine.new('deas_template_source' => deas_source).erb_source
47
+ Deas::Erubis::TemplateEngine.new('default_template_source' => default_source).erb_source
48
48
 
49
- assert_equal deas_source, source_opts[:deas_source]
49
+ assert_equal default_source, source_opts[:default_source]
50
+ end
51
+
52
+ should "pass any given helpers option to its source" do
53
+ engine = Deas::Erubis::TemplateEngine.new('helpers' => [SomeCustomHelpers])
54
+ assert_includes SomeCustomHelpers, engine.erb_source.context_class
50
55
  end
51
56
 
52
57
  should "use 'view' as the handler local name by default" do
@@ -71,4 +76,8 @@ class Deas::Erubis::TemplateEngine
71
76
 
72
77
  end
73
78
 
79
+ module SomeCustomHelpers
80
+ def a_custom_method; end
81
+ end
82
+
74
83
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deas-erubis
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 3
8
+ - 4
9
9
  - 0
10
- version: 0.3.0
10
+ version: 0.4.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: 2015-01-14 00:00:00 Z
19
+ date: 2015-02-04 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  requirement: &id001 !ruby/object:Gem::Requirement
@@ -39,11 +39,11 @@ dependencies:
39
39
  requirements:
40
40
  - - ~>
41
41
  - !ruby/object:Gem::Version
42
- hash: 49
42
+ hash: 55
43
43
  segments:
44
44
  - 0
45
- - 29
46
- version: "0.29"
45
+ - 30
46
+ version: "0.30"
47
47
  type: :runtime
48
48
  name: deas
49
49
  version_requirements: *id002