Skip to content

Instantly share code, notes, and snippets.

@danveloper
danveloper / Service.groovy
Last active December 17, 2015 21:09
Obscurity - "Redefining the Service Layer with Groovy Categories"
class LocalPaymentService implements PaymentService {
// Dependency injected from container
CreditCardPaymentGateway creditCardPaymentGateway
GiftCardPaymentGateway giftCardPaymentGateway
CheckPaymentGateway checkPaymentGateway
EmployeePaymentGateway employeePaymentGateway
public String process(PaymentType paymentType) throws IllegalArgumentException {
if (paymentType instanceof CreditCardPaymentType) {
@danveloper
danveloper / Processor.groovy
Last active December 17, 2015 21:09
Redefining - "Redefining the Service Layer with Groovy Categories"
class CreditCardPaymentProcessor {
static {
CreditCardPaymentProcessor.metaClass.static.methodMissing = { String name, args ->
if (name.startsWith("getGatewayFor")) {
def state = name.replaceAll("getGatewayFor","")?.toLowerCase()
switch (state) {
case "ca":
return new CheckPaymentProcessor()
default:
return new CreditCardPaymentProcessor()
@danveloper
danveloper / Service.groovy
Created May 29, 2013 18:24
Redefining Further - "Redefining the Service Layer with Groovy Categories"
class LocalPaymentService implements PaymentService {
public String process(PaymentType paymentType) {
if (paymentType instanceof CreditCardPaymentType) {
use (CreditCardPaymentProcessor) {
return paymentType.process()
}
}
// ...
}
@danveloper
danveloper / CheckPaymentProcessor.groovy
Last active December 17, 2015 21:09
Bettering - "Redefining the Service Layer with Groovy Categories"
class CheckPaymentProcessor {
static String process(PaymentType paymentType) {
def gateway = new CheckPaymentProcessor()
// … implementation code ...
}
}
@danveloper
danveloper / Registrar.groovy
Created May 29, 2013 18:40
Registrar - "Redefining the Service Layer with Groovy Categories"
class PaymentTypeHandlerRegistrationCategory {
static final registry = [:]
// type => handler registration
static def register(Class type, Class handler) {
registry[type] = handler
}
static def getHandler(Class type) {
registry[type]
@danveloper
danveloper / Service.groovy
Created May 29, 2013 18:44
Registration - "Redefining the Service Layer with Groovy Categories"
class LocalPaymentService implements PaymentService {
static {
use (PaymentTypeHandlerRegistrationCategory) {
CreditCardPaymentType.register(CreditCardPaymentProcessor)
GiftCardPaymentType.register(GiftCardPaymentProcessor)
EmployeePaymentType.register(EmployeePaymentProcessor)
CheckPaymentType.register(CheckPaymentProcessor)
}
}
@danveloper
danveloper / Service.groovy
Last active December 17, 2015 21:09
Redefined - "Redefining the Service Layer with Groovy Categories"
class LocalPaymentService implements PaymentService {
static {
use (PaymentTypeHandlerRegistrationCategory) {
CreditCardPaymentType.register(CreditCardPaymentProcessor)
GiftCardPaymentType.register(GiftCardPaymentProcessor)
EmployeePaymentType.register(EmployeePaymentProcessor)
CheckPaymentType.register(CheckPaymentProcessor)
}
}
@danveloper
danveloper / CustomMetaClassCreationHandle.java
Created July 26, 2013 06:26
Custom MetaClass Creation Handler for Groovy
package com.objectpartners.groovy.ext;
import groovy.lang.MetaClass;
import groovy.lang.MetaClassRegistry;
import org.codehaus.groovy.runtime.GeneratedClosure;
import org.codehaus.groovy.runtime.metaclass.ClosureMetaClass;
class CustomMetaClassCreationHandle extends MetaClassRegistry.MetaClassCreationHandle {
protected MetaClass createNormalMetaClass(Class theClass,MetaClassRegistry registry) {
@danveloper
danveloper / MixinResolvingMetaClass.java
Last active December 20, 2015 06:38
This is the custom meta class that we'll be giving to all classes so that we can resolve the mixins through a location and class nomenclature convention.
package com.objectpartners.groovy.ext;
// imports...
class MixinResolvingMetaClass extends MetaClassImpl {
static final Map<Class, Class> cachedMixins = new ConcurrentHashMap<>();
MixinResolvingMetaClass(MetaClassRegistry registry, Class theClass) {
super(registry, theClass);
}
@danveloper
danveloper / StringMixins.groovy
Created July 26, 2013 07:02
This is a mixin class that is resolvable with the package location and class naming convention resolution strategy.
package groovy.runtime.metaclass.java.lang
import groovy.json.JsonSlurper
class StringMixins {
// Must be static & must take an instance of the receiver as the first vararg
static def toJson(String input) {
new JsonSlurper().parseText(input)
}