Skip to content
Browse files

Issue 973 - Add self type to LifecycleCallbacks trait

  • Loading branch information...
1 parent bbea5a8 commit 15b4a8b17db06c705360cb0ccaa90172530e02d3 @eltimn eltimn committed Apr 20, 2011
View
18 persistence/record/src/main/scala/net/liftweb/record/Field.scala
@@ -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 {}
+}
View
20 persistence/record/src/main/scala/net/liftweb/record/MetaRecord.scala
@@ -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]))
}
@@ -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 {}
-}
-
View
13 persistence/record/src/test/scala/net/liftweb/record/Fixtures.scala
@@ -75,6 +75,8 @@ object MyTestEnum extends Enumeration {
}
trait HarnessedLifecycleCallbacks extends LifecycleCallbacks {
+ this: BaseField =>
+
var beforeValidationHarness: () => Unit = () => ()
override def beforeValidation = beforeValidationHarness()
var afterValidationHarness: () => Unit = () => ()
@@ -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] {
View
2 persistence/record/src/test/scala/net/liftweb/record/RecordSpec.scala
@@ -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)
}

0 comments on commit 15b4a8b

Please sign in to comment.
Something went wrong with that request. Please try again.