Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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