Class: Serve::DynamicHandler

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

Overview

:nodoc:

Defined Under Namespace

Classes: Context, Parser

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from FileTypeHandler

#content_type, extension, find, handlers, #initialize

Constructor Details

This class inherits a constructor from Serve::FileTypeHandler

Class Method Details

+ (Object) extensions



7
8
9
10
# File 'lib/serve/handlers/dynamic_handler.rb', line 7

def self.extensions
  # Get extensions from Tilt, ugly but it works
  @extensions ||= (Tilt.mappings.map { |k,v| ["#{k}", "html.#{k}"] } << ["slim", "html.slim"]).flatten
end

Instance Method Details

- (Object) extensions



12
13
14
# File 'lib/serve/handlers/dynamic_handler.rb', line 12

def extensions
  self.class.extensions
end

- (Object) find_layout_for(filename)



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/serve/handlers/dynamic_handler.rb', line 36

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 = extensions.map do |ext|
      l = "_layout.#{ext}"
      possible_layout = File.join(root, path, l)
      File.file?(possible_layout) ? possible_layout : false
    end
    layout = possible_layouts.detect { |o| o }
  end
  layout
end

- (Object) install_view_helpers(context)



52
53
54
55
56
57
# File 'lib/serve/handlers/dynamic_handler.rb', line 52

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

- (Object) parse(request, response)



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

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

- (Object) process(request, response)



18
19
20
21
# File 'lib/serve/handlers/dynamic_handler.rb', line 18

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