Class: Tantot::Agent::Block

Inherits:
Base
  • Object
show all
Defined in:
lib/tantot/agent/block.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

.identify(watch) ⇒ Object



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

def self.identify(watch)
  if watch.block.present?
    CityHash.hash64("#{watch.model.to_s}|#{watch.attributes.inspect}|#{watch.options.inspect}")
  else
    nil
  end
end

Instance Method Details

#debug_block(block) ⇒ Object



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

def debug_block(block)
  location, line = block.source_location
  short_path = defined?(Rails) ? Pathname.new(location).relative_path_from(Rails.root).to_s : location
  "block @ #{short_path}##{line}"
end

#debug_idObject



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

def debug_id
  debug_block(watches.first.block)
end

#perform(changes_by_model) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/tantot/agent/block.rb', line 18

def perform(changes_by_model)
  # Block agent always has only one watch
  block = watches.first.block
  model = watches.first.model
  # Skip the model part of the changes since it will always be on a
  # single model, and wrap it in the ById helper.
  model.instance_exec(Tantot::Changes::ById.new(changes_by_model.values.first), &block)
end

#setup_watch(watch) ⇒ Object



14
15
16
# File 'lib/tantot/agent/block.rb', line 14

def setup_watch(watch)
  raise Tantot::MultipleWatchesProhibited.new("Can't have multiple block watches per model with the same attributes and options: #{debug_block(watch.block)}") if @watches.any?
end