Sha256: b43f8487528dec187db2708748cb527f78dc7e7d41953593b6b651b1fd4f7bcc

Contents?: true

Size: 792 Bytes

Versions: 3

Compression:

Stored size: 792 Bytes

Contents

module QED

  class Advice

    # Event advice: Before, After and Upon.
    #
    class Events

      #
      attr :signals

      #
      def initialize
        @signals = [{}]
      end

      #
      def add(type, &procedure)
        @signals.last[type.to_sym] = procedure
      end

      #
      def call(type, *args)
        @signals.each do |set|
          proc = set[type.to_sym]
          proc.call(*args) if proc
        end
      end

      # Clear last set of advice.
      def reset
        @signals.pop
      end

      #
      def setup
        @signals.push {}
      end

      # Clear advice.
      def clear(type=nil)
        if type
          @signals.each{ |set| set.delete(type.to_sym) }
        else
          @signals = [{}]
        end
      end

    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
qed-2.1.1 lib/qed/advice/events.rb
qed-2.0.0 lib/qed/advice/events.rb
qed-2.1.0 lib/qed/advice/events.rb