public
Description: The invisible framework
Homepage: http://macournoyer.com/
Clone URL: git://github.com/macournoyer/invisible.git
Search Repo:
macournoyer (author)
Sat Feb 09 10:39:46 -0800 2008
commit  b038e8526a54cd0cbc66aa8968429c76479a17b2
tree    873948aa414b7667226ff605530bb099e9c5efa7
invisible / invisible.rb
100644 37 lines (35 sloc) 1.137 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Start w/ thin start -r invisible.rb
require 'tenjin'
 
module ::Invisible
  class Adapter
    def initialize
      @template = Tenjin::Engine.new(:postfix=>'.rbhtml', :layout=>'../layout.rbhtml', :path=>'home')
      @file = Rack::File.new('public')
    end
    def call(env)
      path = env['PATH_INFO']
      if path.include?('.')
        @file.call(env)
      else
        _, controller, action = env['PATH_INFO'].split('/')
        Invisible.const_get("#{(controller || 'home').capitalize}Controller").new(@template, env).call(action || 'index')
      end
    end
  end
  class Controller
    def initialize(template, env)
      @template, @status, @env, @headers = template, 200, env, {'Content-Type' => 'text/html'}
    end
    def call(action)
      send(action)
      render(action) unless @body
      [@status, @headers.merge('Content-Length'=>@body.size.to_s), [@body]]
    end
    protected
      def render(action=nil)
        @body = @template.render(action.to_sym, instance_variables.inject({}) {|h,v| h[v[1..-1]] = instance_variable_get(v); h})
      end
  end
end
 
require 'app/controllers'
run Invisible::Adapter.new