Class: Tres::TemplateManager

Inherits:
Object
  • Object
show all
Includes:
FileMethods
Defined in:
lib/tres/template_manager.rb

Constant Summary collapse

EXTENSION_MAP =
{
  %w(.haml .erb)  => '.html'
}

Instance Method Summary collapse

Methods included from FileMethods

#append_to_file, #basename, #change_dir, #copy, #create_file, #delete!, #dir?, #dirname, #erb, #expand, #extname, #file?, #json, #mkdir_p, #move, #name_and_extension, #new_dir, #new_file, #read_file, #readlines, #relativize

Constructor Details

#initialize(options = {}) ⇒ TemplateManager

Returns a new instance of TemplateManager.



14
15
16
17
18
19
# File 'lib/tres/template_manager.rb', line 14

def initialize options = {}
  @root = options[:root]
  @templates = @root/'templates'
  @assets = @root/'assets'
  @build = @root/'build'
end

Instance Method Details

#compile_allObject



50
51
52
53
54
55
56
57
58
# File 'lib/tres/template_manager.rb', line 50

def compile_all
  if index = first_index_file
    compile_to_build index 
  end
  Find.find(@templates) do |path|
    next if dir?(path) or path =~ /index/
    compile_to_templates_js relativize(path, @templates)
  end
end

#compile_to_build(path) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/tres/template_manager.rb', line 21

def compile_to_build path
  mkdir_p @build
  return copy(@templates/path, @build/path) if path == 'index.html'
  mkdir_p @build/'templates'/dirname(path)
  if static? path
    copy @templates/path, @build/'templates'/path
  else
    template = Tilt.new @templates/path
    destination = compiled_extension(@build/'templates'/path)
    create_file destination, template.render
  end
end

#compile_to_templates_js(path) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/tres/template_manager.rb', line 34

def compile_to_templates_js path
  contents = static?(path) ? read_file(@templates/path) : Tilt.new(@templates/path).render
  unless file?(@assets/'javascripts'/'templates.js')
    copy Tres.templates_dir/'templates.js', @assets/'javascripts'
  end
  remove_from_templates_js(path) if in_templates_js?(path)
  append_to_file @assets/'javascripts'/'templates.js', jst_format(path, contents)
end

#new_template(path, contents = "") ⇒ Object



43
44
45
46
47
48
# File 'lib/tres/template_manager.rb', line 43

def new_template path, contents = ""
  mkdir_p @templates/dirname(path)
  path = path + '.haml' if extname(path).blank?
  raise Tres::TemplateExistsError if file?(@templates/path)
  create_file @templates/path, contents
end

#remove_template(path) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/tres/template_manager.rb', line 60

def remove_template path
  if path.is_a? Array
    path.each do |file|
      remove_template file
    end
  end
  return false if path =~ /index/
  remove_from_templates_js path
  delete! @templates/path
  delete! @build/path
end