I was hoping to extract a lot of logic into a separate class in the following code, but I am having trouble with that since it ends up calling a protected method.
This is my current code:
class ExcelSheet
...
protected
def save_stuff
# do work
end
end
class CustomSheet < ExcelSheet
def custom_stuff
# lots of logic
save_stuff
# more logic
end
end
This was my attempted code:
class LogicManager
def logic_valid?
# lots of logic
save_stuff
end
end
class CustomSheet < ExcelSheet
def custom_stuff
manager = LogicManager.new(some_data)
if manager.logic_valid?
# more logic
end
end
end
Unfortunately in my LogicManager
I can't call save_stuff
because it's protected
. I didn't write the original method, and I'm sure it's marked as protected
for a reason, so I don't think I should change that.
What options do I have to still refactor nicely?
It seems that you have a bit of a misunderstanding of protected
methods.
Protected methods can be invoked only by the class that defines it, or by it's subclasses. In your case, it means that if you would like to invoke the method save_stuff
in the class LogicManager
, that means that LogicManager
must inherit from ExcelSheet
.
Given the naming of the classes, I am not sure if you want to do that as it doesn't seem logical to me (no pun intended).