as a new Rubyist, I'm running into a recurring problem when it comes to structure my models.
When a method is too long:
- I try to refactor to a better/shorter syntax
- I try to split some parts into "sub methods"
PROBLEM: I don't know how to split the method properly + whith which tool (private method, modules etc.)
For example:
I need to run Foo.main_class_method
My model looks like this:
class Foo < Applicationrecord
def self.main_class_method
[...] # way too long method with nasty iterations
end
end
I try to split my method to improve lisibility. It becomes :
class Foo < Applicationrecord
def self.main_class_method
[...] # fewer code
self.first_splitted_class_method
self.second_splitted_class_method
end
private
def self.first_splitted_class_method
[...] # some code
end
def self.second_splitted_class_method
[...] # some code
end
end
Result: It works, but I fell like this is not the proper way to do it + I have side effects
- expected: splitted_methods are not accessible, except inside
main_class_method
- got: I can call
Foo.first_splitted_class_method
since class methods "ignore" Private. splitted_class_methods underPrivate
are not private
Question: Is it an acceptable way to split main_class_method
or is it a complete misuse of private method ?