Skip to content

Instantly share code, notes, and snippets.

@paulitex
Created March 18, 2011 19:24
Show Gist options
  • Save paulitex/876678 to your computer and use it in GitHub Desktop.
Save paulitex/876678 to your computer and use it in GitHub Desktop.
import org.scalatra.util.MutableMapWithIndifferentAccess
import scala.collection.mutable.{ Map => MMap }
/**
* Provides a Rails/Scalatra like map that sits in the session
* and destroys values after the request they were accessed in has returned
*
* @author paulitex
*
*/
class Flash extends MutableMapWithIndifferentAccess[Any] {
private val map = MMap[String, Any]()
private var dirtyKeys: Set[String] = Set()
def -=(key: String) = {
map -= key
this
}
def +=(kv: (String, Any)) = {
map += kv
this
}
def iterator = map.iterator
def get(key: String) = {
dirtyKeys += key
map.get(key)
}
def sweep() {
dirtyKeys.foreach(map -= _)
dirtyKeys = Set.empty
this
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment