Parent

Class/Module Index [+]

Quicksearch

Irc::Bot::MessageParameter

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.

Attributes

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

Public Class Methods

new(name) click to toggle source
# File lib/rbot/messagemapper.rb, line 314
def initialize(name)
  self.name = name
  @multi = false
  @optional = false
  @default = nil
  @regexp = nil
  @index = nil
end

Public Instance Methods

collect(val) click to toggle source

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
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) click to toggle source

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
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() click to toggle source
# 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?() click to toggle source
# File lib/rbot/messagemapper.rb, line 327
def multi?
  @multi
end
name=(val) click to toggle source
# File lib/rbot/messagemapper.rb, line 323
def name=(val)
  @name = val.to_sym
end
optional?() click to toggle source
# File lib/rbot/messagemapper.rb, line 331
def optional?
  @optional
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.