Class: Rodakase::View::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/rodakase/view/renderer.rb

Constant Summary collapse

TemplateNotFoundError =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, options = {}) ⇒ Renderer

Returns a new instance of Renderer.



17
18
19
20
21
22
# File 'lib/rodakase/view/renderer.rb', line 17

def initialize(dir, options = {})
  @dir = dir
  @root = options.fetch(:root, dir)
  @engine = options[:engine]
  @tilts = self.class.tilts
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



11
12
13
# File 'lib/rodakase/view/renderer.rb', line 11

def dir
  @dir
end

#engineObject (readonly)

Returns the value of attribute engine.



11
12
13
# File 'lib/rodakase/view/renderer.rb', line 11

def engine
  @engine
end

#rootObject (readonly)

Returns the value of attribute root.



11
12
13
# File 'lib/rodakase/view/renderer.rb', line 11

def root
  @root
end

#tiltsObject (readonly)

Returns the value of attribute tilts.



11
12
13
# File 'lib/rodakase/view/renderer.rb', line 11

def tilts
  @tilts
end

Class Method Details

.tiltsObject



13
14
15
# File 'lib/rodakase/view/renderer.rb', line 13

def self.tilts
  @__engines__ ||= {}
end

Instance Method Details

#call(template, scope, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/rodakase/view/renderer.rb', line 24

def call(template, scope, &block)
  path = lookup(template)

  if path
    render(path, scope, &block)
  else
    raise TemplateNotFoundError, "Template #{template} could not be looked up within #{root}"
  end
end

#chdir(dirname) ⇒ Object



62
63
64
# File 'lib/rodakase/view/renderer.rb', line 62

def chdir(dirname)
  self.class.new(dir.join(dirname), engine: engine, root: root)
end

#lookup(name) ⇒ Object



42
43
44
# File 'lib/rodakase/view/renderer.rb', line 42

def lookup(name)
  template?(name) || template?("shared/#{name}") || !root? && chdir('..').lookup(name)
end

#path(name) ⇒ Object



58
59
60
# File 'lib/rodakase/view/renderer.rb', line 58

def path(name)
  dir.join("#{name}.#{engine}")
end

#render(path, scope, &block) ⇒ Object



34
35
36
# File 'lib/rodakase/view/renderer.rb', line 34

def render(path, scope, &block)
  tilt(path).render(scope, &block)
end

#root?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/rodakase/view/renderer.rb', line 46

def root?
  dir == root
end

#template?(name) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
# File 'lib/rodakase/view/renderer.rb', line 50

def template?(name)
  template_path = path(name)

  if File.exist?(template_path)
    template_path
  end
end

#tilt(path) ⇒ Object



38
39
40
# File 'lib/rodakase/view/renderer.rb', line 38

def tilt(path)
  tilts.fetch(path) { tilts[path] = Tilt[engine].new(path) }
end