annotate Libs/LibDataBroker-1.1/LibDataBroker-1.1.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
yellowfive@57 1
yellowfive@57 2 assert(LibStub, "LibDataBroker-1.1 requires LibStub")
yellowfive@57 3 assert(LibStub:GetLibrary("CallbackHandler-1.0", true), "LibDataBroker-1.1 requires CallbackHandler-1.0")
yellowfive@57 4
yellowfive@57 5 local lib, oldminor = LibStub:NewLibrary("LibDataBroker-1.1", 4)
yellowfive@57 6 if not lib then return end
yellowfive@57 7 oldminor = oldminor or 0
yellowfive@57 8
yellowfive@57 9
yellowfive@57 10 lib.callbacks = lib.callbacks or LibStub:GetLibrary("CallbackHandler-1.0"):New(lib)
yellowfive@57 11 lib.attributestorage, lib.namestorage, lib.proxystorage = lib.attributestorage or {}, lib.namestorage or {}, lib.proxystorage or {}
yellowfive@57 12 local attributestorage, namestorage, callbacks = lib.attributestorage, lib.namestorage, lib.callbacks
yellowfive@57 13
yellowfive@57 14 if oldminor < 2 then
yellowfive@57 15 lib.domt = {
yellowfive@57 16 __metatable = "access denied",
yellowfive@57 17 __index = function(self, key) return attributestorage[self] and attributestorage[self][key] end,
yellowfive@57 18 }
yellowfive@57 19 end
yellowfive@57 20
yellowfive@57 21 if oldminor < 3 then
yellowfive@57 22 lib.domt.__newindex = function(self, key, value)
yellowfive@57 23 if not attributestorage[self] then attributestorage[self] = {} end
yellowfive@57 24 if attributestorage[self][key] == value then return end
yellowfive@57 25 attributestorage[self][key] = value
yellowfive@57 26 local name = namestorage[self]
yellowfive@57 27 if not name then return end
yellowfive@57 28 callbacks:Fire("LibDataBroker_AttributeChanged", name, key, value, self)
yellowfive@57 29 callbacks:Fire("LibDataBroker_AttributeChanged_"..name, name, key, value, self)
yellowfive@57 30 callbacks:Fire("LibDataBroker_AttributeChanged_"..name.."_"..key, name, key, value, self)
yellowfive@57 31 callbacks:Fire("LibDataBroker_AttributeChanged__"..key, name, key, value, self)
yellowfive@57 32 end
yellowfive@57 33 end
yellowfive@57 34
yellowfive@57 35 if oldminor < 2 then
yellowfive@57 36 function lib:NewDataObject(name, dataobj)
yellowfive@57 37 if self.proxystorage[name] then return end
yellowfive@57 38
yellowfive@57 39 if dataobj then
yellowfive@57 40 assert(type(dataobj) == "table", "Invalid dataobj, must be nil or a table")
yellowfive@57 41 self.attributestorage[dataobj] = {}
yellowfive@57 42 for i,v in pairs(dataobj) do
yellowfive@57 43 self.attributestorage[dataobj][i] = v
yellowfive@57 44 dataobj[i] = nil
yellowfive@57 45 end
yellowfive@57 46 end
yellowfive@57 47 dataobj = setmetatable(dataobj or {}, self.domt)
yellowfive@57 48 self.proxystorage[name], self.namestorage[dataobj] = dataobj, name
yellowfive@57 49 self.callbacks:Fire("LibDataBroker_DataObjectCreated", name, dataobj)
yellowfive@57 50 return dataobj
yellowfive@57 51 end
yellowfive@57 52 end
yellowfive@57 53
yellowfive@57 54 if oldminor < 1 then
yellowfive@57 55 function lib:DataObjectIterator()
yellowfive@57 56 return pairs(self.proxystorage)
yellowfive@57 57 end
yellowfive@57 58
yellowfive@57 59 function lib:GetDataObjectByName(dataobjectname)
yellowfive@57 60 return self.proxystorage[dataobjectname]
yellowfive@57 61 end
yellowfive@57 62
yellowfive@57 63 function lib:GetNameByDataObject(dataobject)
yellowfive@57 64 return self.namestorage[dataobject]
yellowfive@57 65 end
yellowfive@57 66 end
yellowfive@57 67
yellowfive@57 68 if oldminor < 4 then
yellowfive@57 69 local next = pairs(attributestorage)
yellowfive@57 70 function lib:pairs(dataobject_or_name)
yellowfive@57 71 local t = type(dataobject_or_name)
yellowfive@57 72 assert(t == "string" or t == "table", "Usage: ldb:pairs('dataobjectname') or ldb:pairs(dataobject)")
yellowfive@57 73
yellowfive@57 74 local dataobj = self.proxystorage[dataobject_or_name] or dataobject_or_name
yellowfive@57 75 assert(attributestorage[dataobj], "Data object not found")
yellowfive@57 76
yellowfive@57 77 return next, attributestorage[dataobj], nil
yellowfive@57 78 end
yellowfive@57 79
yellowfive@57 80 local ipairs_iter = ipairs(attributestorage)
yellowfive@57 81 function lib:ipairs(dataobject_or_name)
yellowfive@57 82 local t = type(dataobject_or_name)
yellowfive@57 83 assert(t == "string" or t == "table", "Usage: ldb:ipairs('dataobjectname') or ldb:ipairs(dataobject)")
yellowfive@57 84
yellowfive@57 85 local dataobj = self.proxystorage[dataobject_or_name] or dataobject_or_name
yellowfive@57 86 assert(attributestorage[dataobj], "Data object not found")
yellowfive@57 87
yellowfive@57 88 return ipairs_iter, attributestorage[dataobj], 0
yellowfive@57 89 end
yellowfive@57 90 end