Forum: Ruby on Rails Poor Man's Continuations

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ian W. (Guest)
on 2006-03-08 11:16
(Received via mailing list)
Hi List,

I'm a new rubyist and railist, and my web-dev background is php, where
I used and contributed to the WACT framework.

Continuations are dead cool aren't they.  As I understand it, the
problem with having them in rails is that marshalling them isn't
feasbile, so in a stateless environment, they can't be easily
implemented.  (Is this correct?)

My idea is to use the fact that (in many cases) given the same
starting state (params, and session vars) a controller will end up at
the same point.  So we could implement a continuation-like thingy by
saving the these relevant parts of the state, and using a state stack.
 The first time a continuation_call is found, the control is
redirected to the target, the target redirects back to the source when
it is done.  The second time the continuation_call is encountered
there's a result ready, and that is returned.

So I've written this, by sticking a catch in perform_action which
effectively halts processing and re-routes to somewhere else when
required.

Now I can do this in my controller (note that the main loop does not
need to know about what goes on within the continuation_calls, also
note the simple conditional logic for stating the flow of the
applictaion).

class MyController < ApplicationController
  has_continuations

  def main
    item = conitnuation_call :action => 'get_name'
    if item == 'frank'
      item += ' ' + continuation_call :action => 'get_frank_extra'
    end
  end

  def get_name
    if params[:name] and params[:name].size > 10
      if continuation_call :action => 'confirm_size'
        continuation_return params[:name]
      else
        continuation_return params[:name].slice(0..9)
      end
    end
  end

  def get_frank_extra
    continuation_return params[:extra] if params[:extra]
  end

  def confirm_size
    continuation_return params[:confirm] == 'yes'
  end
end

(this plays nicely with rjs, you can also continuation_call other
controllers, and there are no redirects if the call is to an action
within the same controller)

There are some severe limitations at the moment.  There can only be
one call from a particlar source action to a target action, i.e. it
woudln't play nicely with a while loop.  Also, if your controller's
flow of control is dependent on things other than the 'inputs' (params
and session) then it won't work reliably.  However, there should be a
way of specifying these extra inputs (if you put them in a session
var, it will work).  At the moment, after_filters won't be called for
actions that re-route half way through, but I haven't figured out what
the desired behviour should be in this case yet.

So is this of interest to anyone?  If so, should I just post the code
to the list?  Or somewhere else?  Any comments welcome

Cheers, and thanks for reading,
Ian
Carmen -. (Guest)
on 2006-03-18 02:38
Ian W. wrote:
> So is this of interest to anyone?  If so, should I just post the code
> to the list?  Or somewhere else?  Any comments welcome

hi Ian. interesting to me anyways.. did you check out "Poor Man's
Seaside" (di you write it?)? (where is this anyways, Jersey shore? maybe
that polluted strip of land by LAX?)

http://onestepback.org/articles/callcc/pmseaside/index.html
This topic is locked and can not be replied to.