Module: Oink::Instrumentation::ActiveRecord

Defined in:
lib/oink/instrumentation/active_record.rb

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



20
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/oink/instrumentation/active_record.rb', line 20

def self.included(klass)
  klass.class_eval do

    def self.reset_instance_type_count
      self.instantiated_hash = {}
      Thread.current['oink.activerecord.instantiations_count'] = nil
    end

    def self.increment_instance_type_count
      self.instantiated_hash ||= {}
      self.instantiated_hash[base_class.name] ||= 0
      self.instantiated_hash[base_class.name] += 1
    end

    def self.instantiated_hash
      Thread.current['oink.activerecord.instantiations'] ||= {}
    end

    def self.instantiated_hash=(hsh)
      Thread.current['oink.activerecord.instantiations'] = hsh
    end

    def self.total_objects_instantiated
      Thread.current['oink.activerecord.instantiations_count'] ||= self.instantiated_hash.values.sum
    end

    unless Oink.extended_active_record?
      class << self
        alias_method :allocate_before_oink, :allocate

        def allocate
          value = allocate_before_oink
          increment_instance_type_count
          value
        end
      end

      alias_method :initialize_before_oink, :initialize

      def initialize(*args, &block)
        value = initialize_before_oink(*args, &block)
        self.class.increment_instance_type_count
        value
      end

      Oink.extended_active_record!
    end
  end
end