Sha256: 5a6e96248e646db6c8ba00597b799a3ecefb4e4e6d0ce99f7081d714ea939f64

Contents?: true

Size: 1.39 KB

Versions: 13

Compression:

Stored size: 1.39 KB

Contents

# encoding: utf-8

require 'monitor'

module FiniteMachine
  # A class responsibile for storage of event subscribers
  class Subscribers
    include Enumerable
    include MonitorMixin

    # Initialize a subscribers collection
    #
    # @api public
    def initialize(machine)
      super()
      @machine     = machine
      @subscribers = []
    end

    # Iterate over subscribers
    #
    # @api public
    def each(&block)
      @subscribers.each(&block)
    end

    # Return index of the subscriber
    #
    # @api public
    def index(subscriber)
      @subscribers.index(subscriber)
    end

    # Check if anyone is subscribed
    #
    # @return [Boolean]
    #
    # @api public
    def empty?
      @subscribers.empty?
    end

    # Add listener to subscribers
    #
    # @param [Array[#trigger]] observers
    #
    # @return [undefined]
    #
    # @api public
    def subscribe(*observers)
      synchronize do
        observers.each { |observer| @subscribers << observer }
      end
    end

    # Visit subscribers and notify
    #
    # @param [FiniteMachine::Event] event
    #
    # @return [undefined]
    #
    # @api public
    def visit(event)
      each { |subscriber| synchronize { event.notify subscriber } }
    end

    # Reset subscribers
    #
    # @return [self]
    #
    # @api public
    def reset
      @subscribers.clear
      self
    end
  end # Subscribers
end # FiniteMachine

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
finite_machine-0.10.2 lib/finite_machine/subscribers.rb
finite_machine-0.10.1 lib/finite_machine/subscribers.rb
finite_machine-0.10.0 lib/finite_machine/subscribers.rb
finite_machine-0.9.2 lib/finite_machine/subscribers.rb
finite_machine-0.9.1 lib/finite_machine/subscribers.rb
finite_machine-0.9.0 lib/finite_machine/subscribers.rb
finite_machine-0.8.1 lib/finite_machine/subscribers.rb
finite_machine-0.8.0 lib/finite_machine/subscribers.rb
finite_machine-0.7.1 lib/finite_machine/subscribers.rb
finite_machine-0.7.0 lib/finite_machine/subscribers.rb
finite_machine-0.6.1 lib/finite_machine/subscribers.rb
finite_machine-0.6.0 lib/finite_machine/subscribers.rb
finite_machine-0.5.0 lib/finite_machine/subscribers.rb