Skip to content

Commit

Permalink
Merge pull request #7 from halfbaked/master
Browse files Browse the repository at this point in the history
added monitoring for changes in services
  • Loading branch information
antony committed Feb 2, 2013
2 parents 2bc00fd + 38f2b6b commit 7730cc7
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions FeatureSwitchGrailsPlugin.groovy
@@ -1,9 +1,9 @@
class FeatureSwitchGrailsPlugin {

def version = "0.6"
def version = "0.6.2"
def grailsVersion = "2.0 > *"
def dependsOn = [:]
def observe = ["controllers"]
def observe = ["controllers", "services"]

def pluginExcludes = [
"grails-app/views/error.gsp",
Expand Down Expand Up @@ -31,30 +31,27 @@ class FeatureSwitchGrailsPlugin {

def scm = [ url: "https://github.com/aiten/grails-feature-switch" ]

Closure decorate = {
it.metaClass.withFeature = { String feature, Closure closure ->
applicationContext.featureSwitchService.withFeature(feature, closure)
}
it.metaClass.withoutFeature = { String feature, Closure closure ->
applicationContext.featureSwitchService.withoutFeature(feature, closure)
}
}

def doWithApplicationContext = { applicationContext ->
application.controllerClasses.each(decorate)
def doWithDynamicMethods = { ctx ->
application.controllerClasses.each { decorate(it.clazz, application) }
application.serviceClasses.findAll {
it.name != "FeatureSwitch"
}.each(decorate)

}.each { decorate(it.clazz, application) }
}

def onChange = { event ->
if (isControllerEvent(event)) {
decorate(event.source)
if (application.isControllerClass(event.source) || application.isServiceClass(event.source)) {
decorate(event.source, application)
}
}

private isControllerEvent(def event) {
event.application.controllerClasses.find { it.fullName == event.source.name }
private void decorate(Class c, def application) {
def ctx = application.mainContext
c.metaClass.withFeature = { String feature, Closure closure ->
ctx.featureSwitchService.withFeature(feature, closure)
}
c.metaClass.withoutFeature = { String feature, Closure closure ->
ctx.featureSwitchService.withoutFeature(feature, closure)
}
}

}

0 comments on commit 7730cc7

Please sign in to comment.