Class: Irc::Bot::MessageTemplate
- Inherits:
-
Object
- Object
- Irc::Bot::MessageTemplate
- Defined in:
- /home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb
Overview
MessageTemplate is the class that holds the actual message template map()'d by a BotModule and handled by a MessageMapper
Instance Attribute Summary (collapse)
-
- (Object) botmodule
readonly
the BotModule that map()'d this MessageTemplate.
-
- (Object) defaults
readonly
the defaults hash.
-
- (Object) items
the collection of dynamic and static items in the template.
-
- (Object) options
readonly
the options hash.
-
- (Object) regexp
the Regexp corresponding to the template.
-
- (Object) template
readonly
the actual template string.
Instance Method Summary (collapse)
-
- (MessageTemplate) initialize(botmodule, template, hash = {})
constructor
call-seq: initialize(botmodule, template, opts={}).
- - (Object) inspect
-
- (Object) recognize(m)
Recognize the provided string components, returning a hash of recognized values, or [nil, reason] if the string isn't recognized.
- - (Object) requirements_for(name)
- - (Object) set_auth_path(hash)
Constructor Details
- (MessageTemplate) initialize(botmodule, template, hash = {})
call-seq: initialize(botmodule, template, opts={})
Create a new MessageTemplate associated to BotModule botmodule, with template template and options opts
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb', line 421 def initialize(botmodule, template, hash={}) raise ArgumentError, "Third argument must be a hash!" unless hash.kind_of?(Hash) @defaults = hash[:defaults].kind_of?(Hash) ? hash.delete(:defaults) : {} @requirements = hash[:requirements].kind_of?(Hash) ? hash.delete(:requirements) : {} @template = template case botmodule when String @botmodule = botmodule when Plugins::BotModule @botmodule = botmodule.name else raise ArgumentError, "#{botmodule.inspect} is not a botmodule nor a botmodule name" end self.items = template # @dyn_items is an array of MessageParameters, except for the first entry # which is the template @dyn_items = @items.collect { |it| if it.kind_of?(Symbol) i = it.to_s opt = MessageParameter.new(i) if i.sub!(/^\*/,"") opt.name = i opt.multi = true end opt.default = @defaults[opt.name] opt.collector = @requirements[opt.name] opt else nil end } @dyn_items.unshift(template).compact! debug "Items: #{@items.inspect}; dyn items: #{@dyn_items.inspect}" self.regexp = template debug "Command #{template.inspect} in #{@botmodule} will match using #{@regexp}" set_auth_path(hash) unless hash.has_key?(:action) hash[:action] = items[0] end @options = hash # debug "Create template #{self.inspect}" end |
Instance Attribute Details
- (Object) botmodule (readonly)
the BotModule that map()'d this MessageTemplate
414 415 416 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb', line 414 def botmodule @botmodule end |
- (Object) defaults (readonly)
the defaults hash
409 410 411 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb', line 409 def defaults @defaults end |
- (Object) items
the collection of dynamic and static items in the template
412 413 414 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb', line 412 def items @items end |
- (Object) options (readonly)
the options hash
410 411 412 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb', line 410 def @options end |
- (Object) regexp
the Regexp corresponding to the template
413 414 415 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb', line 413 def regexp @regexp end |
- (Object) template (readonly)
the actual template string
411 412 413 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb', line 411 def template @template end |
Instance Method Details
- (Object) inspect
655 656 657 658 659 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb', line 655 def inspect when_str = @requirements.empty? ? "" : " when #{@requirements.inspect}" default_str = @defaults.empty? ? "" : " || #{@defaults.inspect}" "<#{self.class.to_s} #{@items.map { |c| c.inspect }.join(' ').inspect}#{default_str}#{when_str}>" end |
- (Object) recognize(m)
Recognize the provided string components, returning a hash of recognized values, or [nil, reason] if the string isn't recognized.
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb', line 591 def recognize(m) debug "Testing #{m..inspect} against #{self.inspect}" matching = @regexp.match(m.) return MessageMapper::NoMatchFailure.new(self, m) unless matching return MessageMapper::PartialMatchFailure.new(self, m) unless matching[0] == m. return MessageMapper::NotPrivateFailure.new(self, m) if @options.has_key?(:private) && !@options[:private] && m.private? return MessageMapper::NotPublicFailure.new(self, m) if @options.has_key?(:public) && !@options[:public] && !m.private? debug_match = matching[1..-1].collect{ |d| d.inspect}.join(', ') debug "#{m..inspect} matched #{@regexp} with #{debug_match}" debug "Associating #{debug_match} with dyn items #{@dyn_items.join(', ')}" = @defaults.dup @dyn_items.each_with_index { |it, i| next if i == 0 item = it.name debug "dyn item #{item} (multi-word: #{it.multi?.inspect})" if it.multi? if matching[i].nil? default = it.default case default when Array value = default.clone when String value = default.strip.split when nil, false, [] value = [] else warning "Unmanageable default #{default} detected for :*#{item.to_s}, using []" value = [] end case default when String value.instance_variable_set(:@string_value, default) else value.instance_variable_set(:@string_value, value.join(' ')) end else value = matching[i].split value.instance_variable_set(:@string_value, matching[i]) end def value.to_s @string_value end else if matching[i].nil? warning "No default value for option #{item.inspect} specified" unless @defaults.has_key?(item) value = it.default else value = it.collect(matching[i]) end end [item] = value debug "set #{item} to #{[item].inspect}" } .delete_if {|k, v| v.nil?} # Remove nil values. return end |
- (Object) requirements_for(name)
661 662 663 664 665 666 667 668 669 670 671 672 673 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb', line 661 def requirements_for(name) name = name.to_s.sub(/^\*/,"").intern if (/^\*/ =~ name.inspect) presence = (@defaults.key?(name) && @defaults[name].nil?) requirement = case @requirements[name] when nil then nil when Regexp then "match #{@requirements[name].inspect}" else "be equal to #{@requirements[name].inspect}" end if presence && requirement then "#{name} must be present and #{requirement}" elsif presence || requirement then "#{name} must #{requirement || 'be present'}" else "#{name} has no requirements" end end |
- (Object) set_auth_path(hash)
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/messagemapper.rb', line 470 def set_auth_path(hash) if hash.has_key?(:auth) warning "Command #{@template.inspect} in #{@botmodule} uses old :auth syntax, please upgrade" end if hash.has_key?(:full_auth_path) warning "Command #{@template.inspect} in #{@botmodule} sets :full_auth_path, please don't do this" else pre = @botmodule words = items.reject{ |x| x == pre || x.kind_of?(Symbol) || x =~ /\[|\]/ } if words.empty? post = nil else post = words.first end if hash.has_key?(:auth_path) extra = hash[:auth_path] if extra.sub!(/^:/, "") pre += "::" + post post = nil end if extra.sub!(/:$/, "") if words.length > 1 post = [post,words[1]].compact.join("::") end end pre = nil if extra.sub!(/^!/, "") post = nil if extra.sub!(/!$/, "") extra = nil if extra.empty? else extra = nil end hash[:full_auth_path] = [pre,extra,post].compact.join("::") debug "Command #{@template} in #{botmodule} will use authPath #{hash[:full_auth_path]}" # TODO check if the full_auth_path is sane end end |