Private methods are a great way to restrict what methods of a class can be invoked from the outside. These provide a great degree of control when designing classes.
One common pattern we like to follow is to make every other method except perform as private in our service classes.
Example service class:
Note, all methods other than perform including the attr_reader are private as we don’t plan to access them outside this class.
Until now there was no convenient way of making methods created using the delegate to syntax as private.
Consider the goal of creating a service with only one public method(perform), you would expect the following code to do the trick but it does not work:
The only option you had was to use the following syntax which is not the most elegant and conventional:
Rails 6 adds option to pass
private: true to delegate.
we can achieve our above goal using the following:
This change is backwards compatible and your code with
private *delegate... will still work.
Note: protected option is not supported yet, there is a plan to add that in the future.