class Docks::Renderers::Base

Public Class Methods

new() click to toggle source
# File lib/docks/renderers/base_renderer.rb, line 4
def initialize; @cache = {} end

Public Instance Methods

ivars=(ivars) click to toggle source
# File lib/docks/renderers/base_renderer.rb, line 10
def ivars=(ivars)
  ivars.each do |ivar, value|
    instance_variable_set("@#{ivar}".to_sym, value)
  end
end
render(template, locals = {}) click to toggle source
# File lib/docks/renderers/base_renderer.rb, line 6
def render(template, locals = {})
  raise NotImplementedError.new("You must provide a `render` method.")
end

Private Instance Methods

clean() click to toggle source
# File lib/docks/renderers/base_renderer.rb, line 58
def clean; @cache = {} end
normalize_content_and_locals(template, locals = {}) click to toggle source
# File lib/docks/renderers/base_renderer.rb, line 18
def normalize_content_and_locals(template, locals = {})
  content = nil
  layout = nil
  must_be_partial = false

  if template.kind_of?(Hash)
    locals = template.fetch(:locals, {})
    content = template.fetch(:inline, nil)
    layout = template.fetch(:layout, nil) || nil # guard against false layouts

    if template[:partial].nil?
      template = template[:template]
    else
      template = template[:partial]
      must_be_partial = true
    end
  else
    layout = locals.delete(:layout)
    layout = nil unless layout
    locals = locals.delete(:locals) if locals.key?(:locals)
  end

  if content.nil?
    options = {}
    options[:must_be] = :partial if must_be_partial

    found_template = Templates.search_for_template(template, options)
    content = @cache[found_template] || File.read(found_template)
    @cache[found_template] = content
  end

  if layout
    found_layout = Templates.search_for_template(layout, must_be: :layout)
    layout = @cache[found_layout] || File.read(found_layout)
    @cache[found_layout] = layout
  end

  return content, layout, locals
end