Class: Vedeu::Events::Trigger Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *args) ⇒ Vedeu::Events::Trigger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Vedeu::Events::Trigger.

Parameters:

  • name (NilClass|Symbol|String)

    The name of the model or target model to act upon. May default to ‘Vedeu.focus`.

  • args (Array)

    Any arguments the event needs to execute correctly.



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

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

Instance Attribute Details

#argsArray (readonly, protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Array)


62
63
64
# File 'lib/vedeu/events/trigger.rb', line 62

def args
  @args
end

#nameSymbol|String (readonly, protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Symbol|String)


58
59
60
# File 'lib/vedeu/events/trigger.rb', line 58

def name
  @name
end

#repositoryVedeu::Repositories::Repository (readonly, protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



66
67
68
# File 'lib/vedeu/events/trigger.rb', line 66

def repository
  @repository
end

Class Method Details

.trigger(name, *args) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Trigger an event by name.

Examples:

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

Parameters:

  • name (NilClass|Symbol|String)

    The name of the model or target model to act upon. May default to ‘Vedeu.focus`.

  • args (Array)

    Any arguments the event needs to execute correctly.

Returns:

  • (Array)


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

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

Instance Method Details

#messageString (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String)


71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/vedeu/events/trigger.rb', line 71

def message
  if args.size > 1
    "Triggering: '#{name.inspect}' with #{args.inspect}"

  elsif args.one?
    "Triggering: '#{name.inspect}' for #{args.first.inspect}"

  else
    "Triggering: '#{name.inspect}'"

  end
end

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return all of the registered events for this name.

Returns:



102
103
104
105
106
107
108
109
110
# File 'lib/vedeu/events/trigger.rb', line 102

def registered_events
  return repository.find(name) if repository.registered?(name)

  Vedeu::Events::Aliases.find(name).map do |event_name|
    Vedeu::Events::Trigger.trigger(event_name, *args)
  end

  []
end

#resultsArray<void>|void (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Trigger each registered event for this name.

Returns:

  • (Array<void>|void)


87
88
89
90
91
92
93
94
95
96
97
# File 'lib/vedeu/events/trigger.rb', line 87

def results
  @results ||= registered_events.map do |event|
    Vedeu.outdent do
      Vedeu.log(type: :event, message: message)

      Vedeu.indent do
        event.trigger(*args)
      end
    end
  end
end

#triggerArray

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Trigger the event and return the result or an array of results.

Returns:

  • (Array)


43
44
45
46
47
48
49
50
51
52
# File 'lib/vedeu/events/trigger.rb', line 43

def trigger
  if Vedeu.config.debug? && results.empty?
    Vedeu.log(type:    :nonevent,
              message: "No action for: '#{name.inspect}'")
  end

  return results[0] if results.one?

  results
end