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

Got to be a better way than ridiculous high apr RHApr

Read more at:  https://www.cardratings.com/creditcardblog/whats-the-point-of-having-a-credit-card.html Copyright © CardRatings.com 1. Earn credit card rewards and perks Every month, seems like a joke this is just a way for marketers to get ahold of you Don't need rewards for tracking my behavior. Need living wage and fair prices.  2. Avoid wiping out your bank account if an emergency happens Life is an emergency Should we charge RHApr when someone is in need? Vulture lately? 3. Minimize risk and reduce payment hassles  Debit cards or other e-payment do this without exorbitant fees like RHApr 4. Make budgeting easier  No No one writes checks anymore Debit cards and other forms of e-payment solve most of these 5. Build credit why? So you can have more credit. Circular. Stupid. Read more at:  https://www.cardratings.com/creditcardblog/whats-the-point-of-having-a-credit-card.html Copyright © CardRatings.com