Skip to main content
Here's a script from the Groovy book that explains expandos and makes some sense of why you might want to mess with spinning up methods at runtime. - http://groovy.codehaus.org/ExpandoMetaClass+-+Dynamic+Method+Names

The first example might help you pull off some interactive UI.
The second lends to a more fluid api. 
Run that in your console and smoke it.

import org.springframework.web.util.HtmlUtils

class HTMLCodec {
    static encode = { theTarget ->
        HtmlUtils.htmlEscape(theTarget.toString())
    }

    static decode = { theTarget ->
    HtmlUtils.htmlUnescape(theTarget.toString())
    }
}
//So what we do with these classes is to evaluate the convention and add 
//"encodeAsXXX" methods to every object based on the first part of the 
//name of the codec class such as "encodeAsHTML". The pseudo code to achieve this is below:
def classes = [HTMLCodec]



def codecs = classes.findAll { it.name.endsWith('Codec') }

codecs.each { codec ->
    Object.metaClass."encodeAs${codec.name-'Codec'}" = { codec.newInstance().encode(delegate) }
    Object.metaClass."decodeFrom${codec.name-'Codec'}" = { codec.newInstance().decode(delegate) }
}


def html = '<html><body>hello</body></html>'

println '&lt;html&gt;&lt;body&gt;hello&lt;/body&gt;&lt;/html&gt;' == HtmlUtils.htmlEscape(html)
//vs.
println '&lt;html&gt;&lt;body&gt;hello&lt;/body&gt;&lt;/html&gt;' == html.encodeAsHTML()


println html.encodeAsHTML()

Comments

Popular posts from this blog

Many organizations get vertical and horizontal confused. Their 'verticals' cross-cut the vertical stack and are really 'horizontals'.        scrum-teams-vs-traditionally-organized-teams
Answer to interview question - "What is the most important thing Object Oriented languages have contributed to software development?" ...sufficiently  model  the real world and problem at hand before jumping the gun. ...the success of graphical user interfaces like X and Windows. A GUI consists of several objects which have a behaviour on their own, something that OO is able to represent closely. ...polymorphism to match the logical way classification worked in real life -> code reuse ..limit project churn s tackexchange Tony Hoare introduced Null references in ALGOL W back in 1965 “simply because it was so easy to implement”, says Mr. Hoare. He talks about that decision considering it “my billion-dollar mistake”.  http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare

devops day 1

Kubernetes on a mac tutorial Assumes you have node and docker. Get them with homebrew works. https://kubernetes.io/docs/tutorials/stateless-application/hello-minikube/