Module: Kantox::Split::Hooker::ClassMethods

Defined in:
lib/kantox/split/hooker.rb

Instance Method Summary collapse

Instance Method Details

#hook(default_hooker = nil, **hooks) ⇒ Object

Each handler must implement contructor, receiving ‘ActiveRecord`,

`#save`, `#update` and `#destroy` methods to store the content
into splitted data store, and (optionally) `#to_hash` method,
producing a hash to be splitted and stored.

Parameters:

  • hooks (Hash)

    the hash of ‘method: Handler`, e. g.: `{ create: CreateMapper, destroy: DestroyMapper }`.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/kantox/split/hooker.rb', line 26

def hook default_hooker = nil, **hooks
  @split_hooks_on_commit ||= { hooker: default_hooker }
  @split_hooks_on_commit.merge!(ACTIONS.map { |a| [a, default_hooker] }.to_h) if default_hooker
  @split_hooks_on_commit.merge!(hooks)
  if block_given?
    yielder = Proc.new # reinstantiate &cb
    @split_hooks_on_commit.values.uniq.each do |h|
      h.config(&yielder) if h.respond_to? :config
    end
  end
end