Skip to content

Commit

Permalink
v0.5.6 -- withDelegate added to Closure
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Yates committed Nov 25, 2013
1 parent 278b3c4 commit f692cbc
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 2 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,20 @@ However, the standard deviation shows that they are very different sequences of
// prints: "AverageStats( mean:50.5, median:50.5, variance:2450.25, stdDev:49.5 )"
println a.average()
// prints: "AverageStats( mean:50.5, median:50.5, variance:833.25, stdDev:28.86607004772212 )"
println b.average()
println b.average()

## Closure.`withDelegate`

Allows setting the `delegate` of a `Closure` in-line:

static <T> Closure<T> withDelegate( Closure<T> self, Object delegate )
static <T> Closure<T> withDelegate( Closure<T> self, Object delegate, int strategy )

So you can do:

def testClosure = { num ->
num + val
}

def value = testClosure.withDelegate( [ val: 4 ] )( 3 )
assert value == 7
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ apply plugin: 'signing'
sourceCompatibility=1.6
targetCompatibility=1.6

version = '0.5.5'
version = '0.5.6'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2012-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.bloidonia.groovy.extensions

/**
* @author Tim Yates
*/
class ClosureExtensionMethods {

/**
* Set a Closure's delegate
* <pre class="groovyTestCase">
* def testClosure = { num ->
* num + val
* }
*
* def value = testClosure.withDelegate( [ val: 4 ] )( 3 )
* assert value == 7
* </pre>
*
* @param self the Closure
* @param delegate the object to delegate to
* @return the closure but with its delegate set
*/
static <T> Closure<T> withDelegate( Closure<T> self, Object delegate ) {
self.delegate = delegate
self
}

/**
* Set a Closure's delegate and resolveStrategy
* @param self the Closure
* @param delegate the object to delegate to
* @param strategy the resolveStrategy to use
* @return the closure but with its delegate and strategy set
*/
static <T> Closure<T> withDelegate( Closure<T> self, Object delegate, int strategy ) {
self.delegate = delegate
self.resolveStrategy = strategy
self
}
}
44 changes: 44 additions & 0 deletions src/test/groovy/tests/ClosureTests.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2012-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package tests

import spock.lang.Specification

public class ClosureTests extends Specification {

def val = 99

def testClosure = { num ->
num + val + val2
}

def 'quick test'() {
given:
def val = testClosure.withDelegate( [ val: 4, val2: 8 ] )( 3 )
expect:
// Should use val from the class, but val2 from the delegate
val == 110
}

def 'strategy test'() {
given:
def val = testClosure.withDelegate( [ val: 4, val2: 8 ], Closure.DELEGATE_FIRST )( 3 )
expect:
// val and val2 from the delegate
val == 15
}
}

0 comments on commit f692cbc

Please sign in to comment.