Module: ClassMethodWrapper

Defined in:
lib/instance_method_wrapper.rb

Class Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



32
33
34
35
36
# File 'lib/instance_method_wrapper.rb', line 32

def self.prepended(base)
  base.singleton_class.instance_methods(false).each do |method_name|
    wrap_method(base, method_name)
  end
end

.wrap_method(base, method_name) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/instance_method_wrapper.rb', line 38

def self.wrap_method(base, method_name)
  base.singleton_class.define_method(method_name) do |*args, **kwargs, &block|
    method = base.method(method_name)
    parameters = method.parameters.map(&:last)

    warn format("%s %.#{$imw_len}s",
                imw_indent(:in, false).to_s,
                ":i:> #{sbase}::#{method_name}: " +
                [args == [] ? nil : "args=#{args.inspect}",
                 kwargs == {} ? nil : "kwargs=#{kwargs.inspect}"].compact.join(', '))
    $imw_depth += 1

    result = if parameters.include?(:key) || parameters.include?(:keyreq)
               method.call(*args, **kwargs, &block)
             else
               method.call(*args, &block)
             end

    $imw_depth -= 1
    warn format("%s %.#{$imw_len}s",
                imw_indent(:out, false).to_s,
                "<:o: #{sbase}::#{method_name}: #{result.inspect}")
    result
  end
end