Sha256: 653d21cca8ae3440cd2d7359282fcbc63378f7ffb3b2bf5c86527769bc5bd43b

Contents?: true

Size: 1002 Bytes

Versions: 1

Compression:

Stored size: 1002 Bytes

Contents

require 'notification_center/core_ext/module'
require 'notification_center/configuration'
require 'set'

module NotificationCenter
  extend Configuration
  class << self
    def events; @@events end
    def events= hash; @@events = hash end

    def flush_cache!; @cache = nil end

    def _initialize_event_store
      @@events = Hash.new Array.new
    end
    alias :forget_observers! :_initialize_event_store

    def post_notification *args
      event = args.shift
      if enable_notifications
        enable_cache ? _with_cache(event){ _notify! event, *args } : _notify!(event, *args)
      end
    end

    def _notify! event, *args
      event_handler = "#{event}_#{method_suffix}"
      receivers = @@events[event]
      for receiver in receivers
        receiver.send event_handler, *args
      end
    end

    def _with_cache event
      @cache ||= Set.new
      unless @cache.include? event
        yield
        @cache << event
      end
    end
  end
  self._initialize_event_store
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
notification_center-0.2 lib/notification_center.rb