On 12 May 2011 00:04, Shree Mulay <shreemu...@gmail.com> wrote:
> From this code, I can't figure out where you "instantiate" the session
> var, store to it, and read back from it? I know there's not
> instantiation in clojure(???). I hope your able to get what I'm having
> trouble figuring out.

The session is read from a :session key on the request map, and
written using a :session key on the response map.

For example, this route returns the value of the session key :x

  (GET "/get" {session :session}
    (str "x = " (:x session)))

It's equivalent to the Sinatra route:

  get "/get" do
    "x = #{session[:x]}"
  end

The main difference is that the session has to be bound explicitly. We
don't just get a magic session map hanging around in the background.

Writing a session is a little trickier:

  (GET "/set" [x :as {session :session}]
    {:session (assoc session :x x)
     :body (str "You set x = " x)})

The equivalent Sinatra route is:

  get "/set" do |x|
    session[:x] = x
    "You set x = #{x}"
  end

Ring sessions are a little unwieldy because you write by changing the
response, rather than calling a side-effectful function. Sandbar takes
a slightly different approach that looks more like the Sinatra code:

  (GET "/get" []
    (str "x = " (session-get :x)))

  (GET "/set" [x]
    (session-put! :x x)
    (str "You set x = " x))

Sandbar is a little cleaner for simple things, but Ring sessions have
the advantage of working well with middleware. Idiomatic Ring tends to
use a lot of middleware, far more than one might use in Rack (for
instance).

- James

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to