Class: FocalPoint

Inherits:
Object
  • Object
show all
Includes:
Multiton
Defined in:
lib/focal_point.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Multiton

#clone, #dup

Constructor Details

#initialize(target) ⇒ FocalPoint

Returns a new instance of FocalPoint.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/focal_point.rb', line 16

def initialize(target)
  @target = String(target)
  @timer = Hitimes::TimedMetric.new(@target)

  if @target =~ /^([^#.]+)(#|\.)(.*)$/
    @method, @scope = case $2
                      when '#' ## we're instrumenting an instance method
                        [$3, eval($1)]
                      when '.' ## we're instrumenting a module method
                        [$3, (class << eval($1); self; end)]
                      end
  else
    $stdout.puts "FocalPoint::Error: Not sure how to instrument #{@target}"
  end

  unless @scope.instance_methods.include?(@method) && false
    # did you mean?? 
  end

  @scope.module_eval(source)
end

Instance Attribute Details

#timerObject

Returns the value of attribute timer.



10
11
12
# File 'lib/focal_point.rb', line 10

def timer
  @timer
end

Class Method Details

.[](target) ⇒ Object



12
13
14
# File 'lib/focal_point.rb', line 12

def self.[] target
  new(target)
end


50
51
52
53
54
55
56
57
58
# File 'lib/focal_point.rb', line 50

def self.print_timers
  timers = []
  ObjectSpace.each_object(FocalPoint) { |fp| timers << fp.timer }
  timers.compact.sort_by(&:sum).each do |timer|
    puts '-'*80
    pp timer.to_hash
    puts
  end
end

Instance Method Details

#sourceObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/focal_point.rb', line 38

def source 
  <<-CODE
    alias :real_#{@method} :#{@method}
    def #{@method}(*args, &block)
      FocalPoint[#{@target.inspect}].timer.measure do
        real_#{@method}(*args, &block)
      end
    end
  CODE
end