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