Module: Wallets::Callbacks
- Defined in:
- lib/wallets/callbacks.rb
Overview
Centralized callback dispatcher with error isolation. Callback failures should never break the ledger write that triggered them.
Class Method Summary collapse
- .dispatch(event, **context_data) ⇒ Object
- .execute_safely(callback, context) ⇒ Object
- .log_debug(message) ⇒ Object
- .log_error(message) ⇒ Object
- .log_warn(message) ⇒ Object
Class Method Details
.dispatch(event, **context_data) ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/wallets/callbacks.rb', line 9 def dispatch(event, **context_data) callback = Wallets.configuration.public_send(:"on_#{event}_callback") return unless callback.is_a?(Proc) context = CallbackContext.new(event: event, **context_data) execute_safely(callback, context) end |
.execute_safely(callback, context) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/wallets/callbacks.rb', line 17 def execute_safely(callback, context) case callback.arity when 1, -1, -2 callback.call(context) when 0 callback.call else log_warn "[Wallets] Callback has unexpected arity (#{callback.arity}). Expected 0 or 1." end rescue StandardError => e log_error "[Wallets] Callback error for #{context.event}: #{e.class}: #{e.}" log_debug e.backtrace.join("\n") end |
.log_debug(message) ⇒ Object
47 48 49 50 51 |
# File 'lib/wallets/callbacks.rb', line 47 def log_debug() if defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger&.debug? Rails.logger.debug() end end |
.log_error(message) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/wallets/callbacks.rb', line 31 def log_error() if defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger Rails.logger.error() else warn end end |
.log_warn(message) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/wallets/callbacks.rb', line 39 def log_warn() if defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger Rails.logger.warn() else warn end end |