module ReactiveObservers::Observable::Base

enables class to be observable automatically included to ActiveRecord::Base

Public Instance Methods

process_observer_hook_notification(action, **options) click to toggle source

process notification from ActiveRecord hooks @param action [Symbol] @param options [Hash]

# File lib/reactive_observers/observable/base.rb, line 88
def process_observer_hook_notification(action, **options)
  return if ReactiveObservers.configuration.observed_tables.include?(self.class.table_name.to_sym) || self.class.active_observers.blank?

  process_observer_notifications action, **options
end
process_observer_notification(data) click to toggle source

process notification from db trigger @param data [Hash] data obtain from db trigger

# File lib/reactive_observers/observable/base.rb, line 59
def process_observer_notification(data)
  return if active_observers.blank?

  if data[:action] == 'INSERT'
    find(data[:id]).process_observer_notifications :create
  elsif data[:action] == 'UPDATE'
    find(data[:id]).process_observer_notifications :update, diff: data[:diff]
  elsif data[:action] == 'DELETE'
    new(data[:diff]).process_observer_notifications :destroy
  else
    raise StandardError, "Notification from db returned unknown action: #{data[:action]}"
  end
end
process_observer_notifications(action, **options) click to toggle source

process observer notification @param action [Symbol] @param options [Hash]

# File lib/reactive_observers/observable/base.rb, line 97
def process_observer_notifications(action, **options)
  Observable::Notification.new(self, self.class.active_observers, action, options).perform
end
register_observer(observer) click to toggle source

register observer to this class @param observer [ReactiveObservers::Observer::Container] @return [Array] active observers

# File lib/reactive_observers/observable/base.rb, line 39
def register_observer(observer)
  return if active_observers.any? { |active_observer| active_observer.compare.full? observer }

  active_observers << observer
end
remove_observer(observer, **options) click to toggle source

remove observer for specific object

Topic.remove_observer(ActivityObserver) # remove observer from Topic Topic.remove_observer(observing_service) # removed observer can be also object

@param observer [Class, Object] observer that should be removed @param options [Hash] additional options that specifies which observers should be removed @return [Array] still active observers

# File lib/reactive_observers/observable/base.rb, line 53
def remove_observer(observer, **options)
  Observable::Removing.new(active_observers, observer, options).perform
end