class Delfos::Patching::MethodCache

Attributes

added_methods[R]

Public Class Methods

each_method() { |klass, m, class_method| ... } click to toggle source
# File lib/delfos/patching/method_cache.rb, line 23
def each_method
  instance.send(:added_methods).each do |klass, methods|
    methods.each do |k, m|
      class_method = !!(k[/^ClassMethod_/])
      yield klass, m, class_method
    end
  end
end
instance() click to toggle source
# File lib/delfos/patching/method_cache.rb, line 19
def instance
  @instance ||= new
end
new() click to toggle source
# File lib/delfos/patching/method_cache.rb, line 33
def initialize
  @added_methods = {}
end
reset!() click to toggle source
# File lib/delfos/patching/method_cache.rb, line 15
def reset!
  @instance = nil
end

Public Instance Methods

append(klass:, method:) click to toggle source
# File lib/delfos/patching/method_cache.rb, line 47
def append(klass:, method:)
  class_method = method.respond_to?(:receiver) && method.receiver.is_a?(Class)
  key = key_for(class_method, method.name)
  m = fetch(klass)[key]

  fetch(klass)[key] = method if m.nil?
end
files_for(klass) click to toggle source
# File lib/delfos/patching/method_cache.rb, line 37
def files_for(klass)
  fetch(klass).
    values.
    map(&:source_location).
    compact.
    map(&:first).
    compact.
    uniq
end
find(klass:, method_name:, class_method:) click to toggle source
# File lib/delfos/patching/method_cache.rb, line 55
def find(klass:, method_name:, class_method:)
  key = key_for(class_method, method_name)

  fetch(klass)[key]
end

Private Instance Methods

fetch(klass) click to toggle source
# File lib/delfos/patching/method_cache.rb, line 69
def fetch(klass)
  # Find method definitions defined in klass or its ancestors
  super_klass = klass.ancestors.detect do |k|
    (fetch_without_default(k) || {}).values.length.positive?
  end

  added_methods[(super_klass || klass).to_s] ||= {}
end
fetch_without_default(klass) click to toggle source
# File lib/delfos/patching/method_cache.rb, line 78
def fetch_without_default(klass)
  added_methods[klass.to_s]
end
key_for(class_method, method_name) click to toggle source
# File lib/delfos/patching/method_cache.rb, line 65
def key_for(class_method, method_name)
  class_method ? "ClassMethod_#{method_name}" : "InstanceMethod_#{method_name}"
end