Do not check for method name in "method_missing"

Description

# in app/models/repository.rb

def method_missing(m, *args, &block)
  if m == :lookup && !block_given?
    lookup_cache[m] ||= {}
    lookup_cache[m][args.join(":")] ||= raw_repository.__send__(m, *args, &block) # rubocop:disable GitlabSecurity/PublicSend
  else
    raw_repository.__send__(m, *args, &block) # rubocop:disable GitlabSecurity/PublicSend
  end
end

Checking the method name in method_missing contradicts the purpose of method_missing. If we want to do something special for lookup, we should define it as a regular method.

Proposal

def lookup(*args, &block)
  if block_given?
    raw_repository.lookup(*args, &block)
  else
    lookup_cache[:lookup] ||= {}
    lookup_cache[:lookup][args.join(":")] ||= raw_repository.lookup(*args)
  end
end

def method_missing(m, *args, &block)
  raw_repository.__send__(m, *args, &block) # rubocop:disable GitlabSecurity/PublicSend
end

This can be further simplified knowing that:

  • lookup on raw_repository always accepts a single argument
  • we never pass a block
Assignee Loading
Time tracking Loading