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)


98
99
100
101
102
# File 'lib/mij-discord/events.rb', line 98

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)


104
105
106
107
108
109
110
# File 'lib/mij-discord/events.rb', line 104

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



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

def callbacks
  @callbacks.values
end

#remove_callback(key) ⇒ Object



112
113
114
115
# File 'lib/mij-discord/events.rb', line 112

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

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



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

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