Class: Rasti::Web::Render

Inherits:
Object
  • Object
show all
Defined in:
lib/rasti/web/render.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, response) ⇒ Render

Returns a new instance of Render.



7
8
9
10
11
# File 'lib/rasti/web/render.rb', line 7

def initialize(request, response)
  @request = request
  @response = response
  @view_context = ViewContext.new request, response
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



5
6
7
# File 'lib/rasti/web/render.rb', line 5

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



5
6
7
# File 'lib/rasti/web/render.rb', line 5

def response
  @response
end

#view_contextObject (readonly)

Returns the value of attribute view_context.



5
6
7
# File 'lib/rasti/web/render.rb', line 5

def view_context
  @view_context
end

Instance Method Details

#css(stylesheet, *args) ⇒ Object



43
44
45
46
47
# File 'lib/rasti/web/render.rb', line 43

def css(stylesheet, *args)
  respond_with extract_status(args), 
               extract_headers(args).merge('Content-Type' => 'text/css; charset=utf-8'), 
               stylesheet
end

#file(filename, *args) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/rasti/web/render.rb', line 49

def file(filename, *args)
  content_type = MIME::Types.of(filename).first.content_type
  body = File.read filename

  respond_with extract_status(args), 
               extract_headers(args).merge('Content-Type' => content_type), 
               body
end

#html(html, *args) ⇒ Object



25
26
27
28
29
# File 'lib/rasti/web/render.rb', line 25

def html(html, *args)
  respond_with extract_status(args), 
               extract_headers(args).merge('Content-Type' => 'text/html; charset=utf-8'), 
               html
end

#js(script, *args) ⇒ Object



37
38
39
40
41
# File 'lib/rasti/web/render.rb', line 37

def js(script, *args)
  respond_with extract_status(args), 
               extract_headers(args).merge('Content-Type' => 'application/javascript; charset=utf-8'), 
               script
end

#json(object, *args) ⇒ Object



31
32
33
34
35
# File 'lib/rasti/web/render.rb', line 31

def json(object, *args)
  respond_with extract_status(args), 
               extract_headers(args).merge('Content-Type' => 'application/json; charset=utf-8'), 
               object.is_a?(String) ? object : JSON.dump(object)
end

#layout(template = nil, &block) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/rasti/web/render.rb', line 63

def layout(template=nil, &block)
  content = block.call if block
  layout = view_context.render(template || Web.default_layout) { content }
  
  response['Content-Type'] = 'text/html; charset=utf-8'
  response.write layout
end

#partial(template, locals = {}) ⇒ Object



58
59
60
61
# File 'lib/rasti/web/render.rb', line 58

def partial(template, locals={})
  response['Content-Type'] = 'text/html; charset=utf-8'
  response.write view_context.render(template, locals)
end

#status(status, *args) ⇒ Object



13
14
15
16
17
# 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) ⇒ Object



19
20
21
22
23
# File 'lib/rasti/web/render.rb', line 19

def text(text, *args)
  respond_with extract_status(args), 
               extract_headers(args).merge('Content-Type' => 'text/plain; charset=utf-8'), 
               text
end

#view(template, locals = {}, layout_template = nil) ⇒ Object



71
72
73
# File 'lib/rasti/web/render.rb', line 71

def view(template, locals={}, layout_template=nil)
  layout(layout_template) { view_context.render template, locals }
end