Created
June 12, 2009 20:54
-
-
Save carllerche/128913 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p><%= @name %> is awesome</p> | |
<p>OMG: <%= link_to "click", url(:hello) %></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hello world! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "active_support/core_ext" | |
require "action_controller/new_base/http" | |
require "rack/router" | |
module Rails | |
class Sinatra < ActionController::Http | |
include AbstractController::Renderer | |
include AbstractController::Callbacks | |
include AbstractController::Helpers | |
extlib_inheritable_accessor :router | |
class << self | |
def _routes() @_routes ||= [] end | |
def get(uri, opts={}, &block) route(:get, uri, opts, &block) end | |
def post(uri, opts={}, &block) route(:post, uri, opts, &block) end | |
def put(uri, opts={}, &block) route(:put, uri, opts, &block) end | |
def delete(uri, opts={}, &block) route(:delete, uri, opts, &block) end | |
def route(method, uri, opts, &block) | |
method = method.to_s.downcase | |
action_name = "[#{method}] #{uri}" | |
_routes << { :method => method, :uri => uri, :options => opts } | |
define_method(uri, &block) | |
end | |
def set(config, value) | |
self._view_paths = ActionView::Base.process_view_paths(value) if config == :views | |
end | |
def to_app(fallback = nil) | |
self.router = Rack::Router.new(fallback) do |r| | |
_routes.each { |route| | |
r.map route[:uri], route[:method], (route[:options] || {}).merge(:to => action(route[:uri])) | |
} | |
end | |
end | |
end | |
module Helpers | |
def url(*args) controller.class.router.url(*args) end | |
def link_to(text, url) %[<a href="#{url}">#{text}</a>] end | |
def params() controller.params end | |
end | |
helper Helpers | |
def halt(msg) | |
throw :halt, msg | |
end | |
def params ; @_env["rack_router.params"] ; end | |
def render(name, options = {}) | |
options[:_template_name] = name | |
super(options) | |
end | |
def render_to_body(options = {}) | |
options[:_prefix] = "" | |
super | |
end | |
def process_action(*) | |
retval = catch(:halt) { super } | |
self.response_body ||= retval | |
self.headers["Content-Type"] ||= "text/html" | |
self.status ||= 200 | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "rubygems" | |
$:.unshift "/Users/carllerche/Developer/Source/rack-router/lib" | |
$:.unshift "/Users/carllerche/Developer/Source/rails/actionpack/lib" | |
$:.unshift "/Users/carllerche/Developer/Source/rails/activesupport/lib" | |
$:.unshift File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')) | |
require "rails_sinatra" | |
# * DSL | |
# * Routing | |
# * Templates | |
# * Callbacks | |
# * Helpers | |
class MyApp < Rails::Sinatra | |
set :views, File.dirname(__FILE__) | |
get "/hello", :name => :hello do | |
render :greetings | |
end | |
get "/world" do | |
@name = "Carl" | |
render :awesome | |
end | |
end | |
class MyOtherApp < Rails::Sinatra | |
before_filter do | |
halt "EJECTED!" | |
end | |
get "/goodbye" do | |
"Nope" | |
end | |
end | |
run Rack::Router.new { |r| | |
r.map "/", :to => MyApp.to_app | |
r.map "/other", :to => MyOtherApp.to_app | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment