Module: Esse::ActiveRecord::Callbacks

Defined in:
lib/esse/active_record/callbacks.rb,
lib/esse/active_record/callbacks/indexing_on_create.rb,
lib/esse/active_record/callbacks/indexing_on_update.rb,
lib/esse/active_record/callbacks/indexing_on_destroy.rb,
lib/esse/active_record/callbacks/update_lazy_attribute.rb

Defined Under Namespace

Classes: IndexingOnCreate, IndexingOnDestroy, IndexingOnUpdate, UpdateLazyAttribute

Class Method Summary collapse

Class Method Details

.fetch!(identifier, operation) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/esse/active_record/callbacks.rb', line 50

def fetch!(identifier, operation)
  key = [operation, identifier].join('_').to_sym
  if registered?(identifier, operation)
    [key, @callbacks[key]]
  else
    raise ArgumentError, "callback #{identifier} for #{operation} operation not registered"
  end
end

.register_callback(identifier, operation, callback_class) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/esse/active_record/callbacks.rb', line 26

def register_callback(identifier, operation, callback_class)
  unless callback_class < Esse::ActiveRecord::Callback
    raise ArgumentError, 'callback_class must be a subclass of Esse::ActiveRecord::Callback'
  end

  key = [operation, identifier].join('_').to_sym

  @callbacks = @callbacks ? @callbacks.dup : {}
  if @callbacks.key?(key)
    raise ArgumentError, "callback #{identifier} for #{operation} operation already registered"
  end

  @callbacks[key] = callback_class
ensure
  @callbacks&.freeze
end

.registered?(identifier, operation) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
# File 'lib/esse/active_record/callbacks.rb', line 43

def registered?(identifier, operation)
  return false unless @callbacks

  key = [operation, identifier].join('_').to_sym
  @callbacks.key?(key)
end

.to_hObject



22
23
24
# File 'lib/esse/active_record/callbacks.rb', line 22

def to_h
  @callbacks || {}.freeze
end