Class | Irc::Bot::MessageParameter |
In: |
lib/rbot/messagemapper.rb
|
Parent: | Object |
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.
default | [RW] | |
multi | [W] | |
name | [R] | |
optional | [W] |
# 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
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.
# 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.
# 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
# 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