Sha256: 97699a856ef592e161cb9dafa6de9e1e467e3d8d9902eb4f59bc4aef5fe855c6

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 KB

Contents

require 'active_record'
require 'cat_tree/observer/target_set'
require 'cat_tree/logger'

module CatTree
  class Observer
    def self.check(&block)
      self.new.check(&block)
    end

    def initialize
      @target_set = TargetSet.new
    end

    def notice(object)
      @target_set.notice(object)
    end

    def ar_base_count
      @target_set.object_count
    end

    def same_ar_base_objects
      @target_set.same_objects
    end

    def check
      ActiveRecord::Base.add_cat_tree_observer(self)
      yield
    ensure
      ActiveRecord::Base.remove_cat_tree_observer
      output_message
    end

    private

    def output_message
      return if @target_set.empty?

      msg = ["", "[CatTree]"]
      msg << "  ActiveRecord::Base:\t#{ar_base_count}"

      unless (same_objects = same_ar_base_objects).empty?
        msg << "  Same objects:"
        same_objects.each do |same_obj|
          msg << "    #{same_obj.title}:\t#{same_obj.count}"

          if Config.backtrace
            same_obj.callers.each do |cal|
              cal.each{|c| msg << "      #{c}"}
              msg << ""
            end
          end
        end
      end
      msg << ""

      Logger.warn msg.join("\n")
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cat_tree-1.1.1 lib/cat_tree/observer.rb
cat_tree-1.1.0 lib/cat_tree/observer.rb
cat_tree-1.0.0 lib/cat_tree/observer.rb