annotate lib/LibDataBroker-1.1.lua @ 174:09f7af2f53f8

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