Class: Participle::Command
- Inherits:
-
Object
- Object
- Participle::Command
- Defined in:
- lib/command.rb
Overview
A single command, with matching and execution capabilities.
Instance Attribute Summary (collapse)
-
- (Hash<Symbol, Object>) opts
readonly
Options passed to the command.
-
- (Hash<Symbol, String>) params
readonly
The matched, named parameters.
-
- (String, Regexp) pattern
readonly
The command pattern.
Instance Method Summary (collapse)
-
- (void) call(*args)
private
Executor used on commands without an effective message parameter (such as on the join event).
-
- (void) execute(message)
private
Execute the command.
-
- (Command) initialize(pattern, block, opts, trigger, bot)
constructor
Creates a new Command.
-
- (Boolean) match(str)
(also: #===)
Attempt to match this command to a string.
-
- (Boolean) named?(name)
Try to name a command by string (this needs to be made much more advanced).
-
- (Object) setopt(key, val)
The set value.
-
- (String) to_s
Human-readable command.
-
- (Boolean) unfit_for(bot, pkt)
private
Internal method; in the runloop, skips commands which should not be fired.
Constructor Details
- (Command) initialize(pattern, block, opts, trigger, bot)
Creates a new Command.
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)
Options passed to the command
12 13 14 |
# File 'lib/command.rb', line 12 def opts @opts end |
- (Hash<Symbol, String>) params (readonly)
The matched, named parameters
6 7 8 |
# File 'lib/command.rb', line 6 def params @params end |
- (String, Regexp) pattern (readonly)
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.
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/command.rb', line 104 def execute if Regexp === @pattern if @block.arity == 1 @block.call() else @block.call(, @params) end else @block.call(, @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.
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)
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)
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
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.
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 |