Class: IRC::Client::Dispatcher::EventDispatcher
- Defined in:
- lib/failirc/client/dispatcher/eventdispatcher.rb
Instance Attribute Summary collapse
-
#aliases ⇒ Object
readonly
Returns the value of attribute aliases.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#dispatcher ⇒ Object
readonly
Returns the value of attribute dispatcher.
-
#events ⇒ Object
readonly
Returns the value of attribute events.
-
#handling ⇒ Object
readonly
Returns the value of attribute handling.
Instance Method Summary collapse
- #alias(chain, symbol, regex) ⇒ Object
- #dispatch(chain, server, string, deep = false) ⇒ Object
- #execute(event, *args) ⇒ Object
- #handle(what, chain, deep, server) ⇒ Object
-
#initialize(dispatcher) ⇒ EventDispatcher
constructor
A new instance of EventDispatcher.
- #newAliases ⇒ Object
- #newEvents ⇒ Object
- #register(chain, type, callback, priority = 0) ⇒ Object
Constructor Details
#initialize(dispatcher) ⇒ EventDispatcher
Returns a new instance of EventDispatcher.
32 33 34 35 36 37 38 39 |
# File 'lib/failirc/client/dispatcher/eventdispatcher.rb', line 32 def initialize (dispatcher) @client = dispatcher.client @dispatcher = dispatcher @handling = ThreadSafeHash.new @aliases = newAliases @events = newEvents end |
Instance Attribute Details
#aliases ⇒ Object (readonly)
Returns the value of attribute aliases.
30 31 32 |
# File 'lib/failirc/client/dispatcher/eventdispatcher.rb', line 30 def aliases @aliases end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
30 31 32 |
# File 'lib/failirc/client/dispatcher/eventdispatcher.rb', line 30 def client @client end |
#dispatcher ⇒ Object (readonly)
Returns the value of attribute dispatcher.
30 31 32 |
# File 'lib/failirc/client/dispatcher/eventdispatcher.rb', line 30 def dispatcher @dispatcher end |
#events ⇒ Object (readonly)
Returns the value of attribute events.
30 31 32 |
# File 'lib/failirc/client/dispatcher/eventdispatcher.rb', line 30 def events @events end |
#handling ⇒ Object (readonly)
Returns the value of attribute handling.
30 31 32 |
# File 'lib/failirc/client/dispatcher/eventdispatcher.rb', line 30 def handling @handling end |
Instance Method Details
#alias(chain, symbol, regex) ⇒ Object
144 145 146 147 148 149 150 151 152 |
# File 'lib/failirc/client/dispatcher/eventdispatcher.rb', line 144 def alias (chain, symbol, regex) if !regex @aliases[chain].delete(symbol) elsif !regex.class == Regexp raise 'You have to alias to a Regexp.' else @aliases[chain][symbol] = regex end end |
#dispatch(chain, server, string, deep = false) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/failirc/client/dispatcher/eventdispatcher.rb', line 73 def dispatch (chain, server, string, deep=false) if !server return end handle(:start, chain, deep, server) event = Event.new(self, chain, server, string) result = string @events[:pre].each {|callback| event.special = :pre if callback.call(event, server, string) == false handle(:stop, chain, deep, server) return false end } if !event.types.empty? event.special = nil event.callbacks.each {|callback| begin if callback.call(server, string) == false handle(:stop, chain, deep, server) return false end rescue Exception => e self.debug e end } elsif chain == :input @events[:default].each {|callback| event.special = :default if callback.call(event, server, string) == false handle(:stop, chain, deep, server) return false end } end @events[:post].each {|callback| event.special = :post if callback.call(event, server, string) == false handle(:stop, chain, deep, server) return false end } handle(:stop, chain, deep, server) return result end |
#execute(event, *args) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/failirc/client/dispatcher/eventdispatcher.rb', line 130 def execute (event, *args) if @events[:custom][event] @events[:custom][event].each {|callback| begin if callback.method.call(*args) == false return false end rescue Exception => e self.debug e end } end end |
#handle(what, chain, deep, server) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/failirc/client/dispatcher/eventdispatcher.rb', line 61 def handle (what, chain, deep, server) if chain != :input || deep return end if what == :start @handling[server.socket] = true else @handling.delete(server.socket) end end |
#newAliases ⇒ Object
41 42 43 44 45 46 |
# File 'lib/failirc/client/dispatcher/eventdispatcher.rb', line 41 def newAliases Hash[ :input => {}, :output => {}, ] end |
#newEvents ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/failirc/client/dispatcher/eventdispatcher.rb', line 48 def newEvents Hash[ :pre => [], :post => [], :default => [], :custom => {}, :input => {}, :output => {} ] end |
#register(chain, type, callback, priority = 0) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/failirc/client/dispatcher/eventdispatcher.rb', line 154 def register (chain, type, callback, priority=0) if !type events = @events[chain] if !events events = @events[chain] = [] end else if @aliases[chain] if @aliases[chain][type] type = @aliases[chain][type] end end if !@events[chain] @events[chain] = {} end events = @events[chain][type] if !events events = @events[chain][type] = [] end end if !callback events.clear elsif callback.is_a?(Array) callback.each {|callback| register(chain, type, callback) } else if callback.is_a?(Event::Callback) events.push(callback) else events.push(Event::Callback.new(callback, priority)) end end events.sort! {|a, b| a.priority <=> b.priority } end |