class Rasti::Web::Render

Attributes

request[R]
response[R]
view_context[R]

Public Class Methods

new(request, response) click to toggle source
# File lib/rasti/web/render.rb, line 7
def initialize(request, response)
  @request = request
  @response = response
  @view_context = ViewContext.new request, response
end

Public Instance Methods

css(stylesheet, *args) click to toggle source
# File lib/rasti/web/render.rb, line 43
def css(stylesheet, *args)
  respond_with extract_status(args),
               extract_headers(args).merge(Headers.for_css),
               stylesheet
end
data(content, *args) click to toggle source
# File lib/rasti/web/render.rb, line 55
def data(content, *args)
  respond_with extract_status(args),
               extract_headers(args),
               content
end
file(filename, *args) click to toggle source
# File lib/rasti/web/render.rb, line 49
def file(filename, *args)
  respond_with extract_status(args),
               Headers.for_file(filename).merge(extract_headers(args)),
               File.read(filename)
end
html(html, *args) click to toggle source
# File lib/rasti/web/render.rb, line 25
def html(html, *args)
  respond_with extract_status(args),
               extract_headers(args).merge(Headers.for_html),
               html
end
js(script, *args) click to toggle source
# File lib/rasti/web/render.rb, line 37
def js(script, *args)
  respond_with extract_status(args),
               extract_headers(args).merge(Headers.for_js),
               script
end
json(object, *args) click to toggle source
# File lib/rasti/web/render.rb, line 31
def json(object, *args)
  respond_with extract_status(args),
               extract_headers(args).merge(Headers.for_json),
               object.is_a?(String) ? object : JSON.dump(object)
end
layout(template=nil, &block) click to toggle source
# File lib/rasti/web/render.rb, line 66
def layout(template=nil, &block)
  content = block.call if block
  layout = view_context.render(template || Web.default_layout) { content }

  response.headers.merge! Headers.for_html
  response.write layout
end
partial(template, locals={}) click to toggle source
# File lib/rasti/web/render.rb, line 61
def partial(template, locals={})
  response.headers.merge! Headers.for_html
  response.write view_context.render(template, locals)
end
status(status, *args) click to toggle source
# File lib/rasti/web/render.rb, line 13
def status(status, *args)
  respond_with status,
               extract_headers(args),
               extract_body(args)
end
text(text, *args) click to toggle source
# File lib/rasti/web/render.rb, line 19
def text(text, *args)
  respond_with extract_status(args),
               extract_headers(args).merge(Headers.for_text),
               text
end
view(template, locals={}, layout_template=nil) click to toggle source
# File lib/rasti/web/render.rb, line 74
def view(template, locals={}, layout_template=nil)
  layout(layout_template) { view_context.render template, locals }
end

Private Instance Methods

extract_body(args) click to toggle source
# File lib/rasti/web/render.rb, line 94
def extract_body(args)
  args.detect { |a| a.is_a? String }
end
extract_headers(args) click to toggle source
# File lib/rasti/web/render.rb, line 90
def extract_headers(args)
  args.detect { |a| a.is_a? Hash } || {}
end
extract_status(args) click to toggle source
# File lib/rasti/web/render.rb, line 86
def extract_status(args)
  args.detect { |a| a.is_a? Integer }
end
respond_with(status, headers, body) click to toggle source
# File lib/rasti/web/render.rb, line 80
def respond_with(status, headers, body)
  response.status = status if status
  response.headers.merge! headers
  response.write body if body
end