Sha256: 29f86c79cd83c6bc4ecc3cbffd0a58a911c656cd3eba9f1cbfc7b200b53e305c

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

module Vedeu

  # Trigger a registered or system event by name with arguments. If the
  # event stored returns a value, that is returned. If multiple events are
  # registered for a name, then the result of each event will be returned as
  # part of a collection.
  #
  # @example
  #   Vedeu.trigger(:my_event, :oxidize, 'nitrogen')
  #
  # @api private
  class Trigger

    class << self

      # @param name [Symbol] The name of the event you wish to trigger. The event
      #   does not have to exist.
      # @param args [Array] Any arguments the event needs to execute correctly.
      # @return [Array]
      def trigger(name, *args)
        new(name, *args).trigger
      end

    end

    # @see Vedeu::Trigger.trigger
    # @return [Trigger]
    def initialize(name, *args)
      @name = name
      @args = args
      @repository = Vedeu.events
    end

    # @see Vedeu::Trigger.trigger
    # @return [Array]
    def trigger
      return [] unless repository.registered?(name)

      collection = repository.find(name)

      results = collection.map do |event|
        event.trigger(*args)
      end

      return results.first if results.one?

      results
    end

    private

    attr_reader :name,
      :args,
      :repository

  end # Trigger

end # Vedeu

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vedeu-0.3.1 lib/vedeu/events/trigger.rb
vedeu-0.3.0 lib/vedeu/events/trigger.rb