Service Objects
In previous sections, we learned how to install and include Ruby gems to extend the functionality of our application. We also learned how to interact with files and CSV data. We now know how to read, process, and output data to the screen or the filesystem.
This is useful functionality, although opening files and writing CSV data is something that we would generally consider to be a common functionality that is likely to be shared between classes and can also seem unrelated to the existing classes in our code base.
What do we mean by unrelated? Well, let's assume we have a User
class and a Company
class in our application. We want the ability to load user and company data and print it to the Terminal for both of these classes. So, where do we write the code for this functionality? In the User
class? In the Company
class? Or, in both classes?
Possible solutions to the common usage code issue are using modules, service classes, and class inheritance.
...