Class: Emmett::Template
- Inherits:
-
Object
- Object
- Emmett::Template
- Defined in:
- lib/emmett/template.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- REQUIRED_TEMPLATES =
%w(index section)
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Class Method Summary collapse
Instance Method Summary collapse
- #each_static_file ⇒ Object
- #has_template?(name) ⇒ Boolean
-
#initialize(name, root) ⇒ Template
constructor
A new instance of Template.
- #missing_templates ⇒ Object
- #register ⇒ Object
- #static_path ⇒ Object
- #template_file_path(name) ⇒ Object
- #template_format ⇒ Object
- #template_path ⇒ Object
- #verify! ⇒ Object
Constructor Details
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
30 31 32 |
# File 'lib/emmett/template.rb', line 30 def name @name end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
30 31 32 |
# File 'lib/emmett/template.rb', line 30 def root @root end |
Class Method Details
.[](name) ⇒ Object
17 18 19 |
# File 'lib/emmett/template.rb', line 17 def [](name) registry.fetch(name.to_sym) { raise "Emmett does not know a template by the name '#{name}'" } end |
.add(name, path) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/emmett/template.rb', line 21 def add(name, path) new(name, path).tap do |template| template.verify! template.register end end |
.register(name, value) ⇒ Object
13 14 15 |
# File 'lib/emmett/template.rb', line 13 def register(name, value) registry[name.to_sym] = value end |
.registry ⇒ Object
9 10 11 |
# File 'lib/emmett/template.rb', line 9 def registry @registry ||= {} end |
Instance Method Details
#each_static_file ⇒ Object
76 77 78 79 80 81 |
# File 'lib/emmett/template.rb', line 76 def each_static_file Dir[File.join(static_path, '**/*')].select { |f| File.file?(f) }.each do |file| relative_name = file.gsub(static_path, "") yield relative_name, file end end |
#has_template?(name) ⇒ Boolean
72 73 74 |
# File 'lib/emmett/template.rb', line 72 def has_template?(name) File.exist? end |
#missing_templates ⇒ Object
53 54 55 56 57 |
# File 'lib/emmett/template.rb', line 53 def missing_templates @missing_templates ||= REQUIRED_TEMPLATES.select do |template| template_file_path(template).nil? end end |
#register ⇒ Object
83 84 85 |
# File 'lib/emmett/template.rb', line 83 def register self.class.register name, self end |
#static_path ⇒ Object
59 60 61 |
# File 'lib/emmett/template.rb', line 59 def static_path @static_path ||= File.join(root, 'static') end |
#template_file_path(name) ⇒ Object
67 68 69 70 |
# File 'lib/emmett/template.rb', line 67 def template_file_path(name) path = File.join(template_path, "#{name}.#{template_format}") File.exist?(path) ? path : nil end |
#template_format ⇒ Object
37 |
# File 'lib/emmett/template.rb', line 37 def template_format; "handlebars"; end |
#template_path ⇒ Object
63 64 65 |
# File 'lib/emmett/template.rb', line 63 def template_path @template_path ||= File.join(root, 'templates') end |
#verify! ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/emmett/template.rb', line 39 def verify! errors = [] errors << "Ensure the root directory exists" unless File.directory?(root) errors << "Ensure the name is set" unless name errors << "Ensure the template has a templates subdirectory" unless File.directory?(template_path) errors << "Ensure the template static path is a directory if present" if File.exist?(static_path) && !File.directory?(static_path) errors << "Missing the following files in your template: #{missing_templates.join(", ")}" if missing_templates.any? if errors.any? = "The following errors occured trying to add your template:\n" errors.each { |e| << "* #{e}\n" } raise Error.new() end end |