annotate Libs/LibStub/LibStub.lua @ 5:c31ee4251181

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