Created
March 10, 2013 22:30
-
-
Save danveloper/5130793 to your computer and use it in GitHub Desktop.
Destructurable data type using getAt() to associate a result with an object in a service method call
This file contains 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 Destructurable<E> { | |
E obj | |
Boolean result | |
def getAt(idx) { | |
if (idx == 0) return obj | |
if (idx == 1) return result | |
throw new RuntimeException("Only two parameters are returnable") | |
} | |
} |
This file contains 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 User { | |
String email | |
String password | |
boolean save() { | |
if (!email || !password) { | |
return false | |
} | |
return true | |
} | |
} |
This file contains 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
def userService = new UserService() | |
def (user, success) = userService.register("dan.i.el.p.woods@gmail.com", null) | |
assert !success | |
(user, success) = userService.register("dan.i.el.p.woods@gmail.com", "1234") | |
assert success |
This file contains 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 UserService { | |
Destructurable<User> register(email, password) { | |
def user = new User(email: email, password: password) | |
def result = true | |
if (!user.save()) { | |
result = false | |
} | |
return [obj: user, result: result] as Destructurable<User> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment