-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterceptors.groovy
41 lines (36 loc) · 1.12 KB
/
Interceptors.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class UserService {
def find(params) {
"${findAll(params)} limit 1"
}
def findAll(params) {
"select * from user where ${getWhere(params)}"
}
private def getWhere(params) {
params.collect { k, v ->
"$k = '$v'"
}.join(' and ')
}
}
UserService.metaClass.invokeMethod = { String name, args ->
def paramBuilder = { propString ->
def i=0
propString.split("And").inject([:]) { m, prop ->
m[prop.toLowerCase()] = args[i++]
m
}
}
if (name =~ /findAllBy/) {
def propString = (name =~ /findAllBy(.*)/)[0][1]
def props = paramBuilder(propString)
return delegate.findAll(props)
} else if (name =~ /findBy/) {
def propString = (name =~ /findBy(.*)/)[0][1]
def props = paramBuilder(propString)
return delegate.find(props)
}
return delegate.metaClass.getMetaMethod(name, args).invoke(delegate, args)
}
new UserService().with {
println findAllByNameAndTwitter("Dan Woods", "@danveloper")
println findByNameAndTwitter("Dan Woods", "@danveloper")
}