matcher: Allow rules to be addressed using an identifier.

This commit is contained in:
Emmanuel Lepage Vallee 2019-05-25 10:07:00 -04:00
parent 886f8ea4e3
commit f3f9e4a4b5
2 changed files with 14 additions and 2 deletions

View File

@ -44,3 +44,15 @@
-- @param table -- @param table
-- @see rule -- @see rule
-- @see except -- @see except
--- An identifier for this rule.
--
-- It can be anything. It will be compared with the `==` operator. Strings are
-- highly recommended.
--
-- Setting an `id` is useful to be able to remove the rule by using its id
-- instead of a table reference. Modules can also listen to `rule::appended` and
-- modify or disable a rule.
--
-- @rulecomponent id
-- @param table|string|number|function

View File

@ -362,14 +362,14 @@ end
--- Remove a new rule to the default set. --- Remove a new rule to the default set.
-- @tparam string source The source name. -- @tparam string source The source name.
-- @tparam table rule A valid rule. -- @tparam string|table rule An existing rule or its `id`.
-- @treturn boolean If the rule was removed. -- @treturn boolean If the rule was removed.
-- @method remove_rule -- @method remove_rule
function matcher:remove_rule(source, rule) function matcher:remove_rule(source, rule)
if not self._matching_rules[source] then return end if not self._matching_rules[source] then return end
for k, v in ipairs(self._matching_rules[source]) do for k, v in ipairs(self._matching_rules[source]) do
if v == rule then if v == rule or v.id == rule then
table.remove(self._matching_rules[source], k) table.remove(self._matching_rules[source], k)
self:emit_signal("rule::removed", rule, source, self._matching_rules[source]) self:emit_signal("rule::removed", rule, source, self._matching_rules[source])
return true return true