annotate Libs/LibStub/LibStub.lua @ 57:01b63b8ed811 v21

total rewrite to version 21
author yellowfive
date Fri, 05 Jun 2015 11:05:15 -0700
parents e75889a45130
children
rev   line source
adam@3 1 -- LibStub is a simple versioning stub meant for use in Libraries. http://www.wowace.com/wiki/LibStub for more info
yellowfive@57 2 -- LibStub is hereby placed in the Public Domain Credits: Kaelten, Cladhaire, ckknight, Mikk, Ammo, Nevcairiel, joshborke
adam@3 3 local LIBSTUB_MAJOR, LIBSTUB_MINOR = "LibStub", 2 -- NEVER MAKE THIS AN SVN REVISION! IT NEEDS TO BE USABLE IN ALL REPOS!
adam@3 4 local LibStub = _G[LIBSTUB_MAJOR]
adam@3 5
adam@3 6 if not LibStub or LibStub.minor < LIBSTUB_MINOR then
adam@3 7 LibStub = LibStub or {libs = {}, minors = {} }
adam@3 8 _G[LIBSTUB_MAJOR] = LibStub
adam@3 9 LibStub.minor = LIBSTUB_MINOR
adam@3 10
adam@3 11 function LibStub:NewLibrary(major, minor)
adam@3 12 assert(type(major) == "string", "Bad argument #2 to `NewLibrary' (string expected)")
adam@3 13 minor = assert(tonumber(strmatch(minor, "%d+")), "Minor version must either be a number or contain a number.")
adam@3 14
adam@3 15 local oldminor = self.minors[major]
adam@3 16 if oldminor and oldminor >= minor then return nil end
adam@3 17 self.minors[major], self.libs[major] = minor, self.libs[major] or {}
adam@3 18 return self.libs[major], oldminor
adam@3 19 end
adam@3 20
adam@3 21 function LibStub:GetLibrary(major, silent)
adam@3 22 if not self.libs[major] and not silent then
adam@3 23 error(("Cannot find a library instance of %q."):format(tostring(major)), 2)
adam@3 24 end
adam@3 25 return self.libs[major], self.minors[major]
adam@3 26 end
adam@3 27
yellowfive@57 28 function LibStub:IterateLibraries() return pairs(self.libs) end
adam@3 29 setmetatable(LibStub, { __call = LibStub.GetLibrary })
adam@3 30 end