Skip to content

stonegao/camping

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

== Camping, a Microframework

Camping is a web framework which consistently stays at less than 4kb of code.
You can probably view the complete source code on a single page.  But, you know,
it's so small that, if you think about it, what can it really do?

The idea here is to store a complete fledgling web application in a single file
like many small CGIs.  But to organize it as a Model-View-Controller application
like Rails does.  You can then easily move it to Rails once you've got it going.

== A Camping Skeleton

A skeletal Camping blog could look like this:

  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

Some things you might have noticed:

* Camping::Models uses ActiveRecord to do its work.  We love ActiveRecord!
* Camping::Controllers can be assigned URLs in the class definition.  Neat?
* Camping::Views describes HTML using pure Ruby.  Markup as Ruby, which we
  call Markaby.
* You use Camping::goes to make a copy of the Camping framework under your
  own module name (in this case: <tt>Blog</tt>.)

<b>NOTE:</b> Camping auto-prefixes table names.  If your class is named
<tt>Blog::Models::Post</tt>, your table will be called <b>blog_posts</b>.
Since many Camping apps can be attached to a database at once, this helps
prevent name clash.

(If you want to see the full blog example, check out <tt>examples/blog/blog.rb</tt>
for the complete code.)

If you want to write larger applications with Camping, you are encouraged to
split the application into distinct parts which can be mounted at URLs on your
web server.  You might have a blog at /blog and a wiki at /wiki.  Each
self-contained.  But you can certainly share layouts and models by storing them
in plain Ruby scripts.

Interested yet?  Okay, okay, one step at a time.

== Installation

* <tt>gem install camping</tt>

Or for the bleeding edge:

* <tt>gem install camping --source http://code.whytheluckystiff.net</tt>

You are encourage to install Camping and SQLite3, since it is a small database
which fits perfectly with our compact bylaws, works well with the examples.

* See http://code.whytheluckystiff.net/camping/wiki/BeAlertWhenOnSqlite3 for instructions.

== Running Camping Apps

The blog example above and most Camping applications look a lot like CGI scripts.
If you run them from the commandline, you'll probably just see a pile of HTML.

Camping comes with an tool for launching apps from the commandline:

* Run: <tt>camping blog.rb</tt>
* Visit http://localhost:3301/ to use the app.

== How the Camping Tool Works

If your application isn't working with the <tt>camping</tt> tool, keep in mind
that the tool expects the following conventions to be used:

1. You must have SQLite3 and SQLite3-ruby installed.  (Once again, please see
   http://code.whytheluckystiff.net/camping/wiki/BeAlertWhenOnSqlite3 for instructions.)
2. If your script is called <tt>test.rb</tt>, Camping expects your application to
   be stored in a module called <tt>Test</tt>.  Case is not imporant, though.  The
   module can be called <tt>TeSt</tt> or any other permutation.
3. Your script's postamble (anything enclosed in <tt>if __FILE__ == $0</tt>) will be
   ignored by the tool, since the tool will create an SQLite3 database at 
   <tt>~/.camping.db</tt>.  Or, on Windows, <tt>$USER/Application Data/Camping.db</tt>.
4. If your application's module has a <tt>create</tt> method, it will be executed before
   the web server starts up.

== The Rules of Thumb

Once you've started writing your own Camping app, I'd highly recommend that you become familiar
with the Camping Rules of Thumb which are listed on the wiki: 
http://code.whytheluckystiff.net/camping/wiki/CampingRulesOfThumb

About

the 4k pocket full-of-gags web microframework

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published