Permalink
Browse files

Fix some issues

  • Loading branch information...
meh committed Feb 23, 2012
1 parent cdb9329 commit ddf2e83713be9f8ecb16d1cc60804fa77838d253
@@ -28,98 +28,98 @@
module IRC
class Events
- attr_reader :server, :aliases, :chains
-
- def initialize (server)
- @server = server
-
- DSL.initialize(self)
-
- @hooks = []
- end
-
- def hook (mod)
- @hooks << mod
- end
-
- def alias (chain, name, value = nil)
- if value
- @aliases[chain][name.to_sym.downcase] = value
- else
- @aliases[chain][name.to_sym.downcase] || @hooks.find {|hook|
- hook.aliases[chain][name]
- }
- end
- end
-
- # WARNING: brace for legacy shitty code, I don't even understand it anymore.
- def event (chain, what)
- if what.is_a?(Symbol)
- Event.new(self, chain, (@chains[chain][what] + @hooks.map {|hook| hook.chains[chain][what]}).flatten.compact, [what])
- else
- callbacks = Hash.new {|hash, key| hash[key] = [] }
-
- (@hooks + [self]).each {|hook|
- hook.chains[chain].each {|key, value|
- callbacks[key].insert(-1, *value)
- }
- }
-
- regexps, callbacks = callbacks.to_a.select {|(name, callbacks)|
- !name.is_a?(Symbol)
- }.select {|(regexp, callbacks)|
- what.to_s.match(regexp) rescue false
- }.transpose
-
- aliases = (regexps || []).flatten.compact.map {|regexp|
- @aliases[chain].select {|(name, value)|
- regexp == value
- }.map {|(name, value)|
- name
- } + @hooks.map {|hook|
- hook.aliases[chain].to_a.select {|(name, value)|
- regexp == value
- }.map {|(name, value)|
- name
- }
- }
- }.flatten.compact.uniq
-
- Event.new(self, chain, (callbacks || []).flatten.compact, aliases)
- end
- end
-
- def dispatch (chain=:input, thing, string)
- return unless thing
-
- current = event(chain, string).on(thing, string)
-
- catch(:halt) {
- event(chain, :before).call(current, thing, string)
-
- unless current.callbacks.empty?
- current.call
- else
- event(chain, :default).call(current, thing, string)
- end
-
- event(chain, :after).call(current, thing, string)
- }
- end
-
- def register (chain, what, options={}, &block)
- @chains[chain][what] << Callback.new(options, &block)
- end
-
- def observe (what, options={}, &block)
- @custom[what] << Callback.new(options, &block)
- end
-
- def fire (what, *args, &block)
- catch(:halt) {
- Event.new(self, :custom, (@custom[what] + @hooks.map {|hook| hook.custom[what] }).flatten.compact).call(*args, &block)
- }
- end
+ attr_reader :server, :aliases, :chains
+
+ def initialize (server)
+ @server = server
+
+ DSL.initialize(self)
+
+ @hooks = []
+ end
+
+ def hook (mod)
+ @hooks << mod
+ end
+
+ def alias (chain, name, value = nil)
+ if value
+ @aliases[chain][name.to_sym.downcase] = value
+ else
+ @aliases[chain][name.to_sym.downcase] || @hooks.find {|hook|
+ hook.aliases[chain][name]
+ }
+ end
+ end
+
+ # WARNING: brace for legacy shitty code, I don't even understand it anymore.
+ def event (chain, what)
+ if what.is_a?(Symbol)
+ Event.new(self, chain, (@chains[chain][what] + @hooks.map {|hook| hook.chains[chain][what]}).flatten.compact, [what])
+ else
+ callbacks = Hash.new { |h, k| h[k] = [] }
+
+ (@hooks + [self]).each {|hook|
+ hook.chains[chain].each {|key, value|
+ callbacks[key].insert(-1, *value)
+ }
+ }
+
+ regexps, callbacks = callbacks.to_a.select {|(name, callbacks)|
+ !name.is_a?(Symbol)
+ }.select {|(regexp, callbacks)|
+ what.to_s.match(regexp) rescue false
+ }.transpose
+
+ aliases = (regexps || []).flatten.compact.map {|regexp|
+ @aliases[chain].select {|(name, value)|
+ regexp == value
+ }.map {|(name, value)|
+ name
+ } + @hooks.map {|hook|
+ hook.aliases[chain].to_a.select {|(name, value)|
+ regexp == value
+ }.map {|(name, value)|
+ name
+ }
+ }
+ }.flatten.compact.uniq
+
+ Event.new(self, chain, [callbacks].flatten.compact, aliases)
+ end
+ end
+
+ def dispatch (chain, thing, string)
+ return unless thing
+
+ current = event(chain, string).on(thing, string)
+
+ catch(:halt) {
+ event(chain, :before).call(current, thing, string)
+
+ unless current.callbacks.empty?
+ current.call
+ else
+ event(chain, :default).call(current, thing, string)
+ end
+
+ event(chain, :after).call(current, thing, string)
+ }
+ end
+
+ def register (chain, what, options={}, &block)
+ @chains[chain][what] << Callback.new(options, &block)
+ end
+
+ def observe (what, options={}, &block)
+ @custom[what] << Callback.new(options, &block)
+ end
+
+ def fire (what, *args, &block)
+ catch(:halt) {
+ Event.new(self, :custom, (@custom[what] + @hooks.map {|hook| hook.custom[what] }).flatten.compact).call(*args, &block)
+ }
+ end
end
end
@@ -20,86 +20,86 @@
module IRC; class Events
module DSL
- attr_reader :aliases, :chains, :custom
+ attr_reader :aliases, :chains, :custom
- def self.initialize (what)
- what.instance_eval do
- @aliases = Aliases.new
- @chains = Chains.new
- @custom = Custom.new
- end
- end
+ def self.initialize (what)
+ what.instance_eval do
+ @aliases = Aliases.new
+ @chains = Chains.new
+ @custom = Custom.new
+ end
+ end
- def input (&block)
- tmp, @into = @into, :input
+ def input (&block)
+ tmp, @into = @into, :input
- self.instance_eval(&block)
+ self.instance_eval(&block)
- @into = tmp
- end
+ @into = tmp
+ end
- def output (&block)
- tmp, @into = @into, :output
+ def output (&block)
+ tmp, @into = @into, :output
- self.instance_eval(&block)
+ self.instance_eval(&block)
- @into = tmp
- end
+ @into = tmp
+ end
- def aliases (&block)
- return @aliases unless @into
+ def aliases (&block)
+ return @aliases unless @into
- on = InsensitiveStruct.new
- on.instance_eval(&block)
+ on = InsensitiveStruct.new
+ on.instance_eval(&block)
- on.to_hash.each {|name, value|
- @aliases[@into][name] = value
- }
- end
+ on.to_hash.each {|name, value|
+ @aliases[@into][name] = value
+ }
+ end
- def default (options = {}, &block)
- return unless @into
+ def default (options = {}, &block)
+ return unless @into
- @chains[@into][:default] << Callback.new(options, &block)
- end
+ @chains[@into][:default] << Callback.new(options, &block)
+ end
- def before (options = {}, &block)
- return unless @into
+ def before (options = {}, &block)
+ return unless @into
- @chains[@into][:before] << Callback.new(options, &block)
- end
+ @chains[@into][:before] << Callback.new(options, &block)
+ end
- def after (options = {}, &block)
- return unless @into
+ def after (options = {}, &block)
+ return unless @into
- @chains[@into][:after] << Callback.new(options, &block)
- end
+ @chains[@into][:after] << Callback.new(options, &block)
+ end
- def on (what, options = {}, &block)
- if @into
- @chains[@into][@aliases[@into][what] || what] << Callback.new(options, &block)
- else
- observe(what, options, &block)
- end
- end
+ def on (what, options = {}, &block)
+ if @into
+ @chains[@into][@aliases[@into][what] || what] << Callback.new(options, &block)
+ else
+ observe(what, options, &block)
+ end
+ end
- def observe (what, options = {}, &block)
- @custom[what] << Callback.new(options, &block)
- end
+ def observe (what, options = {}, &block)
+ @custom[what] << Callback.new(options, &block)
+ end
- def fire (what, *args, &block)
- if @owner
- @owner.fire(what, *args, &block)
- else
- catch(:halt) {
- Event.new(self, :custom, @custom[what]).call(*args, &block)
- }
- end
- end
+ def fire (what, *args, &block)
+ if @owner
+ @owner.fire(what, *args, &block)
+ else
+ catch(:halt) {
+ Event.new(self, :custom, @custom[what]).call(*args, &block)
+ }
+ end
+ end
- def skip
- throw :halt
- end
+ def skip
+ throw :halt
+ end
end
end; end
Oops, something went wrong.

0 comments on commit ddf2e83

Please sign in to comment.