Class: IRC::Client::Dispatcher::EventDispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/failirc/client/dispatcher/eventdispatcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#aliasesObject (readonly)

Returns the value of attribute aliases.



30
31
32
# File 'lib/failirc/client/dispatcher/eventdispatcher.rb', line 30

def aliases
  @aliases
end

#clientObject (readonly)

Returns the value of attribute client.



30
31
32
# File 'lib/failirc/client/dispatcher/eventdispatcher.rb', line 30

def client
  @client
end

#dispatcherObject (readonly)

Returns the value of attribute dispatcher.



30
31
32
# File 'lib/failirc/client/dispatcher/eventdispatcher.rb', line 30

def dispatcher
  @dispatcher
end

#eventsObject (readonly)

Returns the value of attribute events.



30
31
32
# File 'lib/failirc/client/dispatcher/eventdispatcher.rb', line 30

def events
  @events
end

#handlingObject (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

#newAliasesObject



41
42
43
44
45
46
# File 'lib/failirc/client/dispatcher/eventdispatcher.rb', line 41

def newAliases
    Hash[
        :input  => {},
        :output => {},
    ]
end

#newEventsObject



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