Module: Foobara::Entity::Concerns::Callbacks

Includes:
Concern
Included in:
Foobara::Entity
Defined in:
foobara-0.5.8/projects/entities/projects/entity/src/concerns/callbacks.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concern

foobara_class_methods_module_for, foobara_concern?, included

Instance Attribute Details

#callback_registryObject

owner helps with determining the relevant object when running class-registered state transition callbacks



17
18
19
# File 'projects/entities/projects/entity/src/concerns/callbacks.rb', line 17

def callback_registry
  @callback_registry
end

Class Method Details

.reset_allObject



10
11
12
13
# File 'projects/entities/projects/entity/src/concerns/callbacks.rb', line 10

def reset_all
  Foobara.raise_if_production!("reset_all")
  Entity.instance_variable_set("@class_callback_registry", nil)
end

Instance Method Details

#fire(action, data = {}) ⇒ Object

TODO: support passing multiple actions here



25
26
27
# File 'projects/entities/projects/entity/src/concerns/callbacks.rb', line 25

def fire(action, data = {})
  callback_registry.runner(action).callback_data(data.merge(record: self, action:)).run
end

#initializeObject



19
20
21
22
# File 'projects/entities/projects/entity/src/concerns/callbacks.rb', line 19

def initialize(...)
  self.callback_registry = Callback::Registry::ChainedMultipleAction.new(self.class.class_callback_registry)
  super
end

#register_callbackObject



40
41
42
# File 'projects/entities/projects/entity/src/concerns/callbacks.rb', line 40

def register_callback(...)
  callback_registry.register_callback(...)
end

#without_callbacksObject



29
30
31
32
33
34
35
36
37
38
# File 'projects/entities/projects/entity/src/concerns/callbacks.rb', line 29

def without_callbacks
  old_callbacks_enabled = @callbacks_disabled

  begin
    @callbacks_disabled = true
    yield
  ensure
    @callbacks_disabled = old_callbacks_enabled
  end
end