Class: Tantot::Agent::Base

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

Direct Known Subclasses

Block, Watcher

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Base

Returns a new instance of Base.



10
11
12
13
14
15
16
17
18
19
20
# 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

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/tantot/agent/base.rb', line 4

def id
  @id
end

#stashObject (readonly)

Returns the value of attribute stash.



4
5
6
# File 'lib/tantot/agent/base.rb', line 4

def stash
  @stash
end

#watchesObject (readonly)

Returns the value of attribute watches.



4
5
6
# File 'lib/tantot/agent/base.rb', line 4

def watches
  @watches
end

Class Method Details

.identify(watch) ⇒ Object

Raises:

  • (NotImplementedError)


6
7
8
# File 'lib/tantot/agent/base.rb', line 6

def self.identify(watch)
  raise NotImplementedError
end

Instance Method Details

#add_watch(watch) ⇒ Object



26
27
28
29
30
# 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) ⇒ Object



65
66
67
# 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_idObject

Raises:

  • (NotImplementedError)


57
58
59
# File 'lib/tantot/agent/base.rb', line 57

def debug_id
  raise NotImplementedError
end

#debug_stashObject



61
62
63
# 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

#optionsObject



22
23
24
# File 'lib/tantot/agent/base.rb', line 22

def options
  @watches.first.options
end

#push(watch, instance, changes_by_attribute) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# 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) ⇒ Object



32
33
34
# File 'lib/tantot/agent/base.rb', line 32

def setup_watch(watch)
  # nop
end

#sweep(strategy_name = nil) ⇒ Object



48
49
50
51
52
53
54
55
# 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