Showing posts with label groovy. Show all posts
Showing posts with label groovy. Show all posts

Friday, July 20, 2012

groovy : <=>

This is the operator usually used in comparator. It will returns –1, 0, 1

println (1<=>2)  // –1
println (1<=>1) // 0
println (2<=>1) // 1

Groovy: what difference between array.add and array +

def inX = [0,0]
def inXExpanded = []
(1..2).each {
    inXExpanded += inX
}
println inXExpanded  // the result is [0,0,0,0]

replace the code above to
(1..2).each {
    inXExpanded.add(inX)
}
println inXExpanded  // the result is [[0,0],[0,0]]

Thursday, October 20, 2011

Thursday, May 19, 2011

grails debug

Run this command

grails-debug run-app

The application is now up and running in debug mode, which means you may setup your eclipse remote debugging on port 5005.

Wednesday, May 18, 2011

Grails i18N on google App Engine

Have this in BootStrap.groovy

import grails.util.Environment;

class BootStrap {
    def messageSource
    def init = { servletContext ->
        if( Environment.getCurrent() == Environment.PRODUCTION ){
            messageSource.basenames = [ 'WEB-INF/grails-app/i18n/messages' ]
            messageSource.clearCache()   
        }
    }
    def destroy = {
    }
}


Run these two commands to package and deploy



  1. grails -Dgrails.env=production app-engine package
  2. appcfg.sh update ./target/war

  

Thursday, May 5, 2011

Ajax in grails

<g:javascript library="prototype" />



<g:remoteLink action="showDetail" id="${messageInstance?.id}"
update="details" >

${messageInstance.author.fullName} - ${messageInstance.subject}

</g:remoteLink>


The final attribute that we

set on the <g:remoteLink> tag is update. This attribute contains the ID of the HTML element on this page that will be updated with the result of the action—in this case, details.



In the method in action



def showDetail = {
def messageInstance = Message.get(params.id)
if (messageInstance) {
render(template:"details" , model:[messageInstance:messageInstance])
}
else {
render "No message found with id: ${params.id}"
}
}

grails template

You can include a GSP template in a GSP page with the <g:render> tag, like this:

<g:render template="someTemplate" />

This line would render a template called _someTemplate.gsp in the same directory as the page that it is being called from. To render templates from a different directory, add the path before the name of the template. We never include the “_” at the beginning of the template name in the <g:render> tag.

Tuesday, March 1, 2011

Groovy convert underscored string to camel case

I cannot find a good convertor online. So I did it in groovy web console

http://groovyconsole.appspot.com/

static String toCamelCase( String text, boolean capitalized = false ) {
text = text.replaceAll( "(_)([A-Za-z0-9])", { Object[] it -> it[2].toUpperCase() } )
return capitalized ? capitalize(text) : text
}
static String toSnakeCase( String text ) {
text.replaceAll( /([A-Z])/, /_$1/ ).toLowerCase().replaceAll( /^_/, '' )
}
println toCamelCase("type_requisition")

The result is

typeRequisition