middleclass

repository·master·Indexed 23 days ago

https://github.com/kikito/middleclass

A lightweight Object-Oriented Programming (OOP) library for Lua that provides inheritance, class variables, metamethods, and weak mixin support. It allows for class definition, superclass initialization, and the use of a .static table for class-level variables and methods.

Tokens
1.2K
Snippets
5
Records
6
Agent score
34%

What's inside middleclass

  1. Migrate from middleclass 2.x to 3.x

    master

    When upgrading to version 3.x, note the following breaking changes:

    1. No Global Variables: class and Object are no longer set on the global scope. You must use require to access them.
    2. Accessing class and Object:
      • Use local class = require 'middleclass' to get the class factory.
      • Access the base object via class.Object.
    3. API Refactoring: The standalone functions instanceOf, subclassOf, and includes have been replaced by methods on instances or static methods on the Object class.

    Note on Parameter Order: When using the new Object static methods, the parameter order has changed compared to the 2.x standalone functions.

    -- 1. Accessing the class factory and Object
    local class = require 'middleclass'
    local Object = class.Object
    
    -- 2. Using the new method-based API (requires valid objects/classes)
    obj:isInstanceOf(MyClass)
    aClass:isSubclassOf(Object)
    aClass:includes(aMixin)
    
    -- 3. Using the Object static API (safe for any type, but note parameter order)
    Object.isInstanceOf(obj, MyClass)
    Object.isSubclassOf(aClass, Object)
    Object.includes(aClass, aMixin)
  2. Migrate from middleclass 3.x to 4.x

    master

    When upgrading to version 4.0, note the following breaking changes:

    1. No Global Object Class: The global Object class has been removed. Classes created via class(<name>) no longer have a default superclass. To restore this behavior, you must explicitly create an Object class and pass it as the superclass to other classes.
    2. Removal of :implements: The class method :implements has been removed.
    3. Metamethod Support: Version 4.0 adds support for the __index metamethod. If you previously manipulated the internal __instanceDict attribute to implement custom lookup logic, you should now use __index instead.

    If you use third-party libraries that depend on middleclass internals, they may break and require updates.

  3. Install middleclass in your Lua project

    master

    To use middleclass, copy the middleclass.lua file into your project directory (e.g., a lib/ folder). You can then access the library using require in any Lua file.

    local class = require 'middleclass'
  4. How to use middleclass for OOP in Lua

    master

    Middleclass provides a simple way to implement Object-Oriented Programming in Lua, supporting inheritance, class variables, and superclass initialization.

    Key patterns:

    • Class Definition: Use class('ClassName') to create a new class. To inherit from another class, use class('SubclassName', ParentClassName).
    • Initialization: Define an initialize method to set up instance state.
    • Superclass Calls: Use ParentClassName.initialize(self, ...) to invoke the parent's initializer.
    • Class Variables/Methods: Use the .static table to define variables or methods that belong to the class itself rather than instances.
    local class = require 'middleclass'
    
    local Fruit = class('Fruit') -- 'Fruit' is the class' name
    
    function Fruit:initialize(sweetness)
      self.sweetness = sweetness
    end
    
    Fruit.static.sweetness_threshold = 5 -- class variable (also admits methods)
    
    function Fruit:isSweet()
      return self.sweetness > Fruit.sweetness_threshold
    end
    
    local Lemon = class('Lemon', Fruit) -- subclassing
    
    function Lemon:initialize()
      Fruit.initialize(self, 1) -- invoking the superclass' initializer
    end
    
    local lemon = Lemon:new()
    
    print(lemon:isSweet()) -- false
  5. Run middleclass specs using busted

    master

    Middleclass uses busted for its specification tests. To run them, ensure busted is installed, navigate to the directory containing the spec folder, and run the busted command.

    cd /folder/where/the/spec/folder/is
    busted