how to call resources plugin's “use” tag as a method

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

how to call resources plugin's “use” tag as a method

domurtag
I'm trying to invoke a tag defined by the resources plugin. The local name of this tag is "use" which is a Groovy keyword. I have tried calling it with :

r.use( module:"jquery" )
ResourceTagLib.metaClass.invokeMethod( "use", [name:"jquery"])
r."use"( module:"jquery" )

but I get the error:

    Expecting at least 2 arguments, a category class and a Closure

is there any way around it?

Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: how to call resources plugin's “use” tag as a method

Marc Palmer

On 15 Feb 2011, at 10:08, domurtag [via Grails Plugins] wrote:

> I'm trying to invoke a tag defined by the resources plugin. The local name of this tag is "use" which is a Groovy keyword. I have tried calling it with :
>
> r.use( module:"jquery" )
> ResourceTagLib.metaClass.invokeMethod( "use", [name:"jquery"])
> r."use"( module:"jquery" )
>
> but I get the error:
>
>     Expecting at least 2 arguments, a category class and a Closure
>
> is there any way around it?
>
> Thanks!
>

Where are you doing this?

More to the point... why?

I'll try to help, with more context.

Marc

Reply | Threaded
Open this post in threaded view
|

Re: how to call resources plugin's “use” tag as a method

domurtag
Hi Marc,

Thanks for your response. I'm attempting to create a taglib of my own which decorates the resource taglib, so I need to invoke it's "use" method.
I presume if you changed the tag name (e.g. to "useResource") then this problem would disappear?

Thanks
Don

Where are you doing this?

More to the point... why?

I'll try to help, with more context.

Marc




If you reply to this email, your message will be added to the discussion below:
http://grails-plugins.847840.n3.nabble.com/how-to-call-resources-plugin-s-use-tag-as-a-method-tp2499907p2500196.html
To unsubscribe from how to call resources plugin's “use” tag as a method, click here.

Reply | Threaded
Open this post in threaded view
|

Re: how to call resources plugin's “use” tag as a method

Marc Palmer

On 15 Feb 2011, at 14:02, domurtag [via Grails Plugins] wrote:

> Hi Marc,
>
> Thanks for your response. I'm attempting to create a taglib of my own which decorates the resource taglib, so I need to invoke it's "use" method.
> I presume if you changed the tag name (e.g. to "useResource") then this problem would disappear?

It should work fine, try an explicit cast:

r.use((Map)[modules:'jquery'])

Marc

Reply | Threaded
Open this post in threaded view
|

Re: how to call resources plugin's “use” tag as a method

domurtag
It doesn't seem to work, because it expects the "use" keyword that is
used with Cateogries, e.g.

use(MyCategory) {
     // MyCategory is used in here
}

For example, if you try and execute the following in the Groovy console

class Foo {
   def use = {Map m -> println m}
}
new Foo().use([1:2])

you'll see the same error. Adding an explicit cast makes no difference.
Is there a reason why you can't change the name to something other than
"use"?

Thanks,
Don

On 15/02/2011 14:29, Marc Palmer [via Grails Plugins] wrote:
> It should work fine, try an explicit cast:
>
> r.use((Map)[modules:'jquery'])
>
> Marc

Reply | Threaded
Open this post in threaded view
|

Re: how to call resources plugin's “use” tag as a method

Marc Palmer

On 15 Feb 2011, at 14:39, domurtag [via Grails Plugins] wrote:

> It doesn't seem to work, because it expects the "use" keyword that is
> used with Cateogries, e.g.
>
> use(MyCategory) {
>      // MyCategory is used in here
> }
>
> For example, if you try and execute the following in the Groovy console
>
> class Foo {
>    def use = {Map m -> println m}
> }
> new Foo().use([1:2])
>
> you'll see the same error. Adding an explicit cast makes no difference.
> Is there a reason why you can't change the name to something other than
> "use"?
>

It should work, because GDK's Object.use does not take the same arguments.

