Referencing Map attribute via a plugin convention object


(steve) #1

I expose a Map through a convention object associated with a plugin (the jdocbook plugin). The only way I have found to actually set values in that Map has been:

transformerParameters([

‘html.stylesheet’: “css/hibernate.css”

])

which just seems ugly to me. Is there a way to achieve this using a more succinct syntax? Thinking something like:

transformerParameters{

‘html.stylesheet’: “css/hibernate.css”

}


(Adam Murdoch) #2

You should be able to do something like this:

transformerParameters 'html.stylesheet': 'css/hibernate.css'

(Luke Daley) #3

That’s going to assign that map to the property, not add to the existing map.

I don’t quite understand what you want here Steve. Are you setting the map or adding to the existing?


(steve) #4

Thanks for the replies!

Luke, I just want to understand the various syntaxes available to me. Eventually i will need the abilitu to add to as well as to redefine that map.


(Luke Daley) #5

There’s two others which are worth knowing about…

class Thing {
  def map = [:]
}
  def t = new Thing()
  configure(t.map) {
  a = 1
}
  t.map.with {
 b = 2
}
  assert t.map == [a: 1, b: 2]

The ‘configure()’ method is Project.configure() and ‘with()’ is a standard Groovy feature patched onto ‘Object’.


(steve) #6

Just getting back to this, sorry for the delay. Luke, another problem here is that the map keys can contain special characters. This is a map of configuration values for an XSLT processor so we see lots of keys with dots for example.

So, while this works

transformerParameters 'html.stylesheet': "css/hibernate.css"

this does not

transformerParameters.with {
    'html.stylesheet' = "css/hibernate.css"
}

nor does

transformerParameters.with {
    html.stylesheet = "css/hibernate.css"
}

I guess I have the “redefine” syntax based on Adam’s suggestion.

For “adding”, one option (I guess) is to add a method to my object:

def transformerParameter(String key, Object value) {
    transformerParameters.put( key, value );
}

(Luke Daley) #7

Yeah, that’s where this approach breaks down. You’d have to do the following:

transformerParameters.with {
    delegate.'html.stylesheet' = "css/hibernate.css"
}

Or…

transformerParameters.with {
    it.'html.stylesheet' = "css/hibernate.css"
}