Module: Tackle

Defined in:
lib/tackle.rb,
lib/tackle/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.check_for_instrumentation(klass, method_name) ⇒ Object

Raises:

  • (ArgumentError)


43
44
45
46
47
# File 'lib/tackle.rb', line 43

def self.check_for_instrumentation(klass, method_name)
  method = "_#{method_name}".to_sym
  raise ArgumentError, "#{method} already instrumented on #{klass}" if klass.instance_methods.include?(method)
  method
end

.check_method(klass, method_name) ⇒ Object

Raises:

  • (ArgumentError)


37
38
39
40
41
# File 'lib/tackle.rb', line 37

def self.check_method(klass, method_name)
  method = method_name.to_sym
  raise ArgumentError, "#{method} not defined on #{klass}" unless klass.instance_methods.include?(method)
  method
end

.decorate(class_name, method_name, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/tackle.rb', line 5

def self.decorate(class_name, method_name, &block)
  klass = Kernel.const_get(class_name)
  method = check_method(klass, method_name)
  old_method = check_for_instrumentation(klass, method_name)

  klass.class_eval do
    alias_method old_method, method

    define_method(method) do |*args|
      send(old_method, *args)
      instance_eval(&block)
    end
  end
end

.time(class_name, method_name, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/tackle.rb', line 20

def self.time(class_name, method_name, &block)
  klass = Kernel.const_get(class_name)
  method = check_method(klass, method_name)
  old_method = check_for_instrumentation(klass, method_name)

  klass.class_eval do
    alias_method old_method, method

    define_method(method) do |*args|
      time = Benchmark.realtime do
        send(old_method, *args)
      end
      block.call(time)
    end
  end
end