Class: Emmett::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/emmett/template.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

REQUIRED_TEMPLATES =
%w(index section)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, root) ⇒ Template

Returns a new instance of Template.



32
33
34
35
# File 'lib/emmett/template.rb', line 32

def initialize(name, root)
  @name = name && name.to_sym
  @root = root.to_s
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



30
31
32
# File 'lib/emmett/template.rb', line 30

def name
  @name
end

#rootObject (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

.registryObject



9
10
11
# File 'lib/emmett/template.rb', line 9

def registry
  @registry ||= {}
end

Instance Method Details

#each_static_fileObject



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

Returns:

  • (Boolean)


72
73
74
# File 'lib/emmett/template.rb', line 72

def has_template?(name)
  File.exist?
end

#missing_templatesObject



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

#registerObject



83
84
85
# File 'lib/emmett/template.rb', line 83

def register
  self.class.register name, self
end

#static_pathObject



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_formatObject



37
# File 'lib/emmett/template.rb', line 37

def template_format; "handlebars"; end

#template_pathObject



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?
    message = "The following errors occured trying to add your template:\n"
    errors.each { |e| message << "* #{e}\n" }
    raise Error.new(message)
  end
end