hassox/warden — pull request #48 by Tigraine (2012-05-08 08:58:55+00:00)
Title: Added ability to have different serialize/deserialize methods for different scopes

Hi,
I ran across a requirement in my current application where I use Devise alongside a custom Warden implementation.

Devise is used to authenticate my admin users that are locally stored in the DB, while I also have to log in Visitors to the site that come from a different system using a Token based authentication.

Visitors and Admins are stored in different models and therefore I was struggling with Warden only having one deserialize method.

This pull request enables you to call ```Warden::Manager.serialize_into_session``` with a scope to supply a different method of serialization/deserialization for that special scope.

Example:

```ruby
Warden::Manager.serialize_from_session(:admin) do |id|
Visitor.find(id)
end

Warden::Manager.serialize_from_session do |id|
User.find(id)
end
```

If no special scope deserializer/serializer has been given it still calls the serialize/deserialize methods as before.

I am a bit unhappy with my current implementation since I have to use ```respond_to?``` and ```send``` to test if that scoped_serialize is present and then retrieve it. I don't know how that kind of meta-programming impacts performance Ruby as I am fairly new to the language.

I hope the tests are in order, they are a bit messy and there are surely better ways to verify some of these methods but I didn't come up with anything better yesterday.

Feedback on how to improve this would be appreciated.

greetings Daniel