This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Student { | |
| enum DormHall { | |
| Honors, Preferred, Normal | |
| } | |
| def firstName | |
| def lastName | |
| def gpa | |
| // We only want dormHall to be assigned when it is deliberate, so throw an exception when | |
| // it is accidentally assigned outside of our rules engine |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class RulesEngineSupport<E> { | |
| RulesEngine engine | |
| E obj | |
| // Deduce the rule list for a given RulesEngine instance | |
| def getRules() { | |
| def rules = [] | |
| engine.class.declaredFields.each { | |
| def field = engine."${it.name}" | |
| if (!it.isSynthetic() && field instanceof Closure && it.name.endsWith("Rule")) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class RulesEngineFactory<E extends RulesEngine> { | |
| private final E engine | |
| public RulesEngineFactory(final Class<E> c) { | |
| this.engine = (E)c.newInstance() | |
| this.engine.engine = this.engine | |
| } | |
| def getObject(final def obj) { | |
| this.engine.obj = obj | |
| this.engine |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface RulesEngine {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class EnrollmentProcessor { | |
| static def enrollmentRulesFactory = new RulesEngineFactory<EnrollmentRulesEngine>(EnrollmentRulesEngine) | |
| def studentService | |
| def annualDormHallAssignment() { | |
| def students = Student.list(/* do some filtering maybe to make sure they are enrolling for this year,etc. */) | |
| def errors = [] | |
| students.each { student -> | |
| try { | |
| def studentEnrollmentRulesEngine = enrollmentRulesFactory.getObject(student) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * | |
| * Structure: | |
| * + module: model | |
| * - class: com.danveloper.model.Student | |
| * | |
| **/ | |
| class Student { | |
| enum DormHall { | |
| Honors, Preferred, Normal |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public enum PaymentVendor { | |
| INTERNAL, STRIPE; | |
| } | |
| public class ProductOrder { | |
| Account account; | |
| Address shippingAddress; | |
| PaymentMethod paymentMethod; | |
| double taxRate = 0.0; | |
| PaymentVendor paymentVendor = PaymentVendor.STRIPE; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Account { | |
| String email; | |
| int orderCount; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Address { | |
| Object street, unit, city, state, zip; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface PaymentMethod { | |
| enum PaymentMethodType { | |
| GIFT_CARD, CREDIT_CARD | |
| } | |
| PaymentMethodType getType(); | |
| } | |
| public class GiftCard implements PaymentMethod { | |
| String number; |