Class: Rodakase::View::Renderer
- Inherits:
-
Object
- Object
- Rodakase::View::Renderer
- Defined in:
- lib/rodakase/view/renderer.rb
Constant Summary collapse
- TemplateNotFoundError =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#engine ⇒ Object
readonly
Returns the value of attribute engine.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#tilts ⇒ Object
readonly
Returns the value of attribute tilts.
Class Method Summary collapse
Instance Method Summary collapse
- #call(template, scope, &block) ⇒ Object
- #chdir(dirname) ⇒ Object
-
#initialize(dir, options = {}) ⇒ Renderer
constructor
A new instance of Renderer.
- #lookup(name) ⇒ Object
- #path(name) ⇒ Object
- #render(path, scope, &block) ⇒ Object
- #root? ⇒ Boolean
- #template?(name) ⇒ Boolean
- #tilt(path) ⇒ Object
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, = {}) @dir = dir @root = .fetch(:root, dir) @engine = [:engine] @tilts = self.class.tilts end |
Instance Attribute Details
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
11 12 13 |
# File 'lib/rodakase/view/renderer.rb', line 11 def dir @dir end |
#engine ⇒ Object (readonly)
Returns the value of attribute engine.
11 12 13 |
# File 'lib/rodakase/view/renderer.rb', line 11 def engine @engine end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
11 12 13 |
# File 'lib/rodakase/view/renderer.rb', line 11 def root @root end |
#tilts ⇒ Object (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
.tilts ⇒ Object
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 |
#template?(name) ⇒ 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 |