itb-community / ITB-ModLoader

A lua-based mod loader for the game Into the Breach
48 stars 17 forks source link

Add iterateInstanceAndParents and tests #214

Closed Lemonymous closed 1 year ago

Lemonymous commented 1 year ago

Allows key-value pair iteration over custom classes created via: ITB's vanilla CreateClass ModLoader's custom Class.new()

This function returns an iterator function that traverses all key-value pairs of a class instance, including those inherited from parent classes in the class hierarchy, but excluding any keys in parent classes that exists in derived classes.

Example usage 1 - ITB's vanilla CreateClass:

local Object = {
    weight = 10,
    color = "Brown",
}
CreateClass(Object)

local LivingBeing = Object:new{
    breathes = true,
    walks = true,
}

local Animal = LivingBeing:new{
    name = "Animal",
    sound = "Roar",
}

for key, value in iterateInstanceAndParents(Animal:new()) do
    LOG(key, value)
end

Example usage 2 - ModLoader's custom Class.new():

local Object = Class.new()
function Object:new()
    self.weight = 10
    self.color = "Brown"
end

local LivingBeing = Class.inherit(Object)
function LivingBeing:new()
    Object.new(self)
    self.breathes = true
    self.walks = true
end

local Animal = Class.inherit(LivingBeing)
function Animal:new()
    LivingBeing.new(self)
    self.name = "Animal"
    self.sound = "Roar"
end

for key, value in iterateInstanceAndParents(Animal()) do
    LOG(key, value)
end