Sha256: 017afd107e29a627ce67707007d4649fb1d6694599a9f7d6ebcd3250519333e7

Contents?: true

Size: 1.49 KB

Versions: 42

Compression:

Stored size: 1.49 KB

Contents

module ActiveSupport
  module Notifications
    # This is a default queue implementation that ships with Notifications. It
    # just pushes events to all registered log subscribers.
    class Fanout
      def initialize
        @subscribers = []
        @listeners_for = {}
      end

      def subscribe(pattern = nil, block = Proc.new)
        subscriber = Subscriber.new(pattern, block).tap do |s|
          @subscribers << s
        end
        @listeners_for.clear
        subscriber
      end

      def unsubscribe(subscriber)
        @subscribers.reject! {|s| s.matches?(subscriber)}
        @listeners_for.clear
      end

      def publish(name, *args)
        listeners_for(name).each { |s| s.publish(name, *args) }
      end

      def listeners_for(name)
        @listeners_for[name] ||= @subscribers.select { |s| s.subscribed_to?(name) }
      end

      def listening?(name)
        listeners_for(name).any?
      end

      # This is a sync queue, so there is not waiting.
      def wait
      end

      class Subscriber #:nodoc:
        def initialize(pattern, delegate)
          @pattern = pattern
          @delegate = delegate
        end

        def publish(message, *args)
          @delegate.call(message, *args)
        end

        def subscribed_to?(name)
          !@pattern || @pattern === name.to_s
        end

        def matches?(subscriber_or_name)
          self === subscriber_or_name ||
            @pattern && @pattern === subscriber_or_name
        end
      end
    end
  end
end

Version data entries

42 entries across 42 versions & 3 rubygems

Version Path
social_url_stats-0.0.1 vendor/ruby/1.9.1/gems/activesupport-3.0.0/lib/active_support/notifications/fanout.rb
activesupport-3.0.20 lib/active_support/notifications/fanout.rb
activesupport-3.0.19 lib/active_support/notifications/fanout.rb
activesupport-3.0.18 lib/active_support/notifications/fanout.rb
activesupport-3.0.17 lib/active_support/notifications/fanout.rb
activesupport-3.0.16 lib/active_support/notifications/fanout.rb
activesupport-3.0.15 lib/active_support/notifications/fanout.rb
activesupport-3.0.14 lib/active_support/notifications/fanout.rb
activesupport-3.0.13 lib/active_support/notifications/fanout.rb
activesupport-3.0.13.rc1 lib/active_support/notifications/fanout.rb
activesupport-3.0.12 lib/active_support/notifications/fanout.rb
activesupport-3.0.12.rc1 lib/active_support/notifications/fanout.rb
activesupport-3.0.11 lib/active_support/notifications/fanout.rb
messagebus_ruby_api-0.4.7 spec/ruby/1.9.1/gems/activesupport-3.0.9/lib/active_support/notifications/fanout.rb
messagebus_ruby_api-0.4.4 spec/ruby/1.9.1/gems/activesupport-3.0.9/lib/active_support/notifications/fanout.rb
activesupport-3.0.10 lib/active_support/notifications/fanout.rb
activesupport-3.0.10.rc1 lib/active_support/notifications/fanout.rb
activesupport-3.0.9 lib/active_support/notifications/fanout.rb
activesupport-3.0.9.rc5 lib/active_support/notifications/fanout.rb
activesupport-3.0.9.rc4 lib/active_support/notifications/fanout.rb