Module: ClassProfiler

Defined in:
lib/class_profiler.rb,
lib/class_profiler/rack.rb,
lib/class_profiler/version.rb

Overview

tons of metaprogramming and hacks, don’t ask..

Defined Under Namespace

Classes: Benchmark, Rack

Constant Summary collapse

CONSTANTIZE =
->(string) {
  string.split(':').select{|i|
    !i.blank? && i.downcase != '<class' && !i.start_with?('0x')
  }.map{|i|
    i.gsub("#","").gsub("<","").gsub(">","")
  }.join("::").constantize #use const_get
}
IGNORED_METHODS =

add some metaprogramming here

[Object, BasicObject, Class, Class.new, Module].map{|o|
  array = []
  [:private_instance_methods, :protected_instance_methods, :instance_methods].each do |m|
    array.concat(o.send(m, true))
  end

  [:private_methods, :protected_methods, :methods].each do |m|
    array.concat(o.send(m, true))
  end
  array
}.flatten.uniq - [:initialize]
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

._get_methods_for(_caller, inherited = false) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/class_profiler.rb', line 72

def self._get_methods_for(_caller, inherited = false)
  _caller.constantize if _caller.is_a? String #use const_get

  array = []
  array.concat(
    _caller.instance_methods(inherited)
  ).concat(
    _caller.protected_instance_methods(inherited)
  ).concat(
    _caller.private_instance_methods(inherited)
  )

  return array unless inherited
  return (array - IGNORED_METHODS).flatten.uniq
end

.for(options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/class_profiler.rb', line 29

def self.for(options = {})
  methods = options[:instance_methods] || []
  _caller = CONSTANTIZE.call(caller_locations(1,1)[0].label)

  if options[:modules]
    methods.concat(
      options[:modules].map{|m|
        _get_methods_for(m)
      }
    )
  end

  @__cp_instance_methods = methods.flatten.uniq

  return self
end

.included(base) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/class_profiler.rb', line 46

def self.included(base)
  @__cp_instance_methods = _get_methods_for(base, true) if @__cp_instance_methods.nil?

  default_protected_methods = self.protected_instance_methods
  default_private_methods = self.private_instance_methods

  (@__cp_instance_methods).each do |method_name|
    base.send(:alias_method, "__#{method_name}", method_name)

    base.send(:define_method, method_name) do |*args, &block|
      Benchmark.instance.start(label(__method__)){
        if block
          self.send("__#{method_name}", *args, &block)
        else
          self.send("__#{method_name}", *args)
        end
      }
    end

    protected(method_name) if default_protected_methods.include?(method_name)
    private(method_name) if default_private_methods.include?(method_name)
  end

  @__cp_instance_methods = nil
end