Class: Margot
- Inherits:
-
Mongrel::HttpHandler
- Object
- Mongrel::HttpHandler
- Margot
- Defined in:
- lib/margot.rb
Constant Summary collapse
- VERSION =
'0.5.0'- @@cache =
{}
Instance Method Summary collapse
- #compile(file, cgi = @cgi, &block) ⇒ Object
-
#initialize(path) ⇒ Margot
constructor
A new instance of Margot.
- #layout(content) ⇒ Object
- #process(request, response) ⇒ Object
- #serve_file(path) ⇒ Object
Constructor Details
#initialize(path) ⇒ Margot
8 9 10 |
# File 'lib/margot.rb', line 8 def initialize(path) @root = File.(path) end |
Instance Method Details
#compile(file, cgi = @cgi, &block) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/margot.rb', line 41 def compile(file, cgi = @cgi, &block) Markaby::Builder.new do |mab| (class << mab; self end).send(:attr_accessor, :params) mab.params = cgi.params return mab.instance_eval(file) unless file =~ /layout/ mab.instance_eval %[def #{layout_method = "layout_" + file.hash}() #{file} end] send(layout_method, &block) end.to_s end |
#layout(content) ⇒ Object
52 53 54 55 |
# File 'lib/margot.rb', line 52 def layout(content) return content unless layout = serve_file(@request.params['PATH_INFO'].split('/').join('') + 'layout') compile(layout) { content } end |
#process(request, response) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/margot.rb', line 19 def process(request, response) @cgi, @request = Mongrel::CGIWrapper.new(request, response), request status, content_type, body = 404, 'text/html', "<h1>404-File Not Found</h1>" if @@cache[cache_key = request.params['PATH_INFO'] + @cgi.params.sort.to_s] status = 200 body = @@cache[cache_key] elsif file = serve_file(request.params['PATH_INFO']) status = 200 @@cache[cache_key] = (body = layout(compile(file))).dup end response.start status do |head, out| head["Content-Type"] = content_type out << body open('log', 'w') { |f| f << "#{status} - GET: #{request.params['REQUEST_URI']}\n" } puts "#{status} - GET: #{request.params['REQUEST_URI']}" if DEBUG end rescue => e puts e., e.backtrace.map { |line| " " << line } end |
#serve_file(path) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/margot.rb', line 12 def serve_file(path) req_path = File.(File.join(@root, Mongrel::HttpRequest.unescape(path)), @root) if File.file?(file = "#{req_path}.mab") || File.file?(file = "#{req_path}/index.mab") File.open(file).read end end |