Sha256: 5074e6b34d3636f1eec2c53dc6c71a296b67fe3dd85836cb391ef7b68ca8a7a6

Contents?: true

Size: 1.59 KB

Versions: 15

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module PubSubModelSync
  module SubscriberConcern
    def self.included(base)
      base.extend(ClassMethods)
    end

    # check if model was changed to skip nonsense .update!()
    def ps_subscriber_changed?(_data)
      validate
      changed?
    end

    # permit to apply custom actions before applying sync
    # @return (nil|:cancel): nil to continue sync OR :cancel to skip sync
    def ps_before_save_sync(_payload); end

    module ClassMethods
      def ps_subscribe(attrs, actions: nil, from_klass: name, id: :id)
        settings = { id: id, from_klass: from_klass }
        actions ||= %i[create update destroy]
        actions.each do |action|
          add_ps_subscriber(action, attrs, settings)
        end
      end

      def ps_class_subscribe(action, from_action: nil, from_klass: nil)
        settings = { direct_mode: true }
        settings[:from_action] = from_action if from_action
        settings[:from_klass] = from_klass if from_klass
        add_ps_subscriber(action, nil, settings)
      end

      def ps_subscriber(action = :create)
        PubSubModelSync::Config.subscribers.find do |subscriber|
          subscriber.klass == name && subscriber.action == action
        end
      end

      private

      # @param settings (Hash): refer to PubSubModelSync::Subscriber.settings
      def add_ps_subscriber(action, attrs, settings = {})
        klass = PubSubModelSync::Subscriber
        subscriber = klass.new(name, action, attrs: attrs, settings: settings)
        PubSubModelSync::Config.subscribers.push(subscriber) && subscriber
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
pub_sub_model_sync-0.5.9.1 lib/pub_sub_model_sync/subscriber_concern.rb
pub_sub_model_sync-0.5.9 lib/pub_sub_model_sync/subscriber_concern.rb
pub_sub_model_sync-0.5.8.2 lib/pub_sub_model_sync/subscriber_concern.rb
pub_sub_model_sync-0.5.8.1 lib/pub_sub_model_sync/subscriber_concern.rb
pub_sub_model_sync-0.5.8 lib/pub_sub_model_sync/subscriber_concern.rb
pub_sub_model_sync-0.5.7.1 lib/pub_sub_model_sync/subscriber_concern.rb
pub_sub_model_sync-0.5.7 lib/pub_sub_model_sync/subscriber_concern.rb
pub_sub_model_sync-0.5.6 lib/pub_sub_model_sync/subscriber_concern.rb
pub_sub_model_sync-0.5.5 lib/pub_sub_model_sync/subscriber_concern.rb
pub_sub_model_sync-0.5.4.1 lib/pub_sub_model_sync/subscriber_concern.rb
pub_sub_model_sync-0.5.4 lib/pub_sub_model_sync/subscriber_concern.rb
pub_sub_model_sync-0.5.3 lib/pub_sub_model_sync/subscriber_concern.rb
pub_sub_model_sync-0.5.2 lib/pub_sub_model_sync/subscriber_concern.rb
pub_sub_model_sync-0.5.1.1 lib/pub_sub_model_sync/subscriber_concern.rb
pub_sub_model_sync-0.5.1 lib/pub_sub_model_sync/subscriber_concern.rb