Class: Irc::Bot::MessageParameter
Overview
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.
Instance Attribute Summary collapse
-
#default ⇒ Object
Returns the value of attribute default.
-
#multi ⇒ Object
writeonly
Sets the attribute multi.
-
#name ⇒ Object
Returns the value of attribute name.
-
#optional ⇒ Object
writeonly
Sets the attribute optional.
Instance Method Summary collapse
-
#collect(val) ⇒ Object
This method is used to turn a matched item into the actual parameter value.
-
#collector=(val) ⇒ Object
This method allow the plugin programmer to choose to only pick a subset of the string matched by a parameter.
-
#initialize(name) ⇒ MessageParameter
constructor
A new instance of MessageParameter.
- #inspect ⇒ Object
- #multi? ⇒ Boolean
- #optional? ⇒ Boolean
Constructor Details
#initialize(name) ⇒ MessageParameter
Returns a new instance of MessageParameter.
314 315 316 317 318 319 320 321 |
# File 'lib/rbot/messagemapper.rb', line 314 def initialize(name) self.name = name @multi = false @optional = false @default = nil @regexp = nil @index = nil end |
Instance Attribute Details
#default ⇒ Object
Returns the value of attribute default.
312 313 314 |
# File 'lib/rbot/messagemapper.rb', line 312 def default @default end |
#multi=(value) ⇒ Object (writeonly)
Sets the attribute multi
310 311 312 |
# File 'lib/rbot/messagemapper.rb', line 310 def multi=(value) @multi = value end |
#name ⇒ Object
Returns the value of attribute name.
309 310 311 |
# File 'lib/rbot/messagemapper.rb', line 309 def name @name end |
#optional=(value) ⇒ Object (writeonly)
Sets the attribute optional
311 312 313 |
# File 'lib/rbot/messagemapper.rb', line 311 def optional=(value) @optional = value end |
Instance Method Details
#collect(val) ⇒ Object
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.
340 341 342 343 344 345 346 347 348 |
# File 'lib/rbot/messagemapper.rb', line 340 def collect(val) return val unless @regexp mdata = @regexp.match(val) if @index return mdata[@index] else return mdata[1..-1].compact.first end end |
#collector=(val) ⇒ Object
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.
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 |
# File 'lib/rbot/messagemapper.rb', line 364 def collector=(val) return unless val case val when Regexp return unless val.has_captures? @regexp = val when Array warning "Collector #{val.inspect} is too long, ignoring extra entries" unless val.length <= 2 @regexp = val[0] @index = val[1] rescue nil when Hash raise "Collector #{val.inspect} doesn't have a :regexp key" unless val.has_key?(:regexp) @regexp = val[:regexp] @index = val.fetch(:regexp, nil) end raise "The regexp of collector #{val.inspect} isn't a Regexp" unless @regexp.kind_of?(Regexp) raise "The index of collector #{val.inspect} is present but not an integer " if @index and not @index.kind_of?(Fixnum) end |
#inspect ⇒ Object
383 384 385 386 387 388 389 390 391 392 |
# File 'lib/rbot/messagemapper.rb', line 383 def inspect mul = multi? ? " multi" : " single" opt = optional? ? " optional" : " needed" if @regexp reg = " regexp=%s index=%s" % [@regexp, @index] else reg = nil end "<%s %s%s%s%s>" % [self.class, name, mul, opt, reg] end |
#multi? ⇒ Boolean
327 328 329 |
# File 'lib/rbot/messagemapper.rb', line 327 def multi? @multi end |
#optional? ⇒ Boolean
331 332 333 |
# File 'lib/rbot/messagemapper.rb', line 331 def optional? @optional end |