require 'rubygems'
require 'ramaze'
require 'ramaze/store/default'

DB = Ramaze::Store::Default.new('blog.yaml')

Post = Struct.new(:title, :body, :created_at)

class MainController < Ramaze::Controller

def index
@posts = []
DB.each do |key,value|
@posts << value
end
%q~
<a href="/new">New</a>
<br/>
<?r @posts.sort {|x,y| x.created_at <=> y.created_at }.each do |post| ?>
<h2>#{post.title}</h2>
<p><small>#{post.created_at}</small></p>
<p>#{post.body}</p>
<?r end ?>
~
end

def my_layout
%q~
<html>
<head>
<title>Ramaze TumbleLog</title>
<link href="/style.css" media="screen" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>My Ramaze Blog</h1>
#{@content}
</body>
</html>
~
end
layout :my_layout

def new
%q~
<h2>Create a new post</h2>
<br/>
<form action="/create">
Title:<br/>
<input type="text" name="title" size="50" /><br/><br/>
Body:<br/>
<textarea name="body"></textarea><br/><br/>
<input type="submit" />
</form>
~
end

def create
post = Post.new
post.title = request[:title]
post.body = request[:body]
post.created_at = Time.now
DB[rand(1_000_000_000)] = post
redirect Rs(:/)
end

define_method 'style.css' do
%q~
h1 { background-color: red; color: #fff; padding: 10px; font-size: 2em; }
h2 { border-bottom: 2px dotted #bababa; padding-bottom: 3px; margin-bottom: 0px;}
textarea { width: 400px; height: 100px; }
~
end
deny_layout 'style.css'

end

Ramaze.start