Class: Cutaneous::FileLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/cutaneous/loader.rb

Overview

Converts a template path or Proc into a Template instance for a particular format

Direct Known Subclasses

CachedFileLoader

Defined Under Namespace

Classes: TemplateReader

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_roots, format, extension = Cutaneous.extension) ⇒ FileLoader

Returns a new instance of FileLoader.



8
9
10
11
# File 'lib/cutaneous/loader.rb', line 8

def initialize(template_roots, format, extension = Cutaneous.extension)
  @roots, @format, @extension = template_roots, format, extension
  @template_class = Template
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



6
7
8
# File 'lib/cutaneous/loader.rb', line 6

def format
  @format
end

#syntaxObject

Returns the value of attribute syntax.



4
5
6
# File 'lib/cutaneous/loader.rb', line 4

def syntax
  @syntax
end

#template_class=(value) ⇒ Object (writeonly)

Sets the attribute template_class

Parameters:

  • value

    the value to set the attribute template_class to.



5
6
7
# File 'lib/cutaneous/loader.rb', line 5

def template_class=(value)
  @template_class = value
end

Instance Method Details

#convert(template, to_syntax) ⇒ Object



17
18
19
# File 'lib/cutaneous/loader.rb', line 17

def convert(template, to_syntax)
  template(template).convert(to_syntax)
end

#exists?(template_root, template_name) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/cutaneous/loader.rb', line 50

def exists?(template_root, template_name)
  path = ::File.join(template_root, filename(template_name))
  ::File.exist?(path)
end

#filename(template_name) ⇒ Object



46
47
48
# File 'lib/cutaneous/loader.rb', line 46

def filename(template_name)
  [template_name, @format, @extension].join(".")
end

#lexer(template) ⇒ Object



36
37
38
# File 'lib/cutaneous/loader.rb', line 36

def lexer(template)
  Lexer.new(template, syntax)
end

#location(template_root, template_name) ⇒ Object



55
56
57
58
# File 'lib/cutaneous/loader.rb', line 55

def location(template_root, template_name)
  return ::File.join(template_root, template_name) if exists?(template_root, template_name)
  nil
end

#open_template(template) ⇒ Object



29
30
31
32
33
34
# File 'lib/cutaneous/loader.rb', line 29

def open_template(template)
  template_path = path(template)
  raise UnknownTemplateError.new(@roots, filename(template)) if template_path.nil?
  # TODO: Make the encoding configurable?
  TemplateReader.new(template_path, Encoding::UTF_8)
end

#path(template_name) ⇒ Object



40
41
42
43
44
# File 'lib/cutaneous/loader.rb', line 40

def path(template_name)
  filename = filename(template_name)
  return filename if ::File.exist?(filename) # Test for an absolute path
  @roots.map { |root| ::File.join(root, filename)}.detect { |path| ::File.exist?(path) }
end

#render(template, context) ⇒ Object



13
14
15
# File 'lib/cutaneous/loader.rb', line 13

def render(template, context)
  template(template).render(context)
end

#template(template) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/cutaneous/loader.rb', line 21

def template(template)
  template = open_template(template) if String === template
  instance = @template_class.new(lexer(template))
  instance.path   = template.path if template.respond_to?(:path)
  instance.loader = self
  instance
end