class Cubic::Render

Render template is responsible for serving up files from the views directory.

Attributes

params[RW]
template[R]

Public Class Methods

new(params, block) click to toggle source
# File lib/cubic/render.rb, line 10
def initialize(params, block)
  @params = params
  instance_exec(&block)
end

Private Instance Methods

application_view(&block) click to toggle source

To keep things dry, application_view inserts HTML from a rendered view into the app/views/layout/application.haml file.

# File lib/cubic/render.rb, line 57
def application_view(&block)
  template = read_file('layout/application', 'haml')
  Haml::Engine.new(template).render(self, &block)
end
build_response(body, status = 200, headers = {}) click to toggle source

Create a response that will satisfy Rack::Response

# File lib/cubic/render.rb, line 38
def build_response(body, status = 200, headers = {})
  @template = { body: body, status: status, headers: headers }
end
erb(path) click to toggle source
# File lib/cubic/render.rb, line 47
def erb(path)
  build_response(ERB.new(read_file(path, 'erb')).result(binding))
end
haml(path) click to toggle source
# File lib/cubic/render.rb, line 42
def haml(path)
  haml = Haml::Engine.new(read_file(path, 'haml'))
  build_response(application_view { haml.render(self) })
end
html(path) click to toggle source
# File lib/cubic/render.rb, line 29
def html(path)
  build_response(read_file(path, 'html'))
end
json(hash) click to toggle source
# File lib/cubic/render.rb, line 33
def json(hash)
  build_response(JSON.generate(hash), 200, 'content-type' => 'application/json')
end
read_file(path, template_engine) click to toggle source
# File lib/cubic/render.rb, line 51
def read_file(path, template_engine)
  File.read(File.join(view_path, "#{path}.#{template_engine}"))
end
redirect(url, status = 302) click to toggle source
# File lib/cubic/render.rb, line 21
def redirect(url, status = 302)
  redir = proc do |response|
    response.redirect(url, status)
  end

  build_response(redir)
end
render(render_method, path) click to toggle source
# File lib/cubic/render.rb, line 17
def render(render_method, path)
  send(render_method, path)
end
view_path() click to toggle source

Returns the absolute path to the applications views directory.

# File lib/cubic/render.rb, line 63
def view_path
  File.join(APP_PATH, 'app/views')
end