Sha256: 78c1b57f0394f884f5196a6ddf5733b27fc6dca107ecda4b06e06c5a66eac924

Contents?: true

Size: 774 Bytes

Versions: 7

Compression:

Stored size: 774 Bytes

Contents

module Larynx
  module Observable

    def add_observer(object)
      @observers ||= []
      @observers << object
    end

    def remove_observer(object)
      @observers && @observers.delete(object)
    end

    def clear_observers!
      @observers = []
    end

    # Like an observer stack which only notifies top observer
    def notify_current_observer(event, data=nil)
      return unless @observers
      obs = @observers.last
      if obs.respond_to?(event)
        data ? obs.send(event, data) : obs.send(event)
      end
    end

    def notify_observers(event, data=nil)
      return unless @observers
      @observers.each do |obs|
        next unless obs.respond_to?(event)
        data ? obs.send(event, data) : obs.send(event)
      end
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
larynx-0.1.6 lib/larynx/observable.rb
larynx-0.1.5 lib/larynx/observable.rb
larynx-0.1.4 lib/larynx/observable.rb
larynx-0.1.3 lib/larynx/observable.rb
larynx-0.1.2 lib/larynx/observable.rb
larynx-0.1.1 lib/larynx/observable.rb
larynx-0.1.0 lib/larynx/observable.rb