CLICK HERE TO READ THE MANUAL ALPHA VERSION. Documentation may be out of date.


         \/ |    |/
      \/ / \||/  /_/___/_
       \/   |/ \/
  _\__\_\   |  /_____/_
         \  | /          /
__ _-----`  |{,-----------~
          \ }{
           }{{     ACTION TREE:
           }}{
           {{}     a dry request router/controller
     , -=-~{ .-^- _
ejm        `}
            {
$ gem install action_tree

Quickstart

The Local Office of Alligators, division of information, subdivision of web2.0 and social media, have drawn a (naive) map for their new alligator web API:

welcome
|-- alligators
|   |-- list
|   |-- create
|   `-- (with an id)
|      |-- edit
|      `-- hug

A map like this not only shows paths, but also relationships between actions. For instance, edit and hug retrieve a specific alligator (with an id) before doing anything else. Similarly, every request beneath /alligators/ should be counted, to see if we're on Digg yet.

So let's put those two in before hooks in an ActionTree:

routes = ActionTree.new do
  action { "Welcome to the alligator manager #{@name}!" }

  with 'alligators' do
    before { @count = (@count || 0) + 1 }

    action            { Alligator.all }
    action('create')  { Alligator.create }

    with :id do
      before { @alligator = Alligator.get(@id) }
      action { 'this is ' + @alligator }
      action('edit')  { "editing #{@alligator}" }
      action('hug')   { "#{@alligator} is pleased." }
    end
  end
end

We can then match and run actions:

routes.match('/')        #=> #<ActionTree::Match >
routes.match('/').found? #=> true

@name = 'Zap'
routes.match('/').run(self) # => "Welcome to the alligator manager, Zap."

routes.match('/alligators/4').run # => "this is #<Alligator 4>"
router.match('/alligators/8/hug').run # => "<#Alligator 8> is pleased."

routes.match('nonexistant/route') # => #<ActionTree::NotFound>
routes.match('nonexistant/route').found? #=> false

 

Available dialects

at-rack turns ActionTree into a Sinatra-like framework, and allows you to mount trees as rack apps.

at-websocket is planned to make writing websocket servers easier.

 


Copyright (c) 2010-2011 Jostein Berre Eliassen. See LICENSE for details.