Sandbox: Lua: Namespace - ov-studio/Vital.sandbox GitHub Wiki

━ What's the Objective?

Namespaces are useful when you've to control or limit the scope of the variables & handlers. Beside that they are also widely used to organize code into logical groups and to prevent naming collisions that can occur especially when you are dealing w/ multiple scripts that utilizes same naming. This module additionally also features a lightweight yet safest way to simulate classes & their instances while having public & private members fully supported out of the box!

Namespace & Class both does support public & private member variables & functions. Private members are completely protected from being accessed via global scope unless the user "INTENTIONALLY" exposes it. Moreover Class can be namespaced as well; i.e you can create classes inside namespace without conflicting the global or other namespaced classes while using same namings!

━ APIs

━ namespace:create() (Shared)

@Objective: Creates the specified namespace.
⚠️ Always ensure to localize the namespace instance to avoid being exposed to global scope.
--Note: Public & Private members can be appended later via cNamespace.public & cNamespace.private respectively.
local namespace: cNamespace = namespace:create(
  string: type --Name of your namespace,
  table: parent --(Optional) Any non class table that you wish to utilize for creating the namespace.
)

━ namespace:destroy() (Shared)

@Objective: Destroys an existing namespace.
⚠️ Destroying namespace will also destroy any further classes & their instances located within it.
local bool: result = namespace:destroy(
  string: type --Name of your namespace
)

━ class:getType() (Shared)

@Objective: Retrieves class's type.
local string: type = self:getType()

━ class:create() (Shared)

@Objective: Creates the specified class.
⚠️ Always ensure to localize the class instance to avoid being exposed to global scope.
--Note: Public & Private members can be appended later via cClass.public & cClass.private respectively.
local class: cClass = class:create(
  string: type, --Name of your class
  table: parent, --(Optional) Any non class table that you wish to utilize for creating the class.
  string: namespace --(Optional) Valid namespace that you prefer to scope the class within. If not specified the instance will be created within the global scope.
)

━ class:destroy() (Shared)

@Objective: Destroys the specified class.
⚠️ Destroying class also destroys all of its belonging instances.
local bool: result = class:destroy(
  class: instance
)

━ class:isInstance() (Shared)

@Objective: Verifies whether the table is an instance of the specified class.
local bool: result = self:isInstance(
  class <instance>: cInstance
)

━ class:createInstance() (Shared)

@Objective: Creates an instance of the specified class.
local class <instance>: cInstance = self:createInstance()

━ class:destroyInstance() (Shared)

@Objective: Destroys specified instance.
local bool: result = cInstance:destroyInstance()
⚠️ **GitHub.com Fallback** ⚠️