doc: Add the new grears.matcher features to the lone example.
This isn't really intended to be used outside of the object rules.
This commit is contained in:
parent
801ae69f23
commit
774465df4d
|
@ -11,7 +11,8 @@ local gears = {matcher = require("gears.matcher")} --DOC_HIDE
|
|||
-- This rule will match
|
||||
local rule1 = {
|
||||
rule = {
|
||||
answer = 42,
|
||||
answer = 42,
|
||||
everything = true,
|
||||
},
|
||||
properties = {
|
||||
name = "baz",
|
||||
|
@ -69,6 +70,41 @@ local gears = {matcher = require("gears.matcher")} --DOC_HIDE
|
|||
-- This will add the `rules` to this matcher.
|
||||
matcher:add_matching_rules("second", rules, {"first"}, {})
|
||||
|
||||
--DOC_NEWLINE
|
||||
-- Some properties cannot be checked with the `==` operator (like those
|
||||
-- with multiple possible types). In that case, it is possible to define
|
||||
-- special comparator function.
|
||||
matcher:add_property_matcher("everything", function(obj, value)
|
||||
return value and obj.answer == 42
|
||||
end)
|
||||
|
||||
--DOC_NEWLINE
|
||||
|
||||
-- The same can be done for the property section.
|
||||
matcher:add_property_setter("multiply_by", function(obj, value)
|
||||
obj.answer = (obj.answer or 1) * value
|
||||
end)
|
||||
|
||||
--DOC_NEWLINE
|
||||
|
||||
-- It is possible to append rules to existing (or new) sources.
|
||||
matcher:append_rule( "second", {
|
||||
id = "rule_with_id",
|
||||
rule = {
|
||||
has_elite = true,
|
||||
},
|
||||
properties = {
|
||||
multiply_by = "1337",
|
||||
},
|
||||
})
|
||||
|
||||
--DOC_NEWLINE
|
||||
|
||||
-- Or remove them.
|
||||
local rm3 = --DOC_HIDE
|
||||
matcher:remove_rule("second", "rule_with_id")
|
||||
assert(rm3) --DOC_HIDE
|
||||
|
||||
--DOC_NEWLINE
|
||||
|
||||
-- Apply the properties to `o`
|
||||
|
@ -76,5 +112,12 @@ local gears = {matcher = require("gears.matcher")} --DOC_HIDE
|
|||
|
||||
assert(o.is_everything) --DOC_HIDE
|
||||
assert(o.name == "baz") --DOC_HIDE
|
||||
local rm1 = --DOC_HIDE
|
||||
matcher:remove_matching_source("first") --DOC_HIDE
|
||||
assert(rm1) --DOC_HIDE
|
||||
|
||||
matcher:append_rules("second", {{},{},{}}) --DOC_HIDE
|
||||
|
||||
local rm2 = --DOC_HIDE
|
||||
matcher:remove_matching_source("second") --DOC_HIDE
|
||||
assert(rm2) --DOC_HIDE
|
||||
|
|
Loading…
Reference in New Issue