Class: MijDiscord::Events::DispatcherBase

Inherits:
Object
  • Object
show all
Defined in:
lib/mij-discord/events.rb

Direct Known Subclasses

EventDispatcher

Defined Under Namespace

Classes: Callback

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ DispatcherBase

Returns a new instance of DispatcherBase.

Raises:

  • (ArgumentError)


102
103
104
105
106
# File 'lib/mij-discord/events.rb', line 102

def initialize(klass)
  raise ArgumentError, 'Class must inherit from EventBase' unless klass < EventBase

  @klass, @callbacks = klass, {}
end

Instance Method Details

#add_callback(key = nil, **filter, &block) ⇒ Object

Raises:

  • (ArgumentError)


108
109
110
111
112
113
114
# File 'lib/mij-discord/events.rb', line 108

def add_callback(key = nil, **filter, &block)
  raise ArgumentError, 'No callback block provided' if block.nil?

  key = block.object_id if key.nil?
  @callbacks[key] = Callback.new(key, block, filter)
  key
end

#callbacksObject



121
122
123
# File 'lib/mij-discord/events.rb', line 121

def callbacks
  @callbacks.values
end

#remove_callback(key) ⇒ Object



116
117
118
119
# File 'lib/mij-discord/events.rb', line 116

def remove_callback(key)
  @callbacks.delete(key)
  nil
end

#trigger(event_args, block_args = nil) ⇒ Object Also known as: raise



125
126
127
128
129
130
131
# File 'lib/mij-discord/events.rb', line 125

def trigger(event_args, block_args = nil)
  event = @klass.new(*event_args)

  @callbacks.each do |_, cb|
    execute_callback(cb, event, block_args) if event.trigger?(cb.filter)
  end
end