Add Component Negation

Fixes #32
This commit is contained in:
Pablo Ariel Mayobre 2023-02-14 18:14:22 -03:00
parent 89eab3fb72
commit 695cc2dfe3
4 changed files with 51 additions and 20 deletions

View file

@ -1,12 +1,11 @@
--- Container for registered ComponentClasses
-- @module Components
local PATH = (...):gsub('%.[^%.]+$', '')
local Type = require(PATH..".type")
local Components = {}
Components.__REJECT_PREFIX = "!"
Components.__REJECT_MATCH = "^(%"..Components.__REJECT_PREFIX.."?)(.+)"
--- Returns true if the containter has the ComponentClass with the specified name
-- @string name Name of the ComponentClass to check
-- @treturn boolean
@ -14,22 +13,43 @@ function Components.has(name)
return rawget(Components, name) and true or false
end
--- Prefix a component's name with the currently set Reject Prefix
-- @string name Name of the ComponentClass to reject
-- @treturn string
function Components.reject(name)
local ok, err = Components.try(name)
if not ok then error(err, 2) end
return Components.__REJECT_PREFIX..name
end
--- Returns true and the ComponentClass if one was registered with the specified name
-- or false and an error otherwise
-- @string name Name of the ComponentClass to check
-- @boolean acceptRejected Whether to accept names prefixed with the Reject Prefix.
-- @treturn boolean
-- @treturn Component or error string
function Components.try(name)
-- @treturn true if acceptRejected was true and the name had the Reject Prefix, false otherwise.
function Components.try(name, acceptRejected)
if type(name) ~= "string" then
return false, "ComponentsClass name is expected to be a string, got "..type(name)..")"
end
local rejected = false
if acceptRejected then
local prefix
prefix, name = string.match(name, Components.__REJECT_MATCH)
rejected = prefix ~= "" and name
end
local value = rawget(Components, name)
if not value then
return false, "ComponentClass '"..name.."' does not exist / was not registered"
end
return true, value
return true, value, rejected
end
--- Returns the ComponentClass with the specified name