Camping.goes :Camping
Posted by Eric Mill
Jan 15
Many in the Rails community have at least heard of Camping, a “microframework” by why the lucky stiff. It was only recently that I discovered that it is genuinely useful, even inspiring, for small applications.
I’m currently at work on a tiny application called Rubedo, which offers a tiny Icecast source client, with an also-tiny web frontend. I’m writing it in Ruby, and so I began to do the frontend in Rails, just to “get it working”, and this was fine. But this application is small—in its entire life I can’t see it having more than 2 models, and right now it only has 1. Since I’m a RESTful man, the same applies to controllers. Furthermore, this app needs to be light and portable, and looking at it from that perspective, Rails is an atom bomb. I feel strongly about the dropping of the atomic bomb on Hiroshima and Nagasaki, so when a coworker mentioned that this might be a good excuse to try out Camping, I suspected this could be a good fit.
So what is Camping, and why is it so great?
The driving idea behind Camping, as translated from why’s beautiful hummingbird language, is to have your entire MVC application in one file. Failing that, in as few files as possible. First, I’ll give a small code example, stolen right from why. Then, bullet points.
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 |
require 'camping' Camping.goes :Blog module Blog::Models class Post < Base; belongs_to :user; end class Comment < Base; belongs_to :user; end class User < Base; end end module Blog::Controllers class Index < R '/' def get @posts = Post.find :all render :index end end end module Blog::Views def layout html do body do self << yield end end end def index for post in @posts h1 post.title end end end |
- For models, Camping uses ActiveRecord, so your model code is nearly identical to what it would be in Rails.
- For views, Camping uses Markaby, so views are written entirely in Ruby and frighteningly compact. As you can see, there’s layouts, and don’t worry, there’s also partials.
- For controllers, Camping uses carefully calculated insanity to intermingle your controllers and routes. This is certainly the most novel part of Camping and the aspect that most differs from Rails, so best practices with it are currently up in the air for me. It’s a land of experimentation.
- SQLite is the database backend. SQLite satisfies both halves of its name extremely well, and doesn’t require any user authentication or background server process. You should think about using SQLite all the time, including right now.
- It runs by typing “camping blog.rb”. You might also use “-d blog.db” as a parameter to specify the DB file. Then, visit http://localhost:3301 in your browser.
“But wait. Routes??”
Your controller class inherits from an anonymous class constructed by calling “R” with a regular expression. So, to make a more nimble route, like to fetch and display a Post by id:
1 2 3 4 5 6 |
class Posts < R '/posts/(\d+)' def get(id) @post = Post.find id render :post end end |
“But wait. Javascript?? CSS??”
Assuming it’s as simple as your application is, just include it with the rest. Yes:
1 2 3 4 5 6 7 8 |
class Stylesheet < R '/blog.css' def get @headers['Content-Type'] = 'text/css' "body {fist: face}" end end # in your view link :href => R(Stylesheet), :rel => 'stylesheet', :type => 'text/css' |
“But wait. time_ago_in_words??”
I couldn’t live without that function either. But you can just include in stuff like that, you know. It’s your app, your dependencies. Like so:
1 2 3 4 |
require 'action_view/helpers/date_helper.rb' module Blog include ActionView::Helpers::DateHelper end |
I have absolutely no problem working like this. I don’t foresee my application, even with an upcoming User model and account system, growing beyond the point where I will get antsy about staying in Camping. Having my whole app in front of me while I work is a surprisingly gratifying experience. Working on it feels more like sculpting a statue, or rubbing a weak, newly born mouseling into life.
I was attracted to Rails by its simplicity and how much you could do in a small amount of code. In fact, it seriously eroded my respect in the PHP and Java languages, and frameworks based off of them. After making the decision to build my radio frontend in Camping, and working in it for a week, I feel almost the same degree of attraction. With it does not come any loss of respect for Rails, as Camping just isn’t appropriate for applications above a certain low bar of complexity. It would be only a step or two above trivial to port an application outgrowing its Camping gear into a full Rails application. I think that you would remember the experience fondly.
Yet you may surprise yourself by how complex your code can get, while still being reducible to something good-looking in a Camping jacket. I’ve been steadily adding more features to my application, and after each one follows a brief inner (and outer) struggle to make it work cheaply, tersely, and elegantly. The culture of Camping demands this, and I’m a better person for it.
Insightfully, why created other, more realistic examples of Camping that helped me a great deal in realizing more of Camping’s power. Look over them, maybe even download and run them, to see it at work. Remember though, that Camping, in the end, only attempts to fulfill one overarching and all consuming purpose:

Comments on this post
Jan 18
jarecare said,
What is the test support for Camping. I mean all apps need tests even simple ones.
Jan 18
Eivind Uggedal said,
Mosquito is a testing framework for camping:
http://code.whytheluckystiff.net/camping/wiki/MosquitoForBugFreeCamping
Jan 18
michael gorsuch said,
Great intro. I get the feeling that camping is being taken a lot more seriously than _why ever thought it would be.
Or perhaps he’s just an evil genius.
Jan 18
fREW Schmidt said,
_why could never be an evil genius. He is too pure of heart!
Sorry, comments are closed for this article.
© 2000 - 2009 by thoughtbot, inc.
written by a bushel of tiny robots
Come “ride the toad” on Hoptoad, the app error app.
Thunder Thimble: Brand monitoring for social media.
Widgetfinger: Simple content management for simple websites.
Tee-Bot, funny shirts your friends won't understand!
Umbrella Today: “It’s like totally the simplest weather report ever, Julie.”
Thoughtbot
thoughtbot is a technology consulting firm that provides web application development and design services. We focus on building modern systems, embracing good ideas and delivering elegant solutions.
Interested in learning Rails?
Sign up for our beginning or advanced training.
Archives