Porting Ruby snippet to Lisp



I'm early in the process of learning Lisp, but I wanted to get a feel for what a port of a snippet of Ruby code (part of a Rails controller) would look like down the road. It was chosen mostly at random, but it actually makes for a pretty good example IMO because of the language features represented.

I gave porting it a shot (see bottom), but my current knowledge is lacking, so it's quite a botched up job. One particular sticking point was the best way to handle the implicit creation of logged-in-user in line 19 of the Lisp version (line 10 of the Ruby version) (wrap the when in a let?). Is there a way to handle dealing with hash tables more concisely?

If an experienced Lisper could give me some pointers, that would be great. I know there has to be a much better way.

1 class AuthenticateController < ApplicationController
2 def register
3 if request.get?
4 logger.info session[:original_url]
5 else
6 @user = User.new(params[:user])
7 if @user.save
8 session[:user_id] = @user.id
9 redirect_to_original
10 elsif (logged_in_user = User.login(
11 params[:user][:user_name], params[:user][:password]))
12 session[:user_id] = logged_in_user.id
13 redirect_to_original
14 end
15 end
16 end

17 def redirect_to_original
18 original_url = session[:original_url] ||
19 {:controller => 'account', :action => 'index'}
20 session[:original_url] = nil
21 redirect_to(original_url)
22 end
23 end

request is an accessor of the superclass returning a request object
logger is an accessor of the superclass returning a logger object
session is an accessor of the superclass returning a hash
session[:key] returns the value associated with the :key from the hash
params is an accessor of the superclass returning a hash
@user is an instance variable of AuthenticateController

1. Begin definition of class AuthenticateController with a superclass of ApplicationControler

2. Begin definition of method register

3. Invoke the get? predicate on request

4. Invoke the info method on logger passing the value associated with the :original_url key from the session hash

6. Construct an instance of User and assign to instance variable @user

7. Invoke save method on @user return true if successful

8. Store id attribute of @user in hash

9. method call

10/11. params[:user] returns a hash; invoke class method, login, with results from hash and assign boolean result to logged_in_user local which is implicitly created at this point

19. { ... } creates a hash object with :key => value

21. Invoke superclass method redirect_to

Here's my attempt:

1 (defclass application-controller ()
2 ((logger :reader logger)
3 (params :reader params)
4 (request :reader request)
5 (session :reader session)))

6 (defclass authenticate-controller (application-controller)
7 ((user :accessor user)))

8 (defmethod register ((x authenticate-controller))
9 (if (getp (request x))
10 (info (logger x) (gethash :original-url (session x)))
11 (progn
12 (setf
13 (user x)
14 (make-instance 'user :user-name (gethash :user (params x))))
15 (if (save (user x))
16 (progn
17 (setf (gethash :user-id (session x)) (id (user x)))
18 (redirect-to-original x))
19 (when (setf logged-in-user ???)
20 (setf (gethash :user-id (session x)) (id logged-in-user))
21 (redirect-to-original))))))

22 (defmethod redirect-to-original ((x authenticate-controller))
23 (let ((original-url
24 (or
25 (gethash :original-url (session x))
26 (let ((temp (make-hash-table)))
27 (setf (gethash :controller "account"))
28 (setf (gethash :action "index"))))))
29 (setf (gethash :original-url (session x)))
30 (redirect-to x original-url)))
.



Relevant Pages

  • Objects losing their blessing?
    ... I'm trying to store an object reference in a hash, and when I try to use ... # Send a sync for each object that was changed ... if I change the code in Foo::cmethodto invoke ...
    (comp.lang.perl.moderated)
  • Re: Is there anything in C++ akin to Javas class Object?
    ... hashcode function that supplies a hash value for any object, ... the linker to include them in the executable. ... of Superclass in C++. ...
    (comp.lang.cpp)
  • Re: perl code
    ... > Then retrieve list should prompt the user to select one of the files it returned and then this would invoke another perl command called parselist.pl. ... You could use a hash or even process each file one at a time. ... HTH, ...
    (perl.beginners)
  • Different super() behavior for store() and =[]() in Hash subclass
    ... I created a simple class that subclasses the Hash item assignment. ... super function does differernt things depending on whether I use the ... class BadHashSubclass < Hash ... For some reason super is finding the superclass of the value 5 rather ...
    (comp.lang.ruby)
  • super behavior different in store and []= not in Hash subclass
    ... I created a simple class that subclasses the Hash item assignment. ... super function does differernt things depending on whether I use the ... class BadHashSubclass < Hash ... For some reason super is finding the superclass of the value 5 rather ...
    (comp.lang.ruby)