Class: Participle::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/command.rb

Overview

A single command, with matching and execution capabilities.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Command) initialize(pattern, block, opts, trigger, bot)

Creates a new Command.

Parameters:

  • pattern (String, Regexp)

    the pattern to try and match with

  • block (Proc)

    the block to call on Command#execute

  • opts (Hash)

    command options

  • trigger (String)

    the bot's trigger

  • bot (Bot)

    the currently running bot



20
21
22
23
24
25
26
27
# File 'lib/command.rb', line 20

def initialize pattern, block, opts, trigger, bot
  @pattern  = pattern
  @block    = block
  @opts     = opts
  @trigger  = trigger
  @params   = {}
  @bot      = bot
end

Instance Attribute Details

- (Hash<Symbol, Object>) opts (readonly)

Returns options passed to the command

Returns:

  • (Hash<Symbol, Object>)

    options passed to the command



12
13
14
# File 'lib/command.rb', line 12

def opts
  @opts
end

- (Hash<Symbol, String>) params (readonly)

Returns the matched, named parameters

Returns:

  • (Hash<Symbol, String>)

    the matched, named parameters



6
7
8
# File 'lib/command.rb', line 6

def params
  @params
end

- (String, Regexp) pattern (readonly)

Returns the command pattern

Returns:

  • (String, Regexp)

    the command pattern



9
10
11
# File 'lib/command.rb', line 9

def pattern
  @pattern
end

Instance Method Details

- (void) call(*args)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Executor used on commands without an effective message parameter (such as on the join event).



119
120
121
# File 'lib/command.rb', line 119

def call *args
  @block.call(*args)
end

- (void) execute(message)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Execute the command.

Parameters:

  • message (Message)

    the message object to pass to the block



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/command.rb', line 104

def execute message
  if Regexp === @pattern
    if @block.arity == 1
      @block.call(message)
    else
      @block.call(message, @params)
    end
  else
    @block.call(message, @params)
  end
end

- (Boolean) match(str) Also known as: ===

Attempt to match this command to a string. If successful, fills the @params hash with the appropriate values.

Parameters:

  • str (String)

    the string to match

Returns:

  • (Boolean)

    whether the command matches



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/command.rb', line 63

def match str
  # pattern is in regular expression format
  if Regexp === @pattern
    params = str.match(@pattern).to_a
    if params[1]
      # dump captured items from capture groups into @params
      params.each_index{|i|
        next if i.zero?
        @params[i-1] = params[i]
      }
    end
    # match if there is at least one parameter
    return !!params[0]
  else # string is in the colon-keyed format
    patt = @pattern.dup
    syms = patt.scan(/:\??[\w\[\]\|]+/)
    syms.each{|sym|
      if sym =~ /\[(.*)\]/
        patt.gsub!(sym, "(#{$1})")
        sym.gsub!(/\[.*\]/, "")
      end
    }
    patternstr = "(?:#{@opts[:klass].to_s}->)?" + patt.gsub(/ :\w+/, " (.+?)").gsub(/ :\?\w+/, "(?: (.+?))?")
    if @opts[:trigger]
      pattern = Regexp.new('\A' + patternstr + '\Z')
    else
      pattern = Regexp.new(patternstr)
    end
    data = pattern.match(str)
    return false if data.nil?
    syms.each_index{|i|
      @params[syms[i].sub(/:\??/, "").to_sym] = data[i+1]
    }
    return true
  end
end

- (Boolean) named?(name)

Try to name a command by string (this needs to be made much more advanced)

Parameters:

  • name (String)

    the command name

Returns:

  • (Boolean)

    whether the command matches the name



32
33
34
35
36
37
38
# File 'lib/command.rb', line 32

def named?(name)
  if Regexp === @pattern
    name =~ Regexp.new(@pattern.inspect[1...-1].split(" ")[0])
  else
    name == @pattern.split(" ")[0]
  end
end

- (Object) setopt(key, val)

Returns the set value

Parameters:

  • key (Symbol)

    the option key

  • val (Object)

    the value to associate with the key

Returns:

  • (Object)

    the set value



56
57
58
# File 'lib/command.rb', line 56

def setopt(key, val)
  @opts[key] = val
end

- (String) to_s

Human-readable command

Returns:

  • (String)

    the command converted to a string



125
126
127
# File 'lib/command.rb', line 125

def to_s
  Regexp === @pattern ? @pattern.inspect[1...-1] : @pattern
end

- (Boolean) unfit_for(bot, pkt)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Internal method; in the runloop, skips commands which should not be fired.

Parameters:

  • bot (Bot)

    the currently running bot

  • pkt (Packet)

    the packet to test

Returns:

  • (Boolean)

    whether the message complies to all the command requirements



45
46
47
48
49
50
51
# File 'lib/command.rb', line 45

def unfit_for bot, pkt
  return true if @opts[:trigger] &&
    pkt.body[0...bot.configuration.trigger.length] != bot.configuration.trigger
  return true if @opts[:disabled]
  return true if bot.configuration.blacklist.include?(pkt.subpkt[:from])
  return false
end