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

“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

jarecare

Jan 18

jarecare said,

What is the test support for Camping. I mean all apps need tests even simple ones.

Eivind Uggedal

Jan 18

Eivind Uggedal said,

Mosquito is a testing framework for camping:

http://code.whytheluckystiff.net/camping/wiki/MosquitoForBugFreeCamping

michael gorsuch

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.

fREW Schmidt

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