Last active
January 31, 2021 07:24
-
-
Save Joxebus/0611c40ae9df4133c17fe55d88b8c240 to your computer and use it in GitHub Desktop.
This is a sample of the Method Missing implementation for dynamic invoke closures
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 MySampleClass { | |
Map props = [:] | |
def propertyMissing(String propertyName, String value) { | |
props[propertyName] = value | |
} | |
def propertyMissing(String propertyName) { | |
props[propertyName] | |
} | |
def methodMissing(String name, args) { | |
println """ | |
method: ${name} | |
args: ${args} | |
""" | |
} | |
static def $static_methodMissing(String name, Object args) { | |
println "El metodo estático de nombre [$name] y argumentos [$args] no existe" | |
} | |
} | |
MySampleClass.myStaticMethod("Ejemplo", "invocación", "dinámica") | |
def object = new MySampleClass() | |
object.someProperty = "Esta propiedad no existe" | |
println object.someProperty | |
object.doSomethingWithArgs("Hola", "Desveloper", 33) |
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 ActionExecutor { | |
Map<String, Closure> actions = [:] | |
def methodMissing(String name, args) { | |
Closure closure = actions[name] | |
if(closure) { | |
closure.call(args) | |
} else { | |
throw new MissingMethodException(name, ActionExecutor.class, args) | |
} | |
} | |
} | |
List elements = ['This', 'is', 'a', 'sample'] | |
ActionExecutor ae = new ActionExecutor() | |
ae.actions << ['sortArgs': { args -> args.sort{ it.toLowerCase() }}] | |
ae.actions << ['printArgsLenght': { args -> println args.collect{ "$it:${it.size()}"} }] | |
List sorted = ae.sortArgs(*elements) | |
ae.printArgsLenght(*sorted) |
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 MiniGorm { | |
def db = [] | |
def methodMissing(String methodName, args) { | |
methodName = methodName.toLowerCase()-"findby" | |
switch(methodName) { | |
case {it =~ /notlike+/ }: | |
def property = methodName - "notlike" | |
db.findAll { elem-> !elem[property].contains(args[0])} | |
break | |
case {it =~ /like+/ }: | |
def property = methodName - "like" | |
db.findAll { elem-> elem[property].contains(args[0])} | |
break | |
default: | |
"Operacion no soportada" | |
} | |
} | |
} | |
List people = [ | |
[first: "Omar", last:"Bautista"], | |
[first: "Jorge", last:"Valenzuela"], | |
[first: "Mia", last:"Bautista"], | |
[first: "Maria", last:"Ojeda"] | |
] | |
def miniGorm = new MiniGorm(db:people) | |
println miniGorm.findByFirstLike("Maria") // [first: "Maria", last:"Ojeda"] | |
println miniGorm.findByLastLike("Bautista") // [first: "Omar", last:"Bautista"], [first: "Mia", last:"Bautista"] | |
println miniGorm.findByLastNotLike("Bautista") // [first: "Jorge", last:"Valenzuela"], [first: "Maria", last:"Ojeda"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment