Class: CatTree::Observer
- Inherits:
-
Object
- Object
- CatTree::Observer
- Defined in:
- lib/cat_tree/observer.rb
Class Method Summary collapse
Instance Method Summary collapse
- #ar_base_count ⇒ Object
- #check ⇒ Object
-
#initialize ⇒ Observer
constructor
A new instance of Observer.
- #notice(object) ⇒ Object
- #same_ar_base_objects ⇒ Object
Constructor Details
#initialize ⇒ Observer
10 11 12 |
# File 'lib/cat_tree/observer.rb', line 10 def initialize @ar_base = {} end |
Class Method Details
.check(&block) ⇒ Object
6 7 8 |
# File 'lib/cat_tree/observer.rb', line 6 def self.check(&block) self.new.check(&block) end |
Instance Method Details
#ar_base_count ⇒ Object
22 23 24 |
# File 'lib/cat_tree/observer.rb', line 22 def ar_base_count @ar_base.values.inject(0){|t,v| t + v[:count]} end |
#check ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/cat_tree/observer.rb', line 30 def check ActiveRecord::Base.add_cat_tree_observer(self) yield ensure ActiveRecord::Base.remove_cat_tree_observer end |
#notice(object) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/cat_tree/observer.rb', line 14 def notice(object) return if object.new_record? key = "#{object.class.name}(id:#{object.id})" @ar_base[key] ||= {:count => 0, :callers => []} @ar_base[key][:count] += 1 record_backtrace(@ar_base[key][:callers]) if Config.backtrace end |
#same_ar_base_objects ⇒ Object
26 27 28 |
# File 'lib/cat_tree/observer.rb', line 26 def same_ar_base_objects Hash[*(@ar_base.select{|k,v| v[:count] > 1}.flatten)] end |