Module: MongoWatchable::Watcher

Defined in:
lib/mongo_watchable/watcher.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(watcher) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mongo_watchable/watcher.rb', line 3

def self.included(watcher)
  watcher.class_eval do
    MongoWatchable.watchables.each do |watchable|
      watchable_key_prefix = watchable.name.underscore.gsub(/\//, '_')
      key :"#{watchable_key_prefix}_watching_ids", Array
      key :"#{watchable_key_prefix}_watchings_count", Integer, :default => 0
      ensure_index :"#{watchable_key_prefix}_watchings_count"
      
      define_method :"#{watchable_key_prefix}_watchings" do
        MongoWatchable::Proxy.new(self, :"#{watchable_key_prefix}_watching_ids", :"#{watchable_key_prefix}_watchings_count", watchable)
      end
    end
  end
  
  MongoWatchable.watchables.each do |watchable|
    watchable.class_eval do
      watcher_key_prefix = watcher.name.underscore.gsub(/\//, '_')
      key :"#{watcher_key_prefix}_watcher_ids", Array
      key :"#{watcher_key_prefix}_watchers_count", Integer, :default => 0
      ensure_index :"#{watcher_key_prefix}_watchers_count"
      
      define_method :"#{watcher_key_prefix}_watchers" do
        MongoWatchable::Proxy.new(self, :"#{watcher_key_prefix}_watcher_ids", :"#{watcher_key_prefix}_watchers_count", watcher)
      end
    end
  end
  
  MongoWatchable.register_watcher watcher
end

Instance Method Details

#unwatch(watchable) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/mongo_watchable/watcher.rb', line 53

def unwatch(watchable)
  if watching?(watchable)
    watchings_proxy_for(watchable).delete watchable
    watchable.watchers_proxy_for(self).delete self
    save if watchable.save
  end
end

#unwatch!(watchable) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/mongo_watchable/watcher.rb', line 61

def unwatch!(watchable)
  raise "#{self.class.name} is not watching #{watchable.class.name}" unless watching?(watchable)
  watchings_proxy_for(watchable).delete watchable
  watchable.watchers_proxy_for(self).delete self
  watchable.save!
  self.save!
end

#watch(watchable) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/mongo_watchable/watcher.rb', line 33

def watch(watchable)
  unless watching?(watchable)
    watchings_proxy_for(watchable) << watchable
    watchable.watchers_proxy_for(self) << self
    save if watchable.save
  end
end

#watch!(watchable) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/mongo_watchable/watcher.rb', line 41

def watch!(watchable)
  raise "#{self.class.name} is already watching #{watchable.class.name}" if watching?(watchable)
  watchings_proxy_for(watchable) << watchable
  watchable.watchers_proxy_for(self) << self
  watchable.save!
  self.save!
end

#watching?(watchable) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/mongo_watchable/watcher.rb', line 49

def watching?(watchable)
  watching_ids_for(watchable).include?(watchable.id)
end

#watching_ids_for(watchable) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/mongo_watchable/watcher.rb', line 69

def watching_ids_for(watchable)
  klass = watchable.class
  while klass.superclass && klass.superclass.include?(MongoWatchable::Watchable)
    klass = klass.superclass
  end
  watchable_key_prefix = klass.name.underscore.gsub(/\//, '_')
  send("#{watchable_key_prefix}_watching_ids")
end

#watchings_count_proxy_for(watchable) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/mongo_watchable/watcher.rb', line 87

def watchings_count_proxy_for(watchable)
  klass = watchable.class
  while klass.superclass && klass.superclass.include?(MongoWatchable::Watchable)
    klass = klass.superclass
  end
  watchable_key_prefix = klass.name.underscore.gsub(/\//, '_')
  send("#{watchable_key_prefix}_watchings")
end

#watchings_proxy_for(watchable) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/mongo_watchable/watcher.rb', line 78

def watchings_proxy_for(watchable)
  klass = watchable.class
  while klass.superclass && klass.superclass.include?(MongoWatchable::Watchable)
    klass = klass.superclass
  end
  watchable_key_prefix = klass.name.underscore.gsub(/\//, '_')
  send("#{watchable_key_prefix}_watchings")
end