Class: Tantot::Agent::Watcher

Inherits:
Base
  • Object
show all
Defined in:
lib/tantot/agent/watcher.rb

Instance Attribute Summary

Attributes inherited from Base

#id, #stash, #watches

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#add_watch, #debug_changes_for_model, #debug_stash, #initialize, #options, #push, #sweep

Constructor Details

This class inherits a constructor from Tantot::Agent::Base

Class Method Details

.derive_watcher(name) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/tantot/agent/watcher.rb', line 14

def self.derive_watcher(name)
  watcher =
    if name.is_a?(Class)
      name
    else
      class_name = "#{name.camelize}Watcher"
      watcher = class_name.safe_constantize
      raise Tantot::UnderivableWatcher, "Can not find watcher named `#{class_name}`" unless watcher
      watcher
    end
  raise Tantot::UnderivableWatcher, "Watcher class does not include Tantot::Watcher: #{watcher}" unless watcher.included_modules.include?(Tantot::Watcher)
  watcher
end

.identify(watch) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/tantot/agent/watcher.rb', line 6

def self.identify(watch)
  if watch.watcher.present?
    derive_watcher(watch.watcher)
  else
    nil
  end
end

Instance Method Details

#debug_idObject



41
42
43
# File 'lib/tantot/agent/watcher.rb', line 41

def debug_id
  id.name
end

#perform(changes_by_model) ⇒ Object



37
38
39
# File 'lib/tantot/agent/watcher.rb', line 37

def perform(changes_by_model)
  watcher.new.perform(Tantot::Changes::ByModel.new(changes_by_model))
end

#setup_watch(watch) ⇒ Object



33
34
35
# File 'lib/tantot/agent/watcher.rb', line 33

def setup_watch(watch)
  watch.options.reverse_merge!(watcher.watcher_options)
end

#watcherObject



28
29
30
31
# File 'lib/tantot/agent/watcher.rb', line 28

def watcher
  # The id of the agent is the watcher class (see self#identify)
  id
end