Class: Emmett::Template

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

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, root) ⇒ Template

Returns a new instance of Template.



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

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.



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

def name
  @name
end

#rootObject (readonly)

Returns the value of attribute root.



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

def root
  @root
end

Class Method Details

.[](name) ⇒ Object



16
17
18
# File 'lib/emmett/template.rb', line 16

def [](name)
  registry.fetch(name.to_sym) { raise "Emmett does not know a template by the name '#{name}'" }
end

.add(name, path) ⇒ Object



20
21
22
23
24
25
# File 'lib/emmett/template.rb', line 20

def add(name, path)
  new(name, path).tap do |template|
    template.verify!
    template.register
  end
end

.register(name, value) ⇒ Object



12
13
14
# File 'lib/emmett/template.rb', line 12

def register(name, value)
  registry[name.to_sym] = value
end

.registryObject



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

def registry
  @registry ||= {}
end

Instance Method Details

#each_static_fileObject



66
67
68
69
70
71
# File 'lib/emmett/template.rb', line 66

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)


62
63
64
# File 'lib/emmett/template.rb', line 62

def has_template?(name)
  File.exist?
end

#registerObject



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

def register
  self.class.register name, self
end

#static_pathObject



49
50
51
# File 'lib/emmett/template.rb', line 49

def static_path
  @static_path ||= File.join(root, 'static')
end

#template_file_path(name) ⇒ Object



57
58
59
60
# File 'lib/emmett/template.rb', line 57

def template_file_path(name)
  path = File.join(template_path, name)
  File.exist?(path) ? path : nil
end

#template_pathObject



53
54
55
# File 'lib/emmett/template.rb', line 53

def template_path
  @template_path ||= File.join(root, 'templates')
end

#verify!Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/emmett/template.rb', line 36

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