Module: Oink::OinkInstanceTypeCounterInstanceMethods

Defined in:
lib/oink/rails/instance_type_counter.rb

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/oink/rails/instance_type_counter.rb', line 38

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 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