scala - How do I make a trait that I can mix into mapped fields to that I don not have to override the same properties for many fields in a database? -
in scala lift framework, how make trait can mix mapped fields don not have override same properties many fields in database?
i eliminate redundancy in code below. specifically, not want have repeatedly add:
override def writepermission_? = false override def readpermission_? = false override def shoulddisplay_? = false override def show_? = false override def dbdisplay_? = false
for several db fields. thinking way go write trait looks this:
trait privatefield extends ...??? { override def writepermission_? = false override def readpermission_? = false override def shoulddisplay_? = false override def show_? = false override def dbdisplay_? = false }
and mix object overrides follows:
object owner extends mappedlongforeignkey(this, user) privatefield object status extends mappedint(this) privatefield
etc. way approach problem? if so, suggestions on how write trait? below code want reduce redundancy.
class mytable extends longkeyedmapper[mytable] idpk { def getsingleton = myclass object owner extends mappedlongforeignkey(this, user) { override def writepermission_? = false override def readpermission_? = false override def shoulddisplay_? = false override def show_? = false override def dbdisplay_? = false } object description extends mappedstring(this, 140) object name extends mappedstring(this, 140) object status extends mappedint(this) { override def writepermission_? = false override def readpermission_? = false override def shoulddisplay_? = false override def show_? = false override def dbdisplay_? = false } object entry_number extends mappedint(this) { override def writepermission_? = false override def readpermission_? = false override def shoulddisplay_? = false override def show_? = false } object quanitity extends mappeddecimal(this, mathcontext.decimal64, 2) { override def writepermission_? = false override def readpermission_? = false override def shoulddisplay_? = false override def show_? = false override def dbdisplay_? = false } } object mytable extends mytable longkeyedmetamapper[mytable] crudify[long, mytable] { override def editmenuloc:box[menu] = empty override def deletemenuloc:box[menu] = empty override def viewmenuloc:box[menu] = empty override def createmenulocparams: list[loc.anylocparam] = list(if(user.loggedin_? _, "not logged in")) override def showallmenulocparams: list[loc.anylocparam] = list(if(user.loggedin_? _, "not logged in")) override def findforlist(start: long, cnt: int): list[mytable] = findall(startat(start), maxrows(cnt), by(owner, user.currentuser)) override def findforparam(in: string): box[mytable] = { user <- user.currentuser id <- helpers.aslong(in) mytable <- find(by(this.id, id), by(owner, user)) } yield mytable }
thanks,
the simplest way use self type annotation:
trait privatefield[fieldtype <: any, ownertype <: mapper[ownertype]] { self: mappedfield[fieldtype, ownertype] => override def writepermission_? = false override def readpermission_? = false override def shoulddisplay_? = false override def show_? = false }
then can use in manner:
mappedint(this) privatefield[int, mytable]
Comments
Post a Comment