Class: Serve::DynamicHandler

Inherits:
FileTypeHandler show all
Defined in:
lib/serve/handlers/dynamic_handler.rb

Overview

:nodoc:

Defined Under Namespace

Modules: ERB Classes: Context, Parser

Instance Method Summary collapse

Methods inherited from FileTypeHandler

#content_type, extension, find, handlers, #initialize

Constructor Details

This class inherits a constructor from Serve::FileTypeHandler

Instance Method Details

#find_layout_for(filename) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/serve/handlers/dynamic_handler.rb', line 23

def find_layout_for(filename)
  root = @root_path
  path = filename[root.size..-1]
  layout = nil
  until layout or path == "/"
    path = File.dirname(path)
    possible_layouts = ['_layout.haml', '_layout.html.haml', '_layout.erb', '_layout.html.erb'].map do |l|
      possible_layout = File.join(root, path, l)
      File.file?(possible_layout) ? possible_layout : false
    end
    layout = possible_layouts.detect { |o| o }
  end
  layout
end

#install_view_helpers(context) ⇒ Object



38
39
40
41
42
43
# File 'lib/serve/handlers/dynamic_handler.rb', line 38

def install_view_helpers(context)
  view_helpers_file_path = @root_path + '/view_helpers.rb'
  if File.file?(view_helpers_file_path)
    context.metaclass.module_eval(File.read(view_helpers_file_path) + "\ninclude ViewHelpers", view_helpers_file_path)
  end
end

#parse(request, response) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/serve/handlers/dynamic_handler.rb', line 10

def parse(request, response)
  context = Context.new(@root_path, request, response)
  install_view_helpers(context)
  parser = Parser.new(context)
  context.content << parser.parse_file(@script_filename)
  layout = find_layout_for(@script_filename)
  if layout
    parser.parse_file(layout)
  else
    context.content
  end
end

#process(request, response) ⇒ Object



5
6
7
8
# File 'lib/serve/handlers/dynamic_handler.rb', line 5

def process(request, response)
  response.headers['content-type'] = content_type
  response.body = parse(request, response)
end