Class: MijDiscord::Events::DispatcherBase
- Inherits:
-
Object
- Object
- MijDiscord::Events::DispatcherBase
- Defined in:
- lib/mij-discord/events.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Callback
Instance Method Summary collapse
- #add_callback(key = nil, **filter, &block) ⇒ Object
- #callbacks ⇒ Object
-
#initialize(klass) ⇒ DispatcherBase
constructor
A new instance of DispatcherBase.
- #remove_callback(key) ⇒ Object
- #trigger(event_args, block_args = nil) ⇒ Object (also: #raise)
Constructor Details
#initialize(klass) ⇒ DispatcherBase
Returns a new instance of DispatcherBase.
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
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 |
#callbacks ⇒ Object
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 |