Class Irc::Bot::MessageParameter
In: lib/rbot/messagemapper.rb
Parent: Object
BasicUserMessage JoinMessage NamesMessage WhoisMessage ModeChangeMessage KickMessage MotdMessage QuitMessage BanlistMessage UserMessage NoSuchTargetMessage TopicMessage NickMessage WelcomeMessage UnknownMessage InviteMessage PartMessage NetmaskList UserList ArrayOf ChannelList Netmask User\n[lib/rbot/botuser.rb\nlib/rbot/irc.rb] Channel Singleton RfcCasemap StrictRfcCasemap AsciiCasemap Casemap PrivMessage NoticeMessage TokyoCabinet::BDB CIBDB Btree CIBtree Socket MessageQueue QueueRing Client DBHash\n[lib/rbot/registry/bdb.rb\nlib/rbot/registry/tc.rb] DBTree\n[lib/rbot/registry/bdb.rb\nlib/rbot/registry/tc.rb] Server NetmaskDb Bot\n[lib/rbot/botuser.rb\nlib/rbot/config.rb\nlib/rbot/ircbot.rb\nlib/rbot/language.rb\nlib/rbot/message.rb\nlib/rbot/messagemapper.rb\nlib/rbot/plugins.rb\nlib/rbot/rbotconfig.rb\nlib/rbot/registry/bdb.rb\nlib/rbot/registry/tc.rb] lib/rbot/ircsocket.rb lib/rbot/rfc2812.rb lib/rbot/registry/tc.rb lib/rbot/irc.rb lib/rbot/maskdb.rb lib/rbot/message.rb lib/rbot/messagemapper.rb lib/rbot/botuser.rb lib/rbot/registry/tc.rb (null) BotConfig PKGConfig ServerOrCasemap Irc dot/m_35_0.png

MessageParameter is a class that collects all the necessary information about a message (dynamic) parameter (the :param or *param that can be found in a map).

It has a name attribute, multi and optional booleans that tell if the parameter collects more than one word, and if it‘s optional (respectively). In the latter case, it can also have a default value.

It is possible to assign a collector to a MessageParameter. This can be either a Regexp with captures or an Array or a Hash. The collector defines what the collect() method is supposed to return.

Methods

collect   collector=   inspect   multi?   name=   new   optional?  

Attributes

default  [RW] 
multi  [W] 
name  [R] 
optional  [W] 

Public Class methods

[Source]

     # File lib/rbot/messagemapper.rb, line 314
314:     def initialize(name)
315:       self.name = name
316:       @multi = false
317:       @optional = false
318:       @default = nil
319:       @regexp = nil
320:       @index = nil
321:     end

Public Instance methods

This method is used to turn a matched item into the actual parameter value. It only does something when collector= set the @regexp to something. In this case, val is matched against @regexp and then the match result specified in @index is selected. As a special case, when @index is nil the first non-nil captured group is returned.

[Source]

     # File lib/rbot/messagemapper.rb, line 340
340:     def collect(val)
341:       return val unless @regexp
342:       mdata = @regexp.match(val)
343:       if @index
344:         return mdata[@index]
345:       else
346:         return mdata[1..-1].compact.first
347:       end
348:     end

This method allow the plugin programmer to choose to only pick a subset of the string matched by a parameter. This is done by passing the collector=() method either a Regexp with captures or an Array or a Hash.

When the method is passed a Regexp with captures, the collect() method will return the first non-nil captured group.

When the method is passed an Array, it will grab a regexp from the first element, and possibly an index from the second element. The index can also be nil.

When the method is passed a Hash, it will grab a regexp from the :regexp element, and possibly an index from the :index element. The index can also be nil.

[Source]

     # File lib/rbot/messagemapper.rb, line 364
364:     def collector=(val)
365:       return unless val
366:       case val
367:       when Regexp
368:         return unless val.has_captures?
369:         @regexp = val
370:       when Array
371:         warning "Collector #{val.inspect} is too long, ignoring extra entries" unless val.length <= 2
372:         @regexp = val[0]
373:         @index = val[1] rescue nil
374:       when Hash
375:         raise "Collector #{val.inspect} doesn't have a :regexp key" unless val.has_key?(:regexp)
376:         @regexp = val[:regexp]
377:         @index = val.fetch(:regexp, nil)
378:       end
379:       raise "The regexp of collector #{val.inspect} isn't a Regexp" unless @regexp.kind_of?(Regexp)
380:       raise "The index of collector #{val.inspect} is present but not an integer " if @index and not @index.kind_of?(Fixnum)
381:     end

[Source]

     # File lib/rbot/messagemapper.rb, line 383
383:     def inspect
384:       mul = multi? ? " multi" : " single"
385:       opt = optional? ? " optional" : " needed"
386:       if @regexp
387:         reg = " regexp=%s index=%s" % [@regexp, @index]
388:       else
389:         reg = nil
390:       end
391:       "<%s %s%s%s%s>" % [self.class, name, mul, opt, reg]
392:     end

[Source]

     # File lib/rbot/messagemapper.rb, line 327
327:     def multi?
328:       @multi
329:     end

[Source]

     # File lib/rbot/messagemapper.rb, line 323
323:     def name=(val)
324:       @name = val.to_sym
325:     end

[Source]

     # File lib/rbot/messagemapper.rb, line 331
331:     def optional?
332:       @optional
333:     end

[Validate]