summaryrefslogtreecommitdiff
path: root/lib/rbot
diff options
context:
space:
mode:
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2008-03-13 09:03:01 +0100
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2008-03-13 09:03:01 +0100
commit1bc0a152a6159ff235c9fb7e872f9e80604883da (patch)
tree5e4e6c058b08f29772040a5e4312708688b52a57 /lib/rbot
parent41e46ac67664f3bbec427d4e332a89783073e856 (diff)
filters: @bot.filter can now be called with a list of filters and with any form of datastream initialization
Diffstat (limited to 'lib/rbot')
-rw-r--r--lib/rbot/core/utils/filters.rb36
1 files changed, 29 insertions, 7 deletions
diff --git a/lib/rbot/core/utils/filters.rb b/lib/rbot/core/utils/filters.rb
index fbd20333..6a4434cc 100644
--- a/lib/rbot/core/utils/filters.rb
+++ b/lib/rbot/core/utils/filters.rb
@@ -51,15 +51,37 @@ module ::Irc
alias :filter :call
end
- # This method processes the DataStream _stream_ with the filters _name_.
- # _name_ can be either a single Symbol (filter name), or an Array of
- # Symbols, in which case the output of each filter will be used as input
- # for the next
+ # call-seq:
+ # filter(filter1, filter2, ..., filterN, stream) -> stream
+ # filter(filter1, filter2, ..., filterN, text, hash) -> stream
+ # filter(filter1, filter2, ..., filterN, hash) -> stream
#
- def filter(name, stream={})
+ # This method processes the DataStream _stream_ with the filters <i>filter1</i>,
+ # <i>filter2</i>, ..., _filterN_, in sequence (the output of each filter is used
+ # as input for the next one.
+ # _stream_ can be provided either as a DataStream or as a String and a Hash
+ # (see DataStream.new).
+ #
+ def filter(*args)
@filters ||= {}
- names = (Symbol === name ? [name] : name.dup)
- ds = (DataStream === stream ? stream : DataStream.new(stream))
+ case args.last
+ when DataStream
+ # the stream is an actual DataStream
+ ds = args.pop
+ when String
+ # the stream is just plain text
+ ds = DataStream.new(args.pop)
+ when Hash
+ # the stream is a Hash, check if the previous element is a String
+ if String === args[-2]
+ ds = DataStream.new(*args.slice!(-2, 2))
+ else
+ ds = DataStream.new(args.pop)
+ end
+ else
+ raise "Unknown DataStream class #{args.last.class}"
+ end
+ names = args.dup
return ds if names.empty?
# check if filters exist
missing = names - @filters.keys