Sha256: 8b42be32f5735bd08728f785cb7367052d8aa9aef36143a06246d9c71ae386d1

Contents?: true

Size: 1.23 KB

Versions: 3

Compression:

Stored size: 1.23 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 { |event| event.trigger(*args) }

      return results.first if results.one?

      results
    end

    private

    attr_reader :name,
      :args,
      :repository

  end # Trigger

end # Vedeu

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vedeu-0.3.4 lib/vedeu/events/trigger.rb
vedeu-0.3.3 lib/vedeu/events/trigger.rb
vedeu-0.3.2 lib/vedeu/events/trigger.rb