Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c0f6fa2
commit f2991cc
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |