module PUNK::Renderable

Constants

FORMATS

Public Instance Methods

inspect() click to toggle source
# File lib/punk/helpers/renderable.rb, line 44
def inspect
  to_s
end
render(format) click to toggle source
# File lib/punk/helpers/renderable.rb, line 19
def render(format)
  raise NotFound, "unknown format '#{format}'" unless FORMATS.key?(format)
  send(FORMATS[format][:renderer])
end
template(name) click to toggle source
# File lib/punk/helpers/renderable.rb, line 15
def template(name)
  @template = name
end
to_csv(options = {}) click to toggle source
# File lib/punk/helpers/renderable.rb, line 32
def to_csv(options = {})
  _render(:csv, options)
end
to_h() click to toggle source
# File lib/punk/helpers/renderable.rb, line 48
def to_h
  ActiveSupport::JSON.decode(to_json).to_h.deep_symbolize_keys
end
to_html(options = {}) click to toggle source
# File lib/punk/helpers/renderable.rb, line 24
def to_html(options = {})
  _render(:html, options)
end
to_json(options = {}) click to toggle source
# File lib/punk/helpers/renderable.rb, line 28
def to_json(options = {})
  _render(:json, options)
end
to_s() click to toggle source
# File lib/punk/helpers/renderable.rb, line 40
def to_s
  to_json
end
to_xml(options = {}) click to toggle source
# File lib/punk/helpers/renderable.rb, line 36
def to_xml(options = {})
  _render(:xml, options)
end

Protected Instance Methods

_dir() click to toggle source
# File lib/punk/helpers/renderable.rb, line 54
def _dir
  File.join(PUNK.get.app.path, "templates")
end

Private Instance Methods

_path(format) click to toggle source
# File lib/punk/helpers/renderable.rb, line 60
def _path(format)
  raise InternalServerError, "No template given" unless @template
  base = File.join(_dir, @template)
  ext = FORMATS[format][:extension]
  "#{base}.#{ext}"
end
_render(format, options) click to toggle source
# File lib/punk/helpers/renderable.rb, line 67
def _render(format, options)
  path = _path(format)
  raise NotImplemented, "No path for template: #{@template}" unless path
  Tilt.new(path).render(self, options)
rescue LoadError, Errno::ENOENT
  raise NotFound, "Cannot load template: #{path}"
end