diff --git a/examples/todo.rb b/examples/todo.rb new file mode 100644 index 0000000..baba05b --- /dev/null +++ b/examples/todo.rb @@ -0,0 +1,28 @@ +#! /usr/bin/env ruby +$:.unshift File.dirname(__FILE__) + "/../lib" +require 'zoid' + +StrokeDB::Config.build :default => true, :base_path => 'todo.strokedb' + +module Todo + + nsurl 'http://strokedb.com/zoid/examples/todo' + + Item = StrokeDB::Meta.new do + def done! + self.done = true + save! + end + end + + List = StrokeDB::Meta.new do + has_many :items + end + + APPLICATION = Zoid::Application.find_or_create(:name => "Todo", :nsurl => Todo.nsurl) + +end + +puts "Serving #{Todo::APPLICATION}" + +Zoid.run(:mongrel, Todo::APPLICATION) diff --git a/lib/zoid.rb b/lib/zoid.rb index 9a1ed54..a853fc6 100644 --- a/lib/zoid.rb +++ b/lib/zoid.rb @@ -1,3 +1,5 @@ $:.unshift File.expand_path(File.dirname(__FILE__)) -require 'zoid/strokedb' \ No newline at end of file +require 'zoid/strokedb' +require 'zoid/rack' +require 'zoid/application' \ No newline at end of file diff --git a/lib/zoid/application.rb b/lib/zoid/application.rb new file mode 100644 index 0000000..323f796 --- /dev/null +++ b/lib/zoid/application.rb @@ -0,0 +1,7 @@ +module Zoid + nsurl "http://strokedb.com/zoid" + Application = StrokeDB::Meta.new do + validates_presence_of :name + validates_presence_of :nsurl + end +end \ No newline at end of file diff --git a/lib/zoid/rack.rb b/lib/zoid/rack.rb new file mode 100644 index 0000000..e3f052f --- /dev/null +++ b/lib/zoid/rack.rb @@ -0,0 +1,56 @@ +require 'rack' + +module Rack #:nodoc: + + class Request #:nodoc: + POST_TUNNEL_METHODS_ALLOWED = %w(PUT DELETE OPTIONS HEAD) + + def request_method + if post_tunnel_method? + params['_method'].upcase + else + @env['REQUEST_METHOD'] + end + end + + private + + def post_tunnel_method? + @env['REQUEST_METHOD'] == 'POST' && POST_TUNNEL_METHODS_ALLOWED.include?(self.POST.fetch('_method', '').upcase) + end + end + + + module Adapter + class Zoid + + def initialize(app) + @app = app + end + + def call(env) + [200,{"Content-Type" => "text/html"}, "Welcome to #{@app.name}!"] + end + + end + end + +end + +module Zoid + + + def self.run(handler,app) + case handler.to_sym + when :mongrel + Rack::Handler::Mongrel.run(Rack::Adapter::Zoid.new(app), :Port => 7777) do |server| + trap(:INT) do + server.stop + end + end + else + raise ArgumentError, "Unknown Rack handler #{handler}" + end + end + +end