Skip to content

Commit

Permalink
first code commit
Browse files Browse the repository at this point in the history
  • Loading branch information
julesjacobs committed Nov 11, 2010
1 parent c0f6fa2 commit f2991cc
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
11 changes: 11 additions & 0 deletions arcchallenge.rb
@@ -0,0 +1,11 @@
require 'sinatra'
require './raamwerk.rb'

page '/' do
form do
msg = input()
submit do
link("Click me"){ puts "You said #{msg}" }
end
end
end
12 changes: 12 additions & 0 deletions count.rb
@@ -0,0 +1,12 @@
require 'sinatra'
require './raamwerk.rb'

def counter(i)
puts "the counter is #{i}"
link("increase") { counter(i+1) }
link("decrease") { counter(i-1) }
end

page '/' do
counter(0)
end
43 changes: 43 additions & 0 deletions raamwerk.rb
@@ -0,0 +1,43 @@
def capture
t,$stdout = $stdout,StringIO.new
yield
t,$stdout = $stdout,t
t.string
end

def tag(name, attrs={})
puts "<#{name}#{attrs.map{|k,v| " #{k}='#{v}'"}.join}>#{capture{yield}}</#{name}>"
end

def link(text)
url = '/' + rand(36**9).to_s(36)
page(url){yield}
tag(:a, :href => url){puts text}
end

def form
content = capture{yield}
tag(:form, :action => $formurl, :method => :post){puts content}
end

def submit
$formurl = '/' + rand(36**9).to_s(36)
page($formurl){yield}
tag(:input, :type => :submit){}
end

$inputs = Hash.new{|h,k| h[k] = ""}

def input
id = rand(36**9).to_s(36)
tag(:input, :name => id){}
$inputs[id]
end

def page(s)
get(s){ capture{yield} }
post(s){
params.each{|(k,v)| $inputs[k].replace(v) }
capture{yield}
}
end
21 changes: 21 additions & 0 deletions todo.rb
@@ -0,0 +1,21 @@
require 'sinatra'
require './raamwerk.rb'

$todo = []

def todo
tag(:ul) do
$todo.each do |x|
tag(:li){puts x; link("delete"){ $todo.delete(x); todo }}
end
end

form do
t = input()
submit { $todo << t; todo }
end
end

page '/' do
todo
end

0 comments on commit f2991cc

Please sign in to comment.