| 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 Camping Skeleton |
|---|
| 12 | |
|---|
| 13 | A skeletal Camping blog could look like this: |
|---|
| 14 | |
|---|
| 15 | require 'camping' |
|---|
| 16 | |
|---|
| 17 | Camping.goes :Blog |
|---|
| 18 | |
|---|
| 19 | module Blog::Models |
|---|
| 20 | class Post < Base; belongs_to :user; end |
|---|
| 21 | class Comment < Base; belongs_to :user; end |
|---|
| 22 | class User < Base; end |
|---|
| 23 | end |
|---|
| 24 | |
|---|
| 25 | module Blog::Controllers |
|---|
| 26 | class Index < R '/' |
|---|
| 27 | def get |
|---|
| 28 | @posts = Post.find :all |
|---|
| 29 | render :index |
|---|
| 30 | end |
|---|
| 31 | end |
|---|
| 32 | end |
|---|
| 33 | |
|---|
| 34 | module Blog::Views |
|---|
| 35 | def layout |
|---|
| 36 | html do |
|---|
| 37 | body do |
|---|
| 38 | self << yield |
|---|
| 39 | end |
|---|
| 40 | end |
|---|
| 41 | end |
|---|
| 42 | |
|---|
| 43 | def index |
|---|
| 44 | for post in @posts |
|---|
| 45 | h1 post.title |
|---|
| 46 | end |
|---|
| 47 | end |
|---|
| 48 | end |
|---|
| 49 | |
|---|
| 50 | Some things you might have noticed: |
|---|
| 51 | |
|---|
| 52 | * Camping::Models uses ActiveRecord to do its work. We love ActiveRecord! |
|---|
| 53 | * Camping::Controllers can be assigned URLs in the class definition. Neat? |
|---|
| 54 | * Camping::Views describes HTML using pure Ruby. Markup as Ruby, which we |
|---|
| 55 | call Markaby. |
|---|
| 56 | * You use Camping::goes to make a copy of the Camping framework under your |
|---|
| 57 | own module name (in this case: <tt>Blog</tt>.) |
|---|
| 58 | |
|---|
| 59 | <b>NOTE:</b> Camping auto-prefixes table names. If your class is named |
|---|
| 60 | <tt>Blog::Models::Post</tt>, your table will be called <b>blog_posts</b>. |
|---|
| 61 | Since many Camping apps can be attached to a database at once, this helps |
|---|
| 62 | prevent name clash. |
|---|
| 63 | |
|---|
| 64 | (If you want to see the full blog example, check out <tt>examples/blog/blog.rb</tt> |
|---|
| 65 | for the complete code.) |
|---|
| 66 | |
|---|
| 67 | If you want to write larger applications with Camping, you are encouraged to |
|---|
| 68 | split the application into distinct parts which can be mounted at URLs on your |
|---|
| 69 | web server. You might have a blog at /blog and a wiki at /wiki. Each |
|---|
| 70 | self-contained. But you can certainly share layouts and models by storing them |
|---|
| 71 | in plain Ruby scripts. |
|---|
| 72 | |
|---|
| 73 | Interested yet? Okay, okay, one step at a time. |
|---|
| 74 | |
|---|
| 75 | == Installation |
|---|
| 76 | |
|---|
| 77 | * <tt>gem install camping</tt> |
|---|
| 78 | |
|---|
| 79 | Or for the bleeding edge: |
|---|
| 80 | |
|---|
| 81 | * <tt>gem install camping --source code.whytheluckystiff.net</tt> |
|---|
| 82 | |
|---|
| 83 | You are encourage to install Camping and SQLite3, since it is a small database |
|---|
| 84 | which fits perfectly with our compact bylaws, works well with the examples. |
|---|
| 85 | |
|---|
| 86 | * See http://code.whytheluckystiff.net/camping/wiki/BeAlertWhenOnSqlite3 for instructions. |
|---|
| 87 | |
|---|
| 88 | == Running Camping Apps |
|---|
| 89 | |
|---|
| 90 | The blog example above and most Camping applications look a lot like CGI scripts. |
|---|
| 91 | If you run them from the commandline, you'll probably just see a pile of HTML. |
|---|
| 92 | |
|---|
| 93 | Camping comes with an tool for launching apps from the commandline: |
|---|
| 94 | |
|---|
| 95 | * Run: <tt>camping blog.rb</tt> |
|---|
| 96 | * Visit http://localhost:3301/ to use the app. |
|---|
| 97 | |
|---|
| 98 | == How the Camping Tool Works |
|---|
| 99 | |
|---|
| 100 | If your application isn't working with the <tt>camping</tt> tool, keep in mind |
|---|
| 101 | that the tool expects the following conventions to be used: |
|---|
| 102 | |
|---|
| 103 | 1. You must have SQLite3 and SQLite3-ruby installed. (Once again, please see |
|---|
| 104 | http://code.whytheluckystiff.net/camping/wiki/BeAlertWhenOnSqlite3 for instructions.) |
|---|
| 105 | 2. If your script is called <tt>test.rb</tt>, Camping expects your application to |
|---|
| 106 | be stored in a module called <tt>Test</tt>. Case is not imporant, though. The |
|---|
| 107 | module can be called <tt>TeSt</tt> or any other permutation. |
|---|
| 108 | 3. Your script's postamble (anything enclosed in <tt>if __FILE__ == $0</tt>) will be |
|---|
| 109 | ignored by the tool, since the tool will create an SQLite3 database at |
|---|
| 110 | <tt>~/.camping.db</tt>. Or, on Windows, <tt>$USER/Application Data/Camping.db</tt>. |
|---|
| 111 | 4. If your application's module has a <tt>create</tt> method, it will be executed before |
|---|
| 112 | the web server starts up. |
|---|
| 113 | |
|---|
| 114 | == The Rules of Thumb |
|---|
| 115 | |
|---|
| 116 | Once you've started writing your own Camping app, I'd highly recommend that you become familiar |
|---|
| 117 | with the Camping Rules of Thumb which are listed on the wiki: |
|---|
| 118 | http://code.whytheluckystiff.net/camping/wiki/CampingRulesOfThumb |
|---|
| 119 | |
|---|