Sha256: b465f8ff789f122fa1f2e6ec4252ada94c47f7deff601824b93e728bc1437467

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 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)

        event = events[e.type]
        event.call(e) if event
      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

1 entries across 1 versions & 1 rubygems

Version Path
thinreports-0.8.2 lib/thinreports/core/events.rb