Class: Cutaneous::Engine

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

Overview

Manages a set of Loaders that render templates

Direct Known Subclasses

CachingEngine

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_roots, syntax = Cutaneous::FirstPassSyntax, default_format = "html") ⇒ Engine

Returns a new instance of Engine.



8
9
10
11
12
13
# File 'lib/cutaneous/engine.rb', line 8

def initialize(template_roots, syntax = Cutaneous::FirstPassSyntax, default_format = "html")
  @roots          = Array(template_roots)
  @syntax         = syntax
  @loader_class   = FileLoader
  @default_format = default_format
end

Instance Attribute Details

#default_formatObject

Returns the value of attribute default_format.



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

def default_format
  @default_format
end

#loader_classObject

Returns the value of attribute loader_class.



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

def loader_class
  @loader_class
end

#rootsObject (readonly)

Returns the value of attribute roots.



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

def roots
  @roots
end

Instance Method Details

#convert(template, to_syntax, format = default_format) ⇒ Object



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

def convert(template, to_syntax, format = default_format)
  file_loader(format).convert(template, to_syntax)
end

#convert_string(template_string, to_syntax, format = default_format) ⇒ Object



59
60
61
# File 'lib/cutaneous/engine.rb', line 59

def convert_string(template_string, to_syntax, format = default_format)
  convert(proc_template(template_string), to_syntax, format)
end

#dynamic_template?(template_string) ⇒ Boolean

Returns:

  • (Boolean)


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

def dynamic_template?(template_string)
  @syntax.is_dynamic?(template_string)
end

#file_loader(format) ⇒ Object

Create and cache a file loader on a per-format basis



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

def file_loader(format)
  file_loader_instance(format.to_sym).tap do |loader|
    loader.syntax = @syntax
  end
end

#proc_template(template_string) ⇒ Object



63
64
65
# File 'lib/cutaneous/engine.rb', line 63

def proc_template(template_string)
  Proc.new { template_string }
end

#render_file(path, context, format = default_format) ⇒ Object Also known as: render



15
16
17
# File 'lib/cutaneous/engine.rb', line 15

def render_file(path, context, format = default_format)
  file_loader(format).render(path, context)
end

#render_string(template_string, context, format = default_format) ⇒ Object

need an explicit #render_string method so it’s possible to distinguish between a String which is a path to a template & a String which is a template itself.



24
25
26
# File 'lib/cutaneous/engine.rb', line 24

def render_string(template_string, context, format = default_format)
  render_file(proc_template(template_string), context, format)
end

#template_exists?(relative_path, format) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
# File 'lib/cutaneous/engine.rb', line 35

def template_exists?(relative_path, format)
  @roots.each do |root|
    return true if file_loader(format).exists?(root, relative_path)
  end
  false
end

#template_location(relative_path, format) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/cutaneous/engine.rb', line 42

def template_location(relative_path, format)
  @roots.each do |root|
    if (path = file_loader(format).location(root, relative_path))
      return path
    end
  end
  nil
end