Module: MonkeyBars::SuperSuper

Defined in:
lib/monkey_bars/patch.rb

Instance Method Summary collapse

Instance Method Details

#super_super(*args, **kwargs) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/monkey_bars/patch.rb', line 94

def super_super(*args, **kwargs)
  # Get the calling method name from the caller stack
  # Caller format can be: "file.rb:line:in 'method_name'" or "file.rb:line:in `method_name'"
  caller_line = caller(1..1).first
  calling_method_name = caller_line[/in [`'](.*?)[`']/, 1] || caller_line[/in (.*?)$/, 1]

  raise "Could not determine calling method name from: #{caller_line}" if calling_method_name.nil?

  if is_a?(Class)
    # For class methods (when extended on a class)
    unless superclass.respond_to?(:singleton_method) && superclass.singleton_methods.include?(calling_method_name.to_sym)
      raise NoMethodError, "undefined method `#{calling_method_name}' for class `#{superclass}'"
    end
    superclass.singleton_method(calling_method_name).call(*args, **kwargs)
  else
    # For instance methods
    unless self.class.superclass&.method_defined?(calling_method_name.to_sym)
      raise(NoMethodError, "undefined method `#{calling_method_name}' for class `#{self.class.superclass}'")
    end
    self.class.superclass.instance_method(calling_method_name).bind_call(self, *args, **kwargs)
  end
end