Class: Vedeu::Trigger

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/events/trigger.rb

Overview

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.

Examples:

Vedeu.trigger(:my_event, :oxidize, 'nitrogen')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *args) ⇒ Trigger

See Also:



23
24
25
26
27
# File 'lib/vedeu/events/trigger.rb', line 23

def initialize(name, *args)
  @name = name
  @args = args
  @repository = Vedeu.events
end

Instance Attribute Details

#argsObject (readonly, private)

Returns the value of attribute args.



43
44
45
# File 'lib/vedeu/events/trigger.rb', line 43

def args
  @args
end

#nameObject (readonly, private)

Returns the value of attribute name.



43
44
45
# File 'lib/vedeu/events/trigger.rb', line 43

def name
  @name
end

#repositoryObject (readonly, private)

Returns the value of attribute repository.



43
44
45
# File 'lib/vedeu/events/trigger.rb', line 43

def repository
  @repository
end

Class Method Details

.trigger(name, *args) ⇒ Array

Parameters:

  • name (Symbol)

    The name of the event you wish to trigger. The event does not have to exist.

  • args (Array)

    Any arguments the event needs to execute correctly.

Returns:

  • (Array)


17
18
19
# File 'lib/vedeu/events/trigger.rb', line 17

def self.trigger(name, *args)
  new(name, *args).trigger
end

Instance Method Details

#registered_eventsArray|Array<Vedeu::Event> (private)

Returns:



53
54
55
56
57
# File 'lib/vedeu/events/trigger.rb', line 53

def registered_events
  return [] unless repository.registered?(name)

  repository.find(name)
end

#resultsArray<void>|void (private)

Returns:

  • (Array<void>|void)


48
49
50
# File 'lib/vedeu/events/trigger.rb', line 48

def results
  @results ||= registered_events.map { |event| event.trigger(*args) }
end

#triggerArray

Returns:

  • (Array)

See Also:



31
32
33
34
35
36
37
38
39
# File 'lib/vedeu/events/trigger.rb', line 31

def trigger
  if results.one?
    results.first

  else
    results

  end
end