Skip to content

Instantly share code, notes, and snippets.

@udzura
Created November 29, 2011 06:36
Show Gist options
  • Select an option

  • Save udzura/1403717 to your computer and use it in GitHub Desktop.

Select an option

Save udzura/1403717 to your computer and use it in GitHub Desktop.
expanding almost-sinatra.rb
%w(rack tilt backports).map do |lib|
require lib
end
%w(INT TERM).map do |sig|
trap(sig){ $r.stop }
end
Sinatra = Module.new {
extend Rack
a = (Application = Builder.new)
D = Object.method(:define_method)
S = /@@ *([^\n]+)\n(((?!@@)[^\n]*\n)*)/m
$p = 4567
q = a
%w[get post put delete].map do |meth|
D.call(meth) do |path, &block|
a.map(path) do
response_app = lambda do |env|
[
200,
{"Content-Type"=>"text/html"},
[a.instance_eval(&block)]
]
end
run(response_app)
end
end
end
Tilt.mappings.map do |k,v|
D.call(k) do |n, *o|
h = {}
app_file = File.read(caller[0][/^[^:]+/])
app_file.scan(S){|a, b| h[a] = b }
$t ||= h
template_engine = v.first
template_engine.new(*o){
n.to_s == n ? n : $t[n.to_s]
}.render(
a,
(o[0].try(:[],:locals) || {})
)
end
end
%w[set enable disable configure helpers use register].each do |m|
D.(m){|*_, &b| b.try :call }
end
END {
Handler.get("webrick").run(a, :Port => $p){|s| $r = s }
}
%w[params session].each do |m|
D.(m){ q.send m }
end
a.use Session::Cookie
a.use Lock
D.(:before){|&b| a.use Rack::Config, &b }
before do |e|
q = Request.new e
q.params.dup.map{|k, v| params[k.to_sym] = v }
end
}
$n = Sinatra
puts "== almost #$n/No Version has taken the stage on #$p for development with backup from Webrick"
@DouglasAllen

Copy link
Copy Markdown

extend Rack? Are you sure you don't mean
include Rack? Builder did not load with extend Rack.
Also there is no method now for Tilt.mappings

@DouglasAllen

DouglasAllen commented Dec 14, 2016

Copy link
Copy Markdown
%w(rack tilt backports).map do |lib|
  require lib
end
%w(INT TERM).map do |sig|
  trap(sig) { $r.stop }
end

Sinatra = Module.new do
  a = (Application = Rack::Builder.new)
  D = Object.method(:define_method)
  S = /@@ *([^\n]+)\n(((?!@@)[^\n]*\n)*)/m
  $p = 4567
  q = a
  %w(get post put delete).map do |meth|
    D.call(meth) do |path, &block|
      a.map(path) do
        response_app = lambda do |_env|
          [
            200,
            { 'Content-Type' => 'text/html' },
            [a.instance_eval(&block)]
          ]
        end
        run(response_app)
      end
    end
  end
  Tilt.default_mapping do |k, v|
    D.call(k) do |n, *o|
      h = {}
      app_file = File.read(caller[0][/^[^:]+/])
      app_file.scan(S) { |a, b| h[a] = b }
      $t ||= h
      template_engine = v.first
      template_engine.new(*o) do
        n.to_s == n ? n : $t[n.to_s]
      end.render(
        a,
        (o[0].try(:[], :locals) || {})
      )
    end
  end
  %w(set enable disable configure helpers use register).each do |m|
    D.call(m) { |*_, &b| b.try :call }
  end
  END {
    Rack::Handler.get('webrick').run(a, Port: $p) { |s| $r = s }
  }
  %w(params session).each do |m|
    D.call(m) { q.send m }
  end
  a.use Session::Cookie
  a.use Lock
  D.call(:before) { |&b| a.use Rack::Config, &b }
  before do |e|
    q = Request.new e
    q.params.dup.map { |k, v| params[k.to_sym] = v }
  end
end
$n = Sinatra
puts "== almost #{$n}/No Version has taken the stage on #{$p} for development with backup from Webrick"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment