Class: Typingpool::Template
- Inherits:
-
Object
- Object
- Typingpool::Template
- Defined in:
- lib/typingpool/template.rb,
lib/typingpool/template/env.rb,
lib/typingpool/template/assignment.rb
Overview
Model class that wraps ERB and adds a few Typingpool-specific capabilities: The ability to look in an array of search paths for a particular relative path, neccesary to support the Config#template dir on top of the built-in app template dir. Also makes it easy to pass in a hash and render the template against that hash, rather than against all the variables in the current namespace.
Direct Known Subclasses
Defined Under Namespace
Classes: Assignment, Env
Instance Attribute Summary collapse
-
#look_in ⇒ Object
readonly
An array of base paths to be searched when we’re given a relative path to the template.
Class Method Summary collapse
-
.from_config(path, config = Config.file) ⇒ Object
Constructor.
-
.look_in_from_config(config) ⇒ Object
private.
- .validate_config(config) ⇒ Object
Instance Method Summary collapse
-
#full_path ⇒ Object
Returns the path to the template after searching the various look_in dirs for the relative path.
-
#initialize(path, look_in) ⇒ Template
constructor
Constructor.
-
#read ⇒ Object
Returns the raw text of the template, unrendered.
-
#render(hash) ⇒ Object
Takes a hash to pass to an ERB template and returns the text from rendering the template against that hash (the hash becomes the top-level namespace of the template, so the keys are accessed just as you’d normally access a variable in an ERB template).
-
#render_with_binding(binding) ⇒ Object
Like render, but takes a binding instead of hash.
Constructor Details
#initialize(path, look_in) ⇒ Template
Constructor. Takes a relative path and an array of base paths to search for relative template paths. See look_in docs. Template should be an ERB template.
43 44 45 46 47 |
# File 'lib/typingpool/template.rb', line 43 def initialize(path, look_in) @path = path @look_in = look_in full_path or raise Error, "Could not find template path '#{path}' in #{look_in.join(',')}" end |
Instance Attribute Details
#look_in ⇒ Object (readonly)
An array of base paths to be searched when we’re given a relative path to the template. Normally this includes the user’s Config#template attribute, if any, followed by the built-in app template dir.
38 39 40 |
# File 'lib/typingpool/template.rb', line 38 def look_in @look_in end |
Class Method Details
.from_config(path, config = Config.file) ⇒ Object
Constructor. Takes a relative template path and an optional config file. Default config is Config.file.
13 14 15 16 |
# File 'lib/typingpool/template.rb', line 13 def from_config(path, config=Config.file) validate_config(config) new(path, look_in_from_config(config)) end |
.look_in_from_config(config) ⇒ Object
private
20 21 22 23 24 |
# File 'lib/typingpool/template.rb', line 20 def look_in_from_config(config) look_in = [File.join(Utility.lib_dir, 'templates'), ''] look_in.unshift(config.templates) if config.templates look_in end |
.validate_config(config) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/typingpool/template.rb', line 26 def validate_config(config) if config.templates File.exists?(config.templates) or raise Error::File::NotExists, "No such templates dir: #{config.templates}" File.directory?(config.templates) or raise Error::File::NotExists, "Templates dir not a directory: #{config.templates}" end end |
Instance Method Details
#full_path ⇒ Object
Returns the path to the template after searching the various look_in dirs for the relative path. Returns nil if the template cannot be located.
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/typingpool/template.rb', line 70 def full_path look_in.each do |dir| extensions.each do |ext| path = File.join(dir, [@path, ext].join) if File.exists?(path) && File.file?(path) return path end end end return end |
#read ⇒ Object
Returns the raw text of the template, unrendered.
63 64 65 |
# File 'lib/typingpool/template.rb', line 63 def read IO.read(full_path) end |
#render(hash) ⇒ Object
Takes a hash to pass to an ERB template and returns the text from rendering the template against that hash (the hash becomes the top-level namespace of the template, so the keys are accessed just as you’d normally access a variable in an ERB template).
53 54 55 |
# File 'lib/typingpool/template.rb', line 53 def render(hash) render_with_binding(Env.new(hash, self).get_binding) end |
#render_with_binding(binding) ⇒ Object
Like render, but takes a binding instead of hash
58 59 60 |
# File 'lib/typingpool/template.rb', line 58 def render_with_binding(binding) ERB.new(read, nil, '<>').result(binding) end |