However it seems like the TagLib implementation in Grails is using methodMissing to implement method call semantics for tags. Just a hunch.

Even then, something funny is happening because the Map is being coerced to something else, if it is resolving to the "use" methods in Object.

I'm afraid there's no chance of us changing the tag r:use to another name any time soon, it will break many production apps.

We need to isolate the cause of this strange invocation problem.

In the meantime, try a very ugly hack:

r.methodMissing('use', argsMap)

Marc



Reply | Threaded
Open this post in threaded view
|

Re: how to call resources plugin's “use” tag as a method

domurtag
On 15/02/2011 16:31, Marc Palmer [via Grails Plugins] wrote:

> It should work, because GDK's Object.use does not take the same
> arguments.
>
> However it seems like the TagLib implementation in Grails is using
> methodMissing to implement method call semantics for tags. Just a hunch.
>
> Even then, something funny is happening because the Map is being
> coerced to something else, if it is resolving to the "use" methods in
> Object.
>
> I'm afraid there's no chance of us changing the tag r:use to another
> name any time soon, it will break many production apps.
>
If it's not possible to remove "use", would you consider adding a second
method, (e.g. "useResource") which does the same thing as use?
Reply | Threaded
Open this post in threaded view
|

Re: how to call resources plugin's “use” tag as a method

Marc Palmer

On 15 Feb 2011, at 16:49, domurtag [via Grails Plugins] wrote:

> On 15/02/2011 16:31, Marc Palmer [via Grails Plugins] wrote:
>
> > It should work, because GDK's Object.use does not take the same
> > arguments.
> >
> > However it seems like the TagLib implementation in Grails is using
> > methodMissing to implement method call semantics for tags. Just a hunch.
> >
> > Even then, something funny is happening because the Map is being
> > coerced to something else, if it is resolving to the "use" methods in
> > Object.
> >
> > I'm afraid there's no chance of us changing the tag r:use to another
> > name any time soon, it will break many production apps.
> >
> If it's not possible to remove "use", would you consider adding a second
> method, (e.g. "useResource") which does the same thing as use?

That would be redundant, not a great idea.

Did the missingMethod workaround work for you?

We will have to debate changing the tag name, but it won't be happening any time very soon.

Like I said, I think there's a bug somewhere in the resolution of methods - I don't see why r.use(Map) should be confused with any of the others - except maybe it is being coerced to a List with length 1. That would be annoying.

Marc


Reply | Threaded
Open this post in threaded view
|

Re: how to call resources plugin's “use” tag as a method

domurtag

> That would be redundant, not a great idea.
It wouldn't be entirely redundant, because (as things currently stand)
it would be the only way to invoke this functionality using the "method
syntax"
>
> Did the missingMethod workaround work for you?
No
>
> We will have to debate changing the tag name, but it won't be
> happening any time very soon.
>
> Like I said, I think there's a bug somewhere in the resolution of
> methods - I don't see why r.use(Map) should be confused with any of
> the others - except maybe it is being coerced to a List with length 1.
> That would be annoying.
Thanks again
Reply | Threaded
Open this post in threaded view
|

Re: how to call resources plugin's “use” tag as a method

Marc Palmer

On 15 Feb 2011, at 17:16, domurtag [via Grails Plugins] wrote:

>
> > That would be redundant, not a great idea.
> It wouldn't be entirely redundant, because (as things currently stand)
> it would be the only way to invoke this functionality using the "method
> syntax"
> >
> > Did the missingMethod workaround work for you?
> No
> >
> > We will have to debate changing the tag name, but it won't be
> > happening any time very soon.
> >
> > Like I said, I think there's a bug somewhere in the resolution of
> > methods - I don't see why r.use(Map) should be confused with any of
> > the others - except maybe it is being coerced to a List with length 1.
> > That would be annoying.
> Thanks again

Try this:

def m = r.metaClass.respondsTo(r, 'use', [Map])
if (m) {
   m.invoke(r, args)
}

Something like that may work... respondsTo will return the method.

I'm not sure "r" is the taglib instance though.

Marc