class Tantot::Agent::Base

Attributes

id[R]
stash[R]
watches[R]

Public Class Methods

identify(watch) click to toggle source
# File lib/tantot/agent/base.rb, line 6
def self.identify(watch)
  raise NotImplementedError
end
new(id) click to toggle source
# File lib/tantot/agent/base.rb, line 10
def initialize(id)
  @id = id
  @watches = []
  @stash = Hash.new do |model_hash, model|
    model_hash[model] = Hash.new do |instance_id_hash, instance_id|
      instance_id_hash[instance_id] = Hash.new do |attribute_hash, attribute|
        attribute_hash[attribute] = []
      end
    end
  end
end

Public Instance Methods

add_watch(watch) click to toggle source
# File lib/tantot/agent/base.rb, line 26
def add_watch(watch)
  watch.agent = self
  setup_watch(watch)
  @watches.push(watch)
end
debug_changes_for_model(model, changes_by_id) click to toggle source
# File lib/tantot/agent/base.rb, line 65
def debug_changes_for_model(model, changes_by_id)
  "#{model.name}#{changes_by_id.keys.inspect}"
end
debug_id() click to toggle source
# File lib/tantot/agent/base.rb, line 57
def debug_id
  raise NotImplementedError
end
debug_stash() click to toggle source
# File lib/tantot/agent/base.rb, line 61
def debug_stash
  "#{@stash.collect {|model, changes_by_id| debug_changes_for_model(model, changes_by_id)}.join(" & ")})"
end
options() click to toggle source
# File lib/tantot/agent/base.rb, line 22
def options
  @watches.first.options
end
push(watch, instance, changes_by_attribute) click to toggle source
# File lib/tantot/agent/base.rb, line 36
def push(watch, instance, changes_by_attribute)
  Tantot.logger.debug do
    mutate = changes_by_attribute.size.zero? ? 'destroy' : "#{changes_by_attribute.size} mutations(s)"
    "[Tantot] [Collecting] [#{self.class.name.demodulize}] #{mutate} on <#{instance.class.name}:#{instance.id}> for <#{debug_id}>"
  end
  attribute_hash = @stash[watch.model][instance.id]
  changes_by_attribute.each do |attr, changes|
    attribute_hash[attr] |= changes
  end
  sweep if Tantot.config.sweep_on_push
end
setup_watch(watch) click to toggle source
# File lib/tantot/agent/base.rb, line 32
def setup_watch(watch)
  # nop
end
sweep(strategy_name = nil) click to toggle source
# File lib/tantot/agent/base.rb, line 48
def sweep(strategy_name = nil)
  if @stash.any?
    strategy = Tantot::Strategy.resolve(strategy_name || options[:strategy] || Tantot.config.strategy).new
    Tantot.logger.debug { "[Tantot] [Strategy] [#{self.class.name.demodulize}] [#{strategy.class.name.demodulize}] [#{debug_id}] #{debug_stash}" }
    strategy.run(self, @stash)
    @stash.clear
  end
end