Sha256: cf9a285a9e486193a621803b5fc24803f57ac57d77c07f8bd565828cd03631cd

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

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

    module ClassMethods
      # @param settings (Hash): { as_klass: nil, actions: nil, id: nil }
      def ps_msync_subscribe(attrs, settings = {})
        settings[:as_klass] = (settings[:as_klass] || name).to_s
        actions = settings.delete(:actions) || %i[create update destroy]
        @ps_msync_subscriber_settings = { attrs: attrs }.merge(settings)
        actions.each do |action|
          add_ps_msync_subscriber(settings[:as_klass], action, action, false)
        end
      end

      def ps_msync_class_subscribe(action, as_action: nil, as_klass: nil)
        add_ps_msync_subscriber(as_klass, action, as_action, true)
      end

      def ps_msync_subscriber_settings
        @ps_msync_subscriber_settings || {}
      end

      private

      def add_ps_msync_subscriber(as_klass, action, as_action, direct_mode)
        listener = {
          klass: name,
          as_klass: (as_klass || name).to_s,
          action: action.to_sym,
          as_action: (as_action || action).to_sym,
          direct_mode: direct_mode
        }
        PubSubModelSync::Config.listeners << listener
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pub_sub_model_sync-0.1.2 lib/pub_sub_model_sync/subscriber_concern.rb
pub_sub_model_sync-0.1.1 lib/pub_sub_model_sync/subscriber_concern.rb
pub_sub_model_sync-0.1.0 lib/pub_sub_model_sync/subscriber_concern.rb