There is more to this subject than what is currently in this post but, for now, this is a cool design approach that I lynched from the community-driven Ruby Style Guide.
For a long time I have been quite the fan of Service Objects.
However, for a while I’ve been uncomfortable with how I’ve been using them; they were doing more than one thing and they never needed to be instantiated. Therefore, they would probably be better off as Modules. However, I didn’t want to relinquish being able to use the class method - I liked being able to do ServiceObject.call
.
This is one of my ‘Service Objects’ that I wasn’t happy with being a Service Object:
This is useful because you can do GetData.open_pull_requests(args)
from anywhere in a file that you’ve required get_data.rb
in.
However, it’s arguably not appropriate being a Service Object; arguably, it does more than one ‘thing’.
module_function
to the Rescue
Benefits:
- Class-Method-Like functionality maintained (
GetData.open_pull_requests(args)
) - More readable
- No need to
include
it anywhere in order to use it.