Sha256: f5993fe42c6ca9c212e78088cab70e2878d30c927a2ce30d2dc94b804f5b2bc6

Contents?: true

Size: 1.1 KB

Versions: 3

Compression:

Stored size: 1.1 KB

Contents

module CatTree
  class Observer
    class ArBase
      attr_reader :count, :callers

      def initialize(object)
        return unless object_valid?(object)
        @valid = true

        @model = object.class.name
        @model_id = object.id
        @count = 1
        @callers = []

        record_backtrace if CatTree::Config.backtrace
      end

      def merge(other)
        @count += other.count
        @callers.concat(other.callers)
        self
      end

      def ==(other)
        key == other.key
      end

      def key
        [@model, @model_id]
      end

      def valid?
        !!@valid
      end

      def title
        "#{@model}(id:#{@model_id})"
      end

      private

      def object_valid?(object)
        object.is_a?(ActiveRecord::Base) && !object.new_record?
      end

      def record_backtrace
        @callers << (defined?(Rails) ? backtrace_in_rails : caller)
      end

      def backtrace_in_rails
        root_path = Rails.root.to_s
        root_path += "/" unless root_path.last == "/"
        caller.select{|c| c =~ %r!#{root_path}(app|lib)/!}
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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