Sha256: 9845e898be5bbedaee21591cbea97a465f1a599ef0b97a98c4474794969feb46

Contents?: true

Size: 1.53 KB

Versions: 36

Compression:

Stored size: 1.53 KB

Contents

module AmaLayout
  # Usage:
  #
  # class MyClass
  #   include AmaLayout::Notifications
  #
  #   notification_store AmaLayout::Notifications::RedisStore.new(options)
  #   notification_foreign_key :a_method_name_or_proc # defaults to :id
  #
  #   ...
  # end
  #
  module Notifications
    InvalidNotificationStore = Class.new(StandardError)

    def self.included(base)
      base.extend(ClassMethods)
      base.include(InstanceMethods)
    end

    module InstanceMethods
      def notifications
        @notifications ||= NotificationSet.new(_store, _foreign_key)
      end

      def notifications=(other)
        @notifications = other
      end

      private

      def _store
        self.class._notification_store || invalid_store!
      end

      def _foreign_key
        self.class._notification_foreign_key.call(self)
      end

      def invalid_store!
        raise InvalidNotificationStore, 'a notification store must be specified'
      end
    end

    module ClassMethods
      def notification_store(store)
        self._notification_store = store
      end

      def notification_foreign_key(key)
        self._notification_foreign_key = key
      end

      def _notification_foreign_key
        @_notification_foreign_key || Proc.new(&:id)
      end

      def _notification_store
        @_notification_store
      end

      private

      def _notification_store=(store)
        @_notification_store = store
      end

      def _notification_foreign_key=(key)
        @_notification_foreign_key = key.to_proc
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
ama_layout-11.5.1 lib/ama_layout/notifications.rb
ama_layout-11.5.0 lib/ama_layout/notifications.rb
ama_layout-11.4.0 lib/ama_layout/notifications.rb
ama_layout-11.3.1 lib/ama_layout/notifications.rb
ama_layout-11.3.0 lib/ama_layout/notifications.rb
ama_layout-11.2.0 lib/ama_layout/notifications.rb
ama_layout-11.1.0 lib/ama_layout/notifications.rb
ama_layout-10.2.0 lib/ama_layout/notifications.rb
ama_layout-10.1.0 lib/ama_layout/notifications.rb
ama_layout-10.0.1 lib/ama_layout/notifications.rb
ama_layout-9.5.3 lib/ama_layout/notifications.rb
ama_layout-9.5.2 lib/ama_layout/notifications.rb
ama_layout-9.5.1 lib/ama_layout/notifications.rb
ama_layout-9.5.0 lib/ama_layout/notifications.rb
ama_layout-9.4.1 lib/ama_layout/notifications.rb
ama_layout-9.4.0 lib/ama_layout/notifications.rb
ama_layout-9.3.0 lib/ama_layout/notifications.rb
ama_layout-9.2.0 lib/ama_layout/notifications.rb
ama_layout-9.1.0 lib/ama_layout/notifications.rb
ama_layout-9.0.0 lib/ama_layout/notifications.rb