Sha256: 9e59913356fc875743856d0eba61cfee53a8c10d3a663539f9992ddfa45e2413

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

# coding: utf-8

module Thinreports
  module Core

    class Events
      include Utils

      Event = ::Struct.new(:type, :target)

      # @return [Hash<Symbol, Thinreports::Core::Events::Event>]
      attr_accessor :events

      # @param [Symbol] types
      def initialize(*types)
        @events = {}
        @types  = types
      end

      # @param [Symbol] type
      # @yield [e]
      # @yieldparam [Thinreports::Core::Events::Event] e
      def listen(type, &block)
        verify_event_type(type)

        if block_given?
          events[type] = block
        else
          raise ArgumentError, '#listen requires a block'
        end
      end
      alias_method :on, :listen

      # @param [Symbol] type
      def unlisten(type)
        events.delete(type)
      end
      alias_method :un, :unlisten

      def dispatch(e)
        unless e.type
          raise ArgumentError, 'Event#type attribute is required'
        end
        verify_event_type(e.type)

        if event = events[e.type]
          event.call(e)
        end
      end

      def copy
        new_events = self.dup
        new_events.events = deep_copy(events)
        new_events
      end

    private

      # @param [Symbol] type
      def verify_event_type(type)
        return if @types.empty?

        unless @types.include?(type)
          raise Thinreports::Errors::UnknownEventType
        end
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
thinreports-0.8.1 lib/thinreports/core/events.rb
thinreports-0.8.0 lib/thinreports/core/events.rb