Module: ActiveRecordInstanceCount::Instrumentation::ActiveRecord

Defined in:
lib/active-record-instance-count/instrumentation/active_record.rb

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



14
15
16
17
18
19
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
# File 'lib/active-record-instance-count/instrumentation/active_record.rb', line 14

def self.included(klass)
  klass.class_eval do

    @@instantiated = {}
    @@total = nil

    def self.reset_instance_type_count
      @@instantiated = {}
      @@total = nil
    end

    def self.increment_instance_type_count
      @@instantiated[base_class.name] ||= 0
      @@instantiated[base_class.name] += 1
    end

    def self.instantiated_hash
      @@instantiated
    end

    def self.total_objects_instantiated
      @@total ||= @@instantiated.values.sum
    end

    unless ActiveRecordInstanceCount.extended_active_record?
      class << self
        alias_method :allocate_before_counter, :allocate

        def allocate
          value = allocate_before_counter
          increment_instance_type_count
          value
        end
      end

      alias_method :initialize_before_counter, :initialize

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

      ActiveRecordInstanceCount.extended_active_record!
    end
  end
end