When writing an API with Grape, why bother using the helpers
macro, versus just including a module, or adding a method?
For example, you can define methods in a module and include them as helpers in Grape like so:
module HelperMethods
def useful_method(param)
"Does a thing with #{param}"
end
end
class HelpersAPI < Grape::API
helpers HelperMethods
get 'do_stuff/:id' do
useful_method(params[:id])
end
end
But, why not just do this?
class IncludeAPI < Grape::API
include HelperMethods
get 'do_stuff/:id' do
useful_method(params[:id])
end
end
I guess it's a little more explicit that you're including the HelperMethods
module for the purpose of providing helper methods, but that seems like a weak reason to add an alternative syntax.
What are the benefits/reasons that you would want to use helpers
versus just a normal include
?
helpers
includes the module in nested namespaces, too. – Cinquecento