Class: Irc::Bot::MessageParameter
- Inherits:
-
Object
- Object
- Irc::Bot::MessageParameter
- Defined in:
- /home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb
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)
-
- (Object) default
Returns the value of attribute default.
-
- (Object) multi
writeonly
Sets the attribute multi.
-
- (Object) name
Returns the value of attribute name.
-
- (Object) optional
writeonly
Sets the attribute optional.
Instance Method Summary (collapse)
-
- (Object) collect(val)
This method is used to turn a matched item into the actual parameter value.
-
- (Object) collector=(val)
This method allow the plugin programmer to choose to only pick a subset of the string matched by a parameter.
-
- (MessageParameter) initialize(name)
constructor
A new instance of MessageParameter.
- - (Object) inspect
- - (Boolean) multi?
- - (Boolean) optional?
Constructor Details
- (MessageParameter) initialize(name)
Returns a new instance of MessageParameter
324 325 326 327 328 329 330 331 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb', line 324 def initialize(name) self.name = name @multi = false @optional = false @default = nil @regexp = nil @index = nil end |
Instance Attribute Details
- (Object) default
Returns the value of attribute default
322 323 324 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb', line 322 def default @default end |
- (Object) multi=(value) (writeonly)
Sets the attribute multi
320 321 322 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb', line 320 def multi=(value) @multi = value end |
- (Object) name
Returns the value of attribute name
319 320 321 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb', line 319 def name @name end |
- (Object) optional=(value) (writeonly)
Sets the attribute optional
321 322 323 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb', line 321 def optional=(value) @optional = value end |
Instance Method Details
- (Object) collect(val)
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.
350 351 352 353 354 355 356 357 358 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb', line 350 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 |
- (Object) collector=(val)
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.
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb', line 374 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 |
- (Object) inspect
393 394 395 396 397 398 399 400 401 402 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb', line 393 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 |
- (Boolean) multi?
337 338 339 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb', line 337 def multi? @multi end |
- (Boolean) optional?
341 342 343 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb', line 341 def optional? @optional end |