Skip to content

Commit

Permalink
Issue 973 - Add self type to LifecycleCallbacks trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Nelson committed Apr 21, 2011
1 parent bbea5a8 commit 15b4a8b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 30 deletions.
18 changes: 18 additions & 0 deletions persistence/record/src/main/scala/net/liftweb/record/Field.scala
Expand Up @@ -448,3 +448,21 @@ object FieldHelpers {
def expectedA(what: String, notA: AnyRef): Failure = Failure("Expected a " + what + ", not a " + (if (notA == null) "null" else notA.getClass.getName))
}


trait LifecycleCallbacks {
this: BaseField =>

def beforeValidation {}
def afterValidation {}

def beforeSave {}
def beforeCreate {}
def beforeUpdate {}

def afterSave {}
def afterCreate {}
def afterUpdate {}

def beforeDelete {}
def afterDelete {}
}
Expand Up @@ -294,10 +294,6 @@ trait MetaRecord[BaseRecord <: Record[BaseRecord]] {
setFieldsFromJValue(inst, JsonParser.parse(json))

protected def foreachCallback(inst: BaseRecord, f: LifecycleCallbacks => Any) {
inst match {
case (lc: LifecycleCallbacks) => f(lc)
case _ => {}
}
lifecycleCallbacks.foreach(m => f(m._2.invoke(inst).asInstanceOf[LifecycleCallbacks]))
}

Expand Down Expand Up @@ -451,19 +447,3 @@ trait MetaRecord[BaseRecord <: Record[BaseRecord]] {
}
}

trait LifecycleCallbacks {
def beforeValidation {}
def afterValidation {}

def beforeSave {}
def beforeCreate {}
def beforeUpdate {}

def afterSave {}
def afterCreate {}
def afterUpdate {}

def beforeDelete {}
def afterDelete {}
}

Expand Up @@ -75,6 +75,8 @@ object MyTestEnum extends Enumeration {
}

trait HarnessedLifecycleCallbacks extends LifecycleCallbacks {
this: BaseField =>

var beforeValidationHarness: () => Unit = () => ()
override def beforeValidation = beforeValidationHarness()
var afterValidationHarness: () => Unit = () => ()
Expand All @@ -100,21 +102,16 @@ trait HarnessedLifecycleCallbacks extends LifecycleCallbacks {
override def afterDelete = afterDeleteHarness()
}

class LifecycleTestRecord private () extends Record[LifecycleTestRecord] with HarnessedLifecycleCallbacks {
class LifecycleTestRecord private () extends Record[LifecycleTestRecord] {
def meta = LifecycleTestRecord

def foreachCallback(f: LifecycleCallbacks => Any): Unit =
meta.foreachCallback(this, f)

object innerObjectWithCallbacks extends LifecycleCallbacks with HarnessedLifecycleCallbacks

object stringFieldWithCallbacks extends StringField(this, 100) with LifecycleCallbacks with HarnessedLifecycleCallbacks
object stringFieldWithCallbacks extends StringField(this, 100) with HarnessedLifecycleCallbacks
}

object LifecycleTestRecord extends LifecycleTestRecord with MetaRecord[LifecycleTestRecord] {
// without this, the Scala 2.7 compiler panics, so don't blame me if you remove it and it's confusing!
override def foreachCallback(inst: LifecycleTestRecord, f: LifecycleCallbacks => Any) = super.foreachCallback(inst, f)
}
object LifecycleTestRecord extends LifecycleTestRecord with MetaRecord[LifecycleTestRecord]


class ValidationTestRecord private() extends Record[ValidationTestRecord] {
Expand Down
Expand Up @@ -151,8 +151,6 @@ object RecordSpec extends Specification("Record Specification") {
}
}

testOneHarness("the record level", rec => rec)
testOneHarness("the inner object level", rec => rec.innerObjectWithCallbacks: HarnessedLifecycleCallbacks)
testOneHarness("the field level", rec => rec.stringFieldWithCallbacks: HarnessedLifecycleCallbacks)
}

Expand Down

0 comments on commit 15b4a8b

Please sign in to comment.