hanami/utils

Idea: Transform classes and modules only with singleton methods into modules with `extend self` or `module_function`.

serradura opened this issue · 1 comments

Today I read part of the source code looking for the approach used to define static/utility methods.

And I saw two approaches:

  1. A class only with public static methods. e.g: Hanami::Utils::Blank
  2. 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:

  1. The API still the same. e.g: Hanami::Utils::Blank.blank? ' '
  2. 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:

  1. Allow to use the private keyword, removing:
  1. 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.

@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.