Idea: Transform classes and modules only with singleton methods into modules with `extend self` or `module_function`.
serradura opened this issue · 1 comments
serradura commented
Today I read part of the source code looking for the approach used to define static/utility methods.
And I saw two approaches:
- A class only with public static methods. e.g: Hanami::Utils::Blank
- A module with public and private static methods. e.g: Hanami::Utils::Files
Ideas for the first approach:
Use a module with module_function
instead of a class.
Benefits:
- The API still the same. e.g:
Hanami::Utils::Blank.blank? ' '
- The user could include the module in their classes and avoid to typing the module namespaces. e.g:
class Person < Struct.new(:name)
include Hanami::Utils::Blank
def name?
# Using module_function the methods (filled? and blank?) will be private
filled? name
end
end
Ideas for the second:
Use a module with extend self
Benefits:
- Allow to use the private keyword, removing:
- A comment to express this group/kind of method.
- The usage of
private_class_method
in modules.
- Allow to create singleton methods without the usage of
self
.
Note: You could use extend self
to the first case, but the module public methods will be public instance methods.
jodosha commented
@serradura I've been coming to this issue from time to time. It's a neat idea, but until we don't get a real use case, I'll close it and mark for future consideration.