Sha256: 45209feaf594f23121a214ea8c94751eeee42f74eadae11aef24df19c9893009

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

require "apn/application"

module APN
  module MultipleApps
    def self.extended(mod)
      class << mod
        alias_method_chain :notify_sync, :app

        delegate(*Application::DELEGATE_METHODS, to: :current_app, prefix: true, allow_nil: true)

        Application::DELEGATE_METHODS.each do |method_name|
          alias_method :"original_#{method_name}", method_name
          alias_method method_name, :"current_app_#{method_name}"
        end
      end
    end

    def notify_sync_with_app(token, notification)
      if notification.is_a?(Hash)
        notification.symbolize_keys!
        app_name = notification.delete(:app)
      end

      with_app(app_name) do
        notify_sync_without_app(token, notification)
      end
    end

    attr_writer :default_app_name

    def default_app_name
      @default_app_name || 'default'.freeze
    end

    def current_app_name
      @_app_name || default_app_name
    end

    def current_app
      Application::APPS[current_app_name] or \
        raise NameError, "Unregistered APN::Application `#{current_app_name}'"
    end

    def with_app(app_name)
      @_app_name, app_was = app_name.presence, @_app_name
      yield if block_given?
    ensure
      @_app_name = app_was
    end
  end
end

APN.extend APN::MultipleApps

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
apn_sender-2.1.1 lib/apn/multiple_apps.rb
apn_sender-2.1.0 lib/apn/multiple_apps.rb