Class: Class

Inherits:
Object show all
Defined in:
lib/epitools/core_ext/class.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.using(*args) ⇒ Object

Return a copy of the class with modules mixed into it.


6
7
8
9
10
11
12
13
14
# File 'lib/epitools/core_ext/class.rb', line 6

def self.using(*args)
  if block_given?
    yield using(*args)
  else
    copy = self.dup
    args.each { |arg| copy.send(:include, arg) }
    copy
  end
end

Instance Method Details

#trace_messages_to(*meths) ⇒ Object

Trace the specified method calls (`meths`, as symbols) to descendends of this class (or all methods if `:*` is supplied). Output is printed to $stderr.


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/epitools/core_ext/class.rb', line 21

def trace_messages_to(*meths)
  return unless $DEBUG
  
  tracers = Module.new
  parent  = self

  $stderr.puts "[*] Tracing messages sent to #{parent} (messages: #{meths.join(", ")})"
  
  meths.each do |meth|
    case meth
    when :*
      tracers.define_method(:send) do |meth, *args, &block|
        p meth, args, block
        super(meth, *args, &block)
      end
    else
      tracers.define_method(meth) do |*args, &block|
        args = args.map(&:inspect)
        args << "&block" if block
        $stderr.puts "[*] #{parent}##{meth}(#{args.join(", ")})"
        if block
          super(*args, &block)
        else
          super(*args)
        end
      end
    end
  end

  self.prepend(tracers)
end