summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2011-05-26 18:22:10 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2011-05-26 18:22:10 +0200
commitd23181c138e8f0258641308ffd34370d3b132e8e (patch)
treef0e28aa0fed6b3093f091d12260637fbcc551c47 /lib
downloadzorglub-d23181c138e8f0258641308ffd34370d3b132e8e.zip
zorglub-d23181c138e8f0258641308ffd34370d3b132e8e.tar.gz
initial commit
Diffstat (limited to 'lib')
-rw-r--r--lib/zorglub.rb12
-rw-r--r--lib/zorglub/app.rb43
-rw-r--r--lib/zorglub/node.rb54
3 files changed, 109 insertions, 0 deletions
diff --git a/lib/zorglub.rb b/lib/zorglub.rb
new file mode 100644
index 0000000..f93b4c1
--- /dev/null
+++ b/lib/zorglub.rb
@@ -0,0 +1,12 @@
+#! /usr/bin/env ruby
+# -*- coding: UTF-8 -*-
+#
+module Zorglub
+ #
+ VERSION = '0.0.1'
+ #
+ autoload :App, './lib/zorglub/app.rb'
+ autoload :Node, './lib/zorglub/node.rb'
+ #
+end
+#
diff --git a/lib/zorglub/app.rb b/lib/zorglub/app.rb
new file mode 100644
index 0000000..d568d26
--- /dev/null
+++ b/lib/zorglub/app.rb
@@ -0,0 +1,43 @@
+#! /usr/bin/ruby
+#
+require 'rack'
+#
+module Zorglub
+ #
+ class App < Rack::URLMap
+ #
+ def initialize map={}, &block
+ super
+ @map = map
+ instance_eval &block if block_given?
+ remap @map
+ end
+ #
+ def map location, object
+ return unless location and object
+ raise Exception.new "#{@map[location]} already mapped to #{location}" if @map.has_key? location
+ @map.merge! location.to_s=>object
+ remap @map
+ end
+ #
+ def delete location
+ @map.delete location
+ remap @map
+ end
+ #
+ def at location
+ @map[location]
+ end
+ #
+ def to object
+ @map.invert[object]
+ end
+ #
+ def to_hash
+ @map.dup
+ end
+ #
+ end
+ #
+end
+#
diff --git a/lib/zorglub/node.rb b/lib/zorglub/node.rb
new file mode 100644
index 0000000..e57a299
--- /dev/null
+++ b/lib/zorglub/node.rb
@@ -0,0 +1,54 @@
+#! /usr/bin/ruby
+#
+module Zorglub
+ #
+ class Node
+ #
+ class << self
+ #
+ attr_reader :map
+ def map app, location
+ @app = app
+ @app.map location, self
+ end
+ #
+ def r
+ @r ||= @app.to self
+ end
+ #
+ def call env
+ node = self.new Rack::Request.new(env), Rack::Response.new
+ meth, *args = env['PATH_INFO'][1..-1].split '/'
+ meth||= 'index'
+ return error_404 node if not node.respond_to? meth
+ # TODO use layout
+ # TODO use view
+ # TODO session
+ node.send meth, *args
+ end
+ #
+ def error_404 node
+ resp = node.response
+ resp.status = 404
+ resp['Content-Type'] = 'text/plain'
+ resp.write "%s mapped at %p can't respond to : %p" % [ node.class.name, node.r, node.request.env['PATH_INFO'] ]
+ resp
+ end
+ #
+ end
+ #
+ attr_reader :request, :response
+ #
+ def initialize req, res
+ @request = req
+ @response = res
+ end
+ #
+ def r
+ self.class.r
+ end
+ #
+ end
+ #
+end
+#