What is Camping?
Camping is a web application framework written in Ruby by "why the lucky stiff" (until version 1.5). Since _why's disappearance (August 19, 2009) from the web it is now maintained by the community.
Camping's complete source code can be viewed on a single page.
It consistently stays under 4kb. The latest version of Camping is available from Github.
Camping stores each new web application in a single file organized in a MVC style.
Camping only requires Rack (0.3 or higher) and (if you want to write HTML) Markaby (0.5 or higher), both available as Rubygems.
Of course, if you plan to use a database, you will need ActiveRecord. And any other resource will require other gems as well.
Camping more recent version includes support for Tilt which means that it now supports ERB and Haml.
Camping install is just a matter of installing a few gems:
gem install camping
gem install rack
gem install markaby
gem install activerecord
That is it and
Putting Camping into Work
As seen before, Camping uses the MVC architecture. But for our studies purpose, I would like to propose a totally different aproach. Turning everything upside down, we will use the CVM order:
- 3 - Controller
- 2 - View
- 1 - Model
Let's Start from the Beginning...
Our very first Camping application:
Every useful Camping application must have at least:
require 'camping' Camping.goes :Name
Name" is the application controller's name.
And should, of course, have a controller like this:
module Name::Controllers class Index < R '/' def get p "Any Ruby code goes here!" end end end
Which generally (not mandatory) uses a class named "
to get the job done.
Note the use of the weird raw string syntax "
for regexes which might contain backslashes. Without the R you'd have to
double every backslash.
This is an uncommon Ruby feature, used in Camping to describe the routes which the controller can be accessed on.
So, let's start with a simple Controller:
Save it as "
app01.rb" and get it running with the "camping" command:
Which will start the server and turn your application on:
Now you can see the results at "
Second step, let's add a View
To show any valuable result as a Web page, You will need Views of course.
Views" can be divided into two parts:
- The basic layout, which means the main page including "html" and "head" parts.
- The body itself, which is yielded by the layout part.
module Trainer::Views def layout html do head do title 'ToR goes Camping' end body do self << yield end end end def view_body p "Second step, first View..." end end
To get this View working You will need to render it from the Controller:
module Trainer::Controllers class Index < R'/' def get render :view_body end end end
Which will bring us this piece of code:
You may save it as "
app02.rb" and again get it running with "camping app02.rb".
Which will let You see this result in your Browser:
And the resulting HTML (thanks to Markaby) is:
Where You can clearly see the layout part and the body part inside that first one.
Baby step 1 - a few improvements
- More controllers and links
We will see now how to add more controllers and deal with Links in the Views.
In the new "Cool" controller, we have used the regexp "
not only to link the "
/cool" URL, but every alphanumeric character after the slash
with the "
We will take those characters (stuff) after the slash and render a corresponding view with:
def get stuff render stuff end
That being so, if we use one of the view names when specifying the URL, it will be rendered accordingly.
You can now make your own experiments...
Third step, adding a Model
Storing data is an essential need for any application.
In camping (as in Rails) the model uses, by default, Active Record to have the dirty work done.
Let's have a look at the basic structure of a model-definition in camping: