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

Microservices Design Patterns

functional decomposition or domain-driven design well-defined interfaces  explicitly published interface  single responsibility principle potentially polyglot http://blog.arungupta.me/microservice-design-patterns/ http://blog.arungupta.me/microservices-monoliths-noops/ https://go.forrester.com/blogs/13-11-20-mobile_needs_a_four_tier_engagement_platform/ three-tier architecture — presentation, application, data vs. 4 tier -- client, delivery, aggregation, services

Dockercon 2021

 Dockercon 2021 Replay presentations ex:  https://docker.events.cube365.net/dockercon-live/2021/content/Videos/zo9AAafDLCPRv2rom https://github.com/docker/roadmap/issues/201 https://aws.amazon.com/ecs/anywhere/ https://cloud.netapp.com/blog/aws-cvo-blg-amazon-eks-anywhere-and-ecs-anywhere-multicloud-services https://docs.docker.com/cloud/ecs-integration/ https://github.com/GoogleContainerTools/skaffold https://docs.docker.com/compose/profiles/ https://docs.docker.com/compose/compose-file/compose-file-v2/ Lessons Learned With Dockerfiles and Docker Builds https://docs.docker.com/develop/develop-images/build_enhancements/  &  https://docs.docker.com/buildx/working-with-buildx/ "Java 11 is first version that is fully 'container aware'." As in it respects cgroups.  Docker Playground https://labs.play-with-docker.com/ Compiling to Containers with BuildKit https://github.com/agbell/compiling-containers https://earthly.dev/

Build software like you build houses

Interfaces facilitate visibility. Software is the only engineering practice that doesn't allow runtime visibility to end-user. If this were true for construction, it would mean your builder would have to live with you for you to even guess what the problem is. https://vimeo.com/221049715 https://github.com/stripe/veneur