So in the REST web services I'm working on, Merb automagically creates a method for me based on the name of the resource: resource-name_url. For example, if the name of the resource is "story," then the method name is "story_url." I need to call this method from a common render_create_success method I have created to be used by any successful resource creation. I use the "singularized" name of the controller itself to determine the name of the method to call to get the new resource's location (url). I can get away with this because: 1) I am not writing a general purpose method which will be distributed to all ends of the earth, 2) there is a reliable relationship between a REST resource name and its associated controller.
Anyway, here's the way I call the resource-name_url method:
url_method = self.class.name.downcase.singularize + "_url"
self.send(url_method, item)
... where item is the actual resource / data object (like the story, or anything else that has an id).
I suppose the important piece of information here from a dynamic method invocation standpoint is the usage of the send method including the passing of parameters to the target method.
No comments:
Post a Comment