| 1 | == Camping, a Microframework |
|---|
| 2 | |
|---|
| 3 | Camping is a web framework which consistently stays at less than 4kb of code. |
|---|
| 4 | You can probably view the complete source code on a single page. But, you know, |
|---|
| 5 | it's so small that, if you think about it, what can it really do? |
|---|
| 6 | |
|---|
| 7 | The idea here is to store a complete fledgling web application in a single file |
|---|
| 8 | like many small CGIs. But to organize it as a Model-View-Controller application |
|---|
| 9 | like Rails does. You can then easily move it to Rails once you've got it going. |
|---|
| 10 | |
|---|
| 11 | A skeleton might be: |
|---|
| 12 | |
|---|
| 13 | require 'camping' |
|---|
| 14 | |
|---|
| 15 | module Camping::Models |
|---|
| 16 | class Post < Base; belongs_to :user; end |
|---|
| 17 | class Comment < Base; belongs_to :user; end |
|---|
| 18 | class User < Base; end |
|---|
| 19 | end |
|---|
| 20 | |
|---|
| 21 | module Camping::Controllers |
|---|
| 22 | class Index < R '/' |
|---|
| 23 | def get |
|---|
| 24 | @posts = Post.find :all |
|---|
| 25 | render :index |
|---|
| 26 | end |
|---|
| 27 | end |
|---|
| 28 | end |
|---|
| 29 | |
|---|
| 30 | module Camping::Views |
|---|
| 31 | def layout |
|---|
| 32 | html do |
|---|
| 33 | body do |
|---|
| 34 | self << yield |
|---|
| 35 | end |
|---|
| 36 | end |
|---|
| 37 | end |
|---|
| 38 | |
|---|
| 39 | def index |
|---|
| 40 | for post in @posts |
|---|
| 41 | h1 post.title |
|---|
| 42 | end |
|---|
| 43 | end |
|---|
| 44 | end |
|---|
| 45 | |
|---|
| 46 | if __FILE__ == $0 |
|---|
| 47 | Camping::Models::Base.establish_connection :adapter => 'sqlite3', :database => 'blog3.db' |
|---|
| 48 | Camping::Models::Base.logger = Logger.new('camping.log') |
|---|
| 49 | Camping.run |
|---|
| 50 | end |
|---|
| 51 | |
|---|
| 52 | Some things you might have noticed: |
|---|
| 53 | |
|---|
| 54 | * Camping::Models uses ActiveRecord to do its work. We love ActiveRecord! |
|---|
| 55 | * Camping::Controllers can be assigned URLs in the class definition. Neat? |
|---|
| 56 | * Camping::Views describes HTML using pure Ruby. Markup as Ruby, which we |
|---|
| 57 | call Markaby. |
|---|
| 58 | |
|---|
| 59 | If you want to write larger applications with Camping, you are encouraged to |
|---|
| 60 | split the application into distinct parts which can be mounted at URLs on your |
|---|
| 61 | web server. You might have a blog at /blog and a wiki at /wiki. Each |
|---|
| 62 | self-contained. But you can certainly share layouts and models by storing them |
|---|
| 63 | in plain Ruby scripts. |
|---|
| 64 | |
|---|
| 65 | Interested yet? Okay, okay, one step at a time. |
|---|
| 66 | |
|---|
| 67 | == Installation |
|---|
| 68 | |
|---|
| 69 | * <tt>gem install camping</tt> |
|---|