Class: Gurk::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/gurk/page.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Page

Returns a new instance of Page.



8
9
10
11
12
13
# File 'lib/gurk/page.rb', line 8

def initialize(data)
  @name = data[:name].downcase
  @locals = data[:locals]
  @route = data[:route]
  @layout= data[:locals][:layout] if data[:locals] && data[:locals][:layout]
end

Instance Attribute Details

#layoutObject

Returns the value of attribute layout.



6
7
8
# File 'lib/gurk/page.rb', line 6

def layout
  @layout
end

#localsObject

Returns the value of attribute locals.



6
7
8
# File 'lib/gurk/page.rb', line 6

def locals
  @locals
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/gurk/page.rb', line 6

def name
  @name
end

#routeObject

Returns the value of attribute route.



6
7
8
# File 'lib/gurk/page.rb', line 6

def route
  @route
end

Instance Method Details

#layout_nameObject



55
56
57
# File 'lib/gurk/page.rb', line 55

def layout_name
  @locals[:layout]
end

#layout_pathObject



59
60
61
62
63
64
65
# File 'lib/gurk/page.rb', line 59

def layout_path
  if layout_name =~ /\.(erb|haml|)/
    Gurk.view_path + '/' + layout_name
  else
    Gurk.view_path + '/' + layout_name + '.' + Gurk.default_view_engine.to_s
  end
end

#render(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gurk/page.rb', line 15

def render(env)
  output = nil

  if @layout
    output = render_template_with_layout(env) do
      render_template(env)
    end
  else
    output = render_template(env)
  end

  [200, {'content_type' => 'text/html'}, [output]]

rescue Errno::ENOENT => e
  raise Gurk::TemplateNotFound.new(e)  
rescue RuntimeError, Exception => e
  # TODO: Make this more sensible
  raise e
end

#render_template(env) ⇒ Object



42
43
44
45
# File 'lib/gurk/page.rb', line 42

def render_template(env)
  tilt = Tilt.new(view_path)
  tilt.render(nil, locals) 
end

#render_template_with_layout(env, &block) ⇒ Object



35
36
37
38
39
40
# File 'lib/gurk/page.rb', line 35

def render_template_with_layout(env, &block)
  tilt = Tilt.new(layout_path)
  tilt.render(nil, locals) do
    yield
  end
end

#view_nameObject



47
48
49
# File 'lib/gurk/page.rb', line 47

def view_name
  @view_name ||= @name
end

#view_pathObject



51
52
53
# File 'lib/gurk/page.rb', line 51

def view_path
  Gurk.view_path + '/' + view_name + '.' + Gurk.default_view_engine.to_s
end