Skip to content
This repository
Browse code

Issue 973 - Add self type to LifecycleCallbacks trait

  • Loading branch information...
commit 15b4a8b17db06c705360cb0ccaa90172530e02d3 1 parent bbea5a8
Tim Nelson eltimn authored
18 persistence/record/src/main/scala/net/liftweb/record/Field.scala
@@ -448,3 +448,21 @@ object FieldHelpers {
448 448 def expectedA(what: String, notA: AnyRef): Failure = Failure("Expected a " + what + ", not a " + (if (notA == null) "null" else notA.getClass.getName))
449 449 }
450 450
  451 +
  452 +trait LifecycleCallbacks {
  453 + this: BaseField =>
  454 +
  455 + def beforeValidation {}
  456 + def afterValidation {}
  457 +
  458 + def beforeSave {}
  459 + def beforeCreate {}
  460 + def beforeUpdate {}
  461 +
  462 + def afterSave {}
  463 + def afterCreate {}
  464 + def afterUpdate {}
  465 +
  466 + def beforeDelete {}
  467 + def afterDelete {}
  468 +}
20 persistence/record/src/main/scala/net/liftweb/record/MetaRecord.scala
@@ -294,10 +294,6 @@ trait MetaRecord[BaseRecord <: Record[BaseRecord]] {
294 294 setFieldsFromJValue(inst, JsonParser.parse(json))
295 295
296 296 protected def foreachCallback(inst: BaseRecord, f: LifecycleCallbacks => Any) {
297   - inst match {
298   - case (lc: LifecycleCallbacks) => f(lc)
299   - case _ => {}
300   - }
301 297 lifecycleCallbacks.foreach(m => f(m._2.invoke(inst).asInstanceOf[LifecycleCallbacks]))
302 298 }
303 299
@@ -451,19 +447,3 @@ trait MetaRecord[BaseRecord <: Record[BaseRecord]] {
451 447 }
452 448 }
453 449
454   -trait LifecycleCallbacks {
455   - def beforeValidation {}
456   - def afterValidation {}
457   -
458   - def beforeSave {}
459   - def beforeCreate {}
460   - def beforeUpdate {}
461   -
462   - def afterSave {}
463   - def afterCreate {}
464   - def afterUpdate {}
465   -
466   - def beforeDelete {}
467   - def afterDelete {}
468   -}
469   -
13 persistence/record/src/test/scala/net/liftweb/record/Fixtures.scala
@@ -75,6 +75,8 @@ object MyTestEnum extends Enumeration {
75 75 }
76 76
77 77 trait HarnessedLifecycleCallbacks extends LifecycleCallbacks {
  78 + this: BaseField =>
  79 +
78 80 var beforeValidationHarness: () => Unit = () => ()
79 81 override def beforeValidation = beforeValidationHarness()
80 82 var afterValidationHarness: () => Unit = () => ()
@@ -100,21 +102,16 @@ trait HarnessedLifecycleCallbacks extends LifecycleCallbacks {
100 102 override def afterDelete = afterDeleteHarness()
101 103 }
102 104
103   -class LifecycleTestRecord private () extends Record[LifecycleTestRecord] with HarnessedLifecycleCallbacks {
  105 +class LifecycleTestRecord private () extends Record[LifecycleTestRecord] {
104 106 def meta = LifecycleTestRecord
105 107
106 108 def foreachCallback(f: LifecycleCallbacks => Any): Unit =
107 109 meta.foreachCallback(this, f)
108 110
109   - object innerObjectWithCallbacks extends LifecycleCallbacks with HarnessedLifecycleCallbacks
110   -
111   - object stringFieldWithCallbacks extends StringField(this, 100) with LifecycleCallbacks with HarnessedLifecycleCallbacks
  111 + object stringFieldWithCallbacks extends StringField(this, 100) with HarnessedLifecycleCallbacks
112 112 }
113 113
114   -object LifecycleTestRecord extends LifecycleTestRecord with MetaRecord[LifecycleTestRecord] {
115   - // without this, the Scala 2.7 compiler panics, so don't blame me if you remove it and it's confusing!
116   - override def foreachCallback(inst: LifecycleTestRecord, f: LifecycleCallbacks => Any) = super.foreachCallback(inst, f)
117   -}
  114 +object LifecycleTestRecord extends LifecycleTestRecord with MetaRecord[LifecycleTestRecord]
118 115
119 116
120 117 class ValidationTestRecord private() extends Record[ValidationTestRecord] {
2  persistence/record/src/test/scala/net/liftweb/record/RecordSpec.scala
@@ -151,8 +151,6 @@ object RecordSpec extends Specification("Record Specification") {
151 151 }
152 152 }
153 153
154   - testOneHarness("the record level", rec => rec)
155   - testOneHarness("the inner object level", rec => rec.innerObjectWithCallbacks: HarnessedLifecycleCallbacks)
156 154 testOneHarness("the field level", rec => rec.stringFieldWithCallbacks: HarnessedLifecycleCallbacks)
157 155 }
158 156

0 comments on commit 15b4a8b

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