{"id":24193314,"url":"https://github.com/shira-374/lua-object-model","last_synced_at":"2025-03-03T05:43:01.960Z","repository":{"id":25985875,"uuid":"29428058","full_name":"shira-374/lua-object-model","owner":"shira-374","description":"Simple object model implementation in Lua","archived":false,"fork":false,"pushed_at":"2023-04-22T15:07:21.000Z","size":9,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-13T17:16:29.259Z","etag":null,"topics":["class","inheritance","lua","model","object","oop"],"latest_commit_sha":null,"homepage":"","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shira-374.png","metadata":{"files":{"readme":"README.markdown","changelog":"CHANGELOG.rst","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-01-18T14:14:00.000Z","updated_at":"2023-10-13T20:15:13.000Z","dependencies_parsed_at":"2024-12-20T11:40:10.639Z","dependency_job_id":null,"html_url":"https://github.com/shira-374/lua-object-model","commit_stats":null,"previous_names":["shira-374/lua-object-model"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shira-374%2Flua-object-model","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shira-374%2Flua-object-model/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shira-374%2Flua-object-model/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shira-374%2Flua-object-model/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shira-374","download_url":"https://codeload.github.com/shira-374/lua-object-model/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241616681,"owners_count":19991542,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["class","inheritance","lua","model","object","oop"],"created_at":"2025-01-13T17:16:39.054Z","updated_at":"2025-03-03T05:43:01.934Z","avatar_url":"https://github.com/shira-374.png","language":"Lua","readme":"Lua object model\n================\n\nSimple object model implementation in Lua.\n\nInspired by [http://lua-users.org/wiki/SimpleLuaClasses](http://lua-users.org/wiki/SimpleLuaClasses)\n\n\n## Features\n\n- classes\n- constructors\n- destructors\n- inheritance\n- instanceof\n- parent method calls\n\n\n## Requirements\n\n- Lua 5.1 or newer\n\n\n## Module method overview\n\n- `class([parentClass]): table`\n    - create a new class, optionally inheriting methods and properties of another one\n- `instanceof(object, class): boolean`\n    - check if the given object is an instance of the given class or has that class as\n      one of its parents\n- `new(class, ...): object`\n    - create an instance of the given class and invoke its constructor\n    - any additional arguments will be passed to the constructor\n    - should not be used directly, invoke the class table instead (refer to the *Instantiating a class* section)\n- `super(object, methodName, ...): mixed`\n    - invokes a parent method\n    - this is the default implementation of `object:super(method, ...)`\n\n\n## Feature documentation\n\nLet's assume you have imported the `ObjectModel` module like this:\n\n    local o = require 'ObjectModel'\n\n\n### Creating a new class\n\nThe `class` function returns a new table that you can populate with your methods\nand properties.\n\n    MyClass = o.class()\n\n\n### Inheritance\n\nIf another class is passed  to the `class()` function, all of its methods\nand properties are inherited.\n\n    MyClass = o.class(OtherClass)\n\nInternally, a shallow copy of the parent class is created for performance reasons.\nModifications of the parent class at a later point will NOT be propagated to the child\nclasses.\n\n\n### Prototype properties\n\nPrototype properties can be defined in the class table. They are available in all\ninstances of the class and can be accessed statically. Instance properties will shadow\nprototype properties with the same name.\n\n    MyClass.foo = 'bar'\n\n**Warning!** Do not put tables into prototype properties unless you want the table\nto be shared across all the instances (see instance properties).\n\n\n### Methods\n\nLua provides syntactic sugar for this purpose.\n\n    function MyClass:doSomething()\n        -- do something, self points to the instance\n\t\tprint(self.someProperty) -- getting properties\n\t\tprint(self:someMethod()) -- calling other methods\n\tend\n\nSame as prototype properties, methods defined this way are available in all instances\nof the class and can be called statically.\n\n\n### Constructors\n\nConstructor is a special method that is invoked when a class is being instantiated.\n\n    function MyClass:constructor(lorem, ipsum)\n        -- self points to the instance\n    end\n\n\n### Destructors\n\nDestructor is a special method that is invoked when an instance is being garbage-collected\nby Lua.\n\n    function MyClass:destructor()\n        -- self points to the instance\n        -- no arguments are passed\n\tend\n\n\n### Instantiating a class\n\nTo create an instance of a class, simply call the class table as a function.\n\n    local obj = MyClass('something')\n\nAll arguments passed to this function are forwarded to the constructor.\n\n\n### Instance properties\n\nProperties defined on an object are privately owned by that object. They will shadow prototype\nproperties with the same name.\n\n    MyClass.test = 'hello from prototype'\n\n    local obj = MyClass()\n\n\tprint(obj.test) -- prints: hello from prototype\n\tobj.test = 'hello from instance'\n\tprint(obj.test) -- prints: hello from instance\n\tobj.test = nil\n\tprint(obj.test) -- prints: hello from prototype\n\n\n### Instanceof\n\nTo check whether an object is instance of a specific class or has that class as one of its parents,\nuse the `instanceof()` function.\n\n    local obj = MyClass()\n\n    if o.instanceof(obj, MyClass) then\n        print('obj is instance of MyClass')\n    end\n\n\n### Calling parent methods\n\nIf a class has inherited from another and overridden some of its methods, these methods can be\ninvoked using the `:super(methodName, ...)` method.\n\n    BaseClass = o.class()\n\n    function BaseClass:constructor(foo)\n        self.foo = foo\n    end\n\n    DerivedClass = o.class(BaseClass)\n\n    function DerivedClass:constructor(foo, bar)\n        self:super('constructor', foo) -- invoke the parent constructor\n        self.bar = bar\n    end\n\n    local obj = DerivedClass('hello', 'world')\n\n    print(obj.foo, obj.bar) -- prints: hello world\n\n\n### Metamethods\n\nMetamethods can be defined the same way as any other methods and will work as expected.\n\n    function MyClass:__tostring()\n        return 'stringified!'\n    end\n\n    local obj = MyClass()\n\n    print(obj) -- prints: stringified!\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshira-374%2Flua-object-model","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshira-374%2Flua-object-model","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshira-374%2Flua-object-model/lists"}