{"id":27946812,"url":"https://github.com/dmccuskey/lua-objects","last_synced_at":"2025-05-07T13:58:09.960Z","repository":{"id":24651893,"uuid":"28061888","full_name":"dmccuskey/lua-objects","owner":"dmccuskey","description":"Advanced object oriented module for Lua (OOP)","archived":false,"fork":false,"pushed_at":"2015-05-03T06:28:55.000Z","size":572,"stargazers_count":15,"open_issues_count":0,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-07T13:58:07.112Z","etag":null,"topics":[],"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/dmccuskey.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-12-15T23:51:09.000Z","updated_at":"2024-08-25T23:15:00.000Z","dependencies_parsed_at":"2022-08-23T01:51:03.664Z","dependency_job_id":null,"html_url":"https://github.com/dmccuskey/lua-objects","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmccuskey%2Flua-objects","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmccuskey%2Flua-objects/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmccuskey%2Flua-objects/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmccuskey%2Flua-objects/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dmccuskey","download_url":"https://codeload.github.com/dmccuskey/lua-objects/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252892523,"owners_count":21820647,"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":[],"created_at":"2025-05-07T13:58:05.252Z","updated_at":"2025-05-07T13:58:09.948Z","avatar_url":"https://github.com/dmccuskey.png","language":"Lua","funding_links":[],"categories":[],"sub_categories":[],"readme":"## lua-objects ##\n\nAdvanced object oriented module for Lua (OOP)\n\nThis single-file module started its life as [dmc-objects](https://github.com/dmccuskey/dmc-objects) and was used to create mobile apps built with the Corona SDK. It was later refactored into two files `lua_objects.lua` \u0026 `dmc_objects.lua` so that pure-Lua environments could benefit, too (eg, [lua-corovel](https://github.com/dmccuskey/lua-corovel)).\n\nThis power-duo have been used to create relatively complex Lua mobile apps (~60k LOC), clients for websockets and the WAMP-protocol, and countless others.\n\n\n### Features ###\n\n* **_new!_** customizable methods and names for constructor/destructor\n* **_new!_** multiple inheritance (all way to top level)\n* **_new!_** handles ambiguities of inherited attributes\n* **_new!_** advanced support for mixins\n* getters and setters\n* correctly handles missing methods on super classes\n* optimization (copy methods from super classes)\n* **_new!_** unit tested\n\n\n### Examples ###\n\n#### A Simple Custom Class ####\n\nHere's a quick example showing how to create a custom class.\n\n```lua\n--== Import module\n\nlocal Objects = require 'dmc_lua.lua_objects'\n\n\n--== Create a class\n\nlocal AccountClass = newClass()\n \n\n--== Class Properties\n\nAccountClass.DEFAULT_PATH = '/path/dir/'\nAccountClass.DEFAULT_AMOUNT = 100.45\n\n\n--== Class constructor/destructor\n\n-- called from obj:new()\nfunction AccountClass:__new__( params )\n\tparams = params or {}\n\tself._secure = params.secure or true \n\tself._amount = params.amount or self.DEFAULT_AMOUNT \nend\n\n-- called from obj:destroy()\nfunction AccountClass:__destroy__()\n\tself._secure = nil \n\tself._amount = nil \nend\n\n\n--== Class getters/setters\n\nfunction AccountClass.__setters:secure( value )\n\tassert( type(value)=='boolean', \"property 'secure' must be boolean\" )\n\tself._secure = value\nend\nfunction AccountClass.__getters:secure()\n\treturn self._secure\nend\n\n\n--== Class methods\n\nfunction AccountClass:deposit( amount )\n\tself._amount = self._amount + amount\n\tself:dispatchEvent( AccountClass.AMOUNT_CHANGED_EVENT, { amount=self._amount } )\nend\nfunction AccountClass:withdraw( amount )\n\tself._amount = self._amount - amount\nend\n\n```\n\n\n#### Create Class Instance ####\n\nAnd here's how to work with that class.\n\n```lua\n\n-- Create instance\n\nlocal account = AccountClass:new{ secure=true, amount=94.32 }\n\n-- Call methods\n\naccount:deposit( 32.12 )\naccount:withdraw( 50.00 )\n\n\n-- optimize method lookup\n\nobj:optimize()\nobj:deoptimize()\n\n\n-- Check class/object types \n\nassert( AccountClass.is_class == true ), \"AccountClass is a class\" )\nassert( AccountClass.is_instance == false ), \"AccountClass is not an instance\" )\n\nassert( obj.is_class == false, \"an object instance is not a class\" ) \nassert( obj.is_instance == true, \"an objects is an instance of a class\" )\nassert( obj:isa( AccountClass ) == true, \"this obj is an instance of AccountClass\" )\n\n\n-- Destroy instance\n\naccount:destroy()\naccount = nil \n\n```\n\n#### More, Advanced Examples ####\n\nThe project [dmc-objects](https://github.com/dmccuskey/dmc-objects) contains two `lua-objects` sub-classes made for mobile development (`ObjectBase` \u0026 `ComponentBase`). These sub-classes show how to get more out of `lua_objects`, such as:\n\n* custom initialization and teardown\n* custom constructor/destructor names\n* custom Event mixin (add/removeListener/dispatchEvent) [lua-events-mixin](https://github.com/dmccuskey/lua-events-mixin)\n\n\n\n### Custom Constructor/Destructor ###\n\nYou can even customize the names used for construction and destruction.\n\n```lua\n-- use 'create' instead of 'new'\n-- eg,  MyClass:create{ secure=true, amount=94.32 }\n--\nregisterCtorName( 'create' )\n\n-- use 'removeSelf' instead of 'destroy'\n-- eg,  obj:removeSelf()\n--\nregisterDtorName( 'removeSelf' )\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmccuskey%2Flua-objects","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdmccuskey%2Flua-objects","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmccuskey%2Flua-objects/lists"}