harryh RSS

I don't really like tumblr much, but some of my friends use it, so I have to have one to leave "comments." Pretty much everything here should be seen as a comment and not an actual blog entry.

Archive

Jan
28th
Thu
permalink

Scala Puzzler of the day

Many different database objects in foursquare have a foreign key to User.  So I wrote something like so:

object User {
  trait FK[T <: FK[T]] extends LongKeyedMapper[T] {
    self: T=>
    object userid extends MappedLongForeignKey(this, User)
    def hasUser_? = userid.defined_?
  }

  class FKList[A <: FK[A]](list: List[A]) {    
    def primeUsers: List[A] = {
      if (!list.isEmpty) {
        val uids = list.filter(_.hasUser_?).map(_.userid.is)
        val users = findAll(ByList(User.id, uids))
        list.foreach(item => {
          item.userid.primeObj(users.find(_.id.is == item.userid.is))
        })
      }
      list
    }
  }
}

So that things can extend User.FK[Foo] and get automatic extra functionality for dealing with their User parents.  But you’ll notice that the field name is hard coded in the above.  Some things have two user parents.  For example Venues have creators and mayors.  Is there some way to enhance the code above so that a single model can extend User.FK twice with different field names?  Pondering what might be a good way to do this.