Skip to content
Browse files

#{`curl http://whatthecommit.com/`}

  • Loading branch information...
0 parents commit 1b7fff75c6348cfd8761fa538c79f94bb22ac00b @rkh committed Mar 8, 2011
Showing with 173 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +112 −0 README.md
  3. +8 −0 almost_sinatra.rb
  4. +52 −0 example.rb
1 .gitignore
@@ -0,0 +1 @@
+annoying people
112 README.md
@@ -0,0 +1,112 @@
+# Almost Sinatra
+
+A Sinatra implementation in less than 10 lines.
+Dependencies: Tilt and Rack (like Sinatra) and [Backports](https://github.com/marcandre/backports/).
+Only works on Ruby 1.9.
+
+Features:
+
+* all template engines Sinatra supports
+* get/post/put/delete
+* before filters
+* configure/set/enable/disable/...
+* session/params
+* support for helpers
+* stand alone usage
+* inline templates
+* thread safe
+
+Features missing:
+
+* serving static files, use `Rack::File` or something, or code like this:
+
+ get('/foo.txt') { File.read '/foo.txt' }
+
+ or just don't do it. Patch for this will be accepted if it doesn't add
+ another line.
+
+## Installation
+
+Copy the contents of `almost_sinatra.rb` into your app file (at the top), that
+way you also avoid running the wrong version by accident.
+
+## Try it
+
+ $ ruby example.rb
+ What Sinatra implementation should I use?
+ [R]eal Sinatra
+ [A]lmost Sinatra
+ >> a
+
+## Modular style
+
+When pasting the Almost Sinatra code into your file, replace `Application=$a`
+with `Base=$a` (second line atm, but don't expect me to update the readme if
+I move stuff around).
+
+## Performance
+
+It's just amazing. No code is faster than no code.
+
+ $ sloccount alomst_sinatra.rb
+ Total Physical Source Lines of Code (SLOC) = 8
+ Development Effort Estimate, Person-Years (Person-Months) = 0.00 (0.02)
+ (Basic COCOMO model, Person-Months = 2.4 * (KSLOC**1.05))
+ Schedule Estimate, Years (Months) = 0.04 (0.51)
+ (Basic COCOMO model, Months = 2.5 * (person-months**0.38))
+ Estimated Average Number of Developers (Effort/Schedule) = 0.03
+ Total Estimated Cost to Develop = $ 170
+ (average salary = $56,286/year, overhead = 2.40).
+
+ $ sloccount ../sinatra
+ Total Physical Source Lines of Code (SLOC) = 5,771
+ Development Effort Estimate, Person-Years (Person-Months) = 1.26 (15.12)
+ (Basic COCOMO model, Person-Months = 2.4 * (KSLOC**1.05))
+ Schedule Estimate, Years (Months) = 0.58 (7.02)
+ (Basic COCOMO model, Months = 2.5 * (person-months**0.38))
+ Estimated Average Number of Developers (Effort/Schedule) = 2.15
+ Total Estimated Cost to Develop = $ 170,198
+ (average salary = $56,286/year, overhead = 2.40).
+
+As you can see Sinatra is 1000000% more expensive than Almost Sinatra!
+
+Generated using David A. Wheeler's 'SLOCCount'.
+
+## Coding guidelines
+
+* keep code under ten lines, this includes require and whatnot
+
+* avoid spaces and newlines if possible
+
+* wrap at 200 characters to make it readable on a terminal
+
+* newlines may be used instead of `;` to improve readability
+
+* use `map` instead of `each`, it's shorter
+
+* store constants in global variables to dry up your code (like `$f = File`)
+
+* if you have one loop, reuse it. instead of this:
+
+ ['a','b'].map{|e|...}
+ [Rack::Something].map{|e|...}
+
+ do this:
+
+ ['a','b',Rack::Something].map{|e|(e==e.to_s)?(...):(...)}
+
+ Safes a line!
+
+* `e.to_s==e` is shorter than `e.is_a? String` or `String===e`
+
+* use `->{}` instead of `proc` or `lambda` (this is why it depends on 1.9)
+
+* add methods for class scope to `$o` and for instance scope to `$a` (or `$o`),
+ see http://www.sinatrarb.com/intro#Scopes%20and%20Binding
+
+* don't include tests. tests just bloat the code base. just commit, the users
+ will complain if you break anything.
+
+## About Versioning
+
+Versions are to Software what Git is to SVN.
8 almost_sinatra.rb
@@ -0,0 +1,8 @@
+['rack', 'tilt', 'backports', :INT, :TERM].map{|l|(l==l.to_s)?(require(l)):(trap(l){$r.stop})}
+Sinatra=Module.new{$a,$o,$d,$h,$s,$f=Rack::Builder.new,Object,:define_method,Hash,/@@ *([^\n]+)\n(((?!@@)[^\n]*\n)*)/m,File;Application=$a}
+%w[get post put delete].map{|m|$o.send($d,m){|u,&b|$a.map(u){run->(e){[200,{"Content-Type"=>"text/html"},[$a.instance_eval(&b)]]}}}}
+Tilt.mappings.map{|k,v|$o.send($d,k){|n,*o|$t||=$f.read(caller.first[/^[^:]+/]).scan($s).inject({}){|h,(a,b,c)|h[a]=b;h};v.new(*o){(n.to_s==n)?(n):($t[n.to_s])}.render($a,o[0].try(:[],:locals)||{})}}
+%w[set enable disable configure helpers use].map{|m|$o.send($d,m){|*_,&b|b.try(:[])}};at_exit{Rack::Handler.get("webrick").run($a,:Port=>4567){|s|$r=s}}
+['params', 'session', Rack::Session::Cookie, Rack::Lock].map{|m|(m==m.to_s)?($o.send($d,m){$q.send(m)}):($a.use(m))}
+$o.send($d,:before){|&b|$a.use(Rack::Config,&b)};before{|e|$q=Rack::Request.new(e);$q.params.dup.map{|k,v|params[k.to_sym]=v}}
+puts "== Almost Sinatra/No Version has taken the stage on 4567 for development with backup from Webrick"
52 example.rb
@@ -0,0 +1,52 @@
+until defined? Sinatra
+ puts "What Sinatra implementation should I use?",
+ "[R]eal Sinatra", "[A]lmost Sinatra"
+ print ">> "
+ case gets.strip.downcase
+ when 'a' then require_relative "almost_sinatra"
+ when 'r' then require 'sinatra'
+ else puts "invalid input"
+ end
+end
+
+configure do
+ enable :sessions
+end
+
+helpers do
+ def inc_counter
+ session[:counter] ||= 0
+ session[:counter] += 1
+ end
+end
+
+before do
+ puts " yay! got a request ".center(80, "=")
+end
+
+get '/' do
+ @title = "Almost Sinatra"
+ haml :index
+end
+
+get '/hello' do
+ erb :hello, :locals => { :name => params[:name] }
+end
+
+get '/counter' do
+ inc_counter
+ session[:counter].to_s
+end
+
+__END__
+
+@@ index
+%html
+ %head
+ %title= @title
+ %body
+ %a{:href => '/hello?name=World'} Say hello!
+ %a{:href => '/counter'} Show Counter
+
+@@ hello
+Hello <%= name %>!

0 comments on commit 1b7fff7

Please sign in to comment.
Something went wrong with that request. Please try again.