Liars is a tiny MVC web framework.
"liars" = "#'rail''rail'.reverses". It's a very quick and dirty hack, just demonstrating some concepts. As its name said, Don't trust it. :-)
Install
gem install liars.
Usage
- Create a new scaffold file. The command below generates a scaffold ruby file named test.rb. liars -s test
- Run it. The command below also generates a database file named db.yaml. liars test.rb
- Now lauch your browser to http://localhost:2008
Help
liars --help
An extra blog example:
Copy code below blog.rb to a new ruby file such as blog.rb, then run:
liars blog.rb -d blog.yaml
blog.rb:
module Liars
# Model ======================================================================
class Article < M
has_field :title
has_field :content
has_many :comments
end
class Comment < M
has_field :title
has_field :content
belongs_to :article
end
class ArticleC < C
route_of :index, '/', '/index', '/default'
def index
@articles = Article.find(:all)
render
end
route_of :show, '/show/(\d+)'
def show(id)
@article = Article.find(id.to_i)
render
end
route_of :create, '/create'
def create
Article.create(params[:article])
redirect_to '/'
end
route_of :add_comment, '/add_comment/(\d+)'
def add_comment(id)
@article = Article.find(id.to_i)
@article.comments << Comment.create(params[:comment])
@article.save
redirect_to "/show/#{id}"
end
route_of :style, '/styles.css'
def style
@headers["Content-Type"] = "text/css; charset=utf-8"
@body = %[
body {
font-family: Utopia, Georga, serif;
}
h1 {
background-color: #fef;
margin: 0; padding: 10px;
}
div {
padding: 10px;
}
]
end
end
class ArticleV < V
def layout
html do
head do
title 'Liars never tell lies!'
link :rel => 'stylesheet', :type => 'text/css',
:href => '/styles.css', :media => 'screen'
end
body do
h1 { a 'Articles', :href => "/" }
div do
yield
end
end
end
end
def index
h1 'Articles'
hr
@articles.each do |ar|
b ar.title
p ar.content
a 'show comments', :href=> "/show/#{ar.id}"
hr
end
form :action=>'/create', :method=>'Post' do
input :type=>'text', :name=>'article[title]'; br
textarea '', :name=>'article[content]'; br
input :type=>'submit'
end
end
def show
h1 'Articles'
a 'index', :href=> "/"
hr
b @article.title
p @article.content
h1 'Comments'
hr
@article.comments.each do |c|
b c.title
p c.content
end
hr
form :action=> "/add_comment/#{@article.id}", :method=>'Post' do
input :type=>'text', :name=>'comment[title]'; br
textarea '', :name=>'comment[content]'; br
input :type=>'submit'
end
end
end
